yolo txt 转 labelme json 格式

news/2025/2/15 21:38:09/

talk is cheap show me the code!

def convert_txt_to_labelme_json(txt_path, image_path, output_dir, image_fmt='.jpg'):# txt 转labelme json# 将yolo的txt转labelme jsontxts = glob.glob(os.path.join(txt_path, "*.txt"))for txt in txts:labelme_json = {'version': '4.5.7','flags': {},'shapes': [],'imagePath': None,'imageData': None,'imageHeight': None,'imageWidth': None,}txt_name = os.path.basename(txt)image_name = txt_name.split(".")[0] + image_fmtlabelme_json['imagePath'] = image_nameimage_name = os.path.join(image_path, image_name)if not os.path.exists(image_name):raise Exception('txt 文件={},找不到对应的图像={}'.format(txt, image_name))image = cv2.imdecode(np.fromfile(image_name, dtype=np.uint8), cv2.IMREAD_COLOR)h, w = image.shape[:2]labelme_json['imageHeight'] = hlabelme_json['imageWidth'] = wwith open(txt, 'r') as t:lines = t.readlines()for line in lines:content = line.split(' ')label = content[0]object_width = float(content[3])object_height = float(content[4])top_left_x = (float(content[1]) - object_width / 2) * wtop_left_y = (float(content[2]) - object_height / 2) * hbottom_right_x = (float(content[1]) + object_width / 2) * wbottom_right_y = (float(content[2]) + object_height / 2) * hshape = {'label': str(label),'group_id': None,'shape_type': 'rectangle','flags': {},'points': [[float(top_left_x), float(top_left_y)],[float(bottom_right_x), float(bottom_right_y)]]}labelme_json['shapes'].append(shape)json_name = txt_name.split('.')[0] + '.json'json_name_path = os.path.join(output_dir, json_name)fd = open(json_name_path, 'w')json.dump(labelme_json, fd, indent=4)fd.close()print("save json={}".format(json_name_path))

附 Yolo 坐标系格式: https://roboflow.com/formats/yolov5-pytorch-txt
在这里插入图片描述


http://www.ppmy.cn/news/1013413.html

相关文章

java文件

一.File类 二.扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件 我的代码: import java.io.File; import java.io.IOException; import java.util.Scanner;public class Tes…

SQLAlchemy与标准SQL相比有哪些优点?

让我来给你讲讲SQLAlchemy和标准SQL相比有哪些优点吧! 首先,我们要知道,SQLAlchemy是一个Python的SQL工具包和对象关系映射(ORM)系统,它把Python的面向对象编程(OOP)的理念带入了数…

机器学习笔记之优化算法(十一)凸函数铺垫:梯度与方向导数

机器学习笔记之优化算法——凸函数铺垫:梯度与方向导数 引言回顾:偏导数方向余弦方向导数方向导数的几何意义方向导数的定义 方向导数与偏导数之间的关联关系证明过程 梯度 ( Gradient ) (\text{Gradient}) (Gradient) 引言 本节作为介绍凸函数的铺垫&a…

FastAPI 构建 API 高性能的 web 框架(一)

如果要部署一些大模型一般langchainfastapi,或者fastchat, 先大概了解一下fastapi,本篇主要就是贴几个实际例子。 官方文档地址: https://fastapi.tiangolo.com/zh/ 1 案例1:复旦MOSS大模型fastapi接口服务 来源:大语言模型工程…

封装上传文件组件(axios,onUploadProgress,取消请求)

目录 定时模拟进度条 方法 A.axios B.xhr 取消请求​​​​​​​ 完整代码 A.自定义上传组件 B.二次封装组件 情况 增加cancelToken不生效,刷新页面 进度条太快->设置浏览器网速 定时模拟进度条 startUpload() {if (!this.file) return;const totalS…

0基础学习VR全景平台篇 第79篇:全景相机-泰科易如何直播推流

泰科易科技是中国的一家研发全景相机的高科技公司,前不久,在2020世界VR产业大会上发布了新一代5G VR直播影像采集终端--360starlight。以其出色的夜景成像效果和一“部”到位的直播方案重新定义了VR慢直播相机,对行业具有高度借鉴意义。 本文…

SpringCloud项目打包注意事项以及可能出错的几种情况

SpringCloud项目打包注意事项和可能出错的几种情况 1、检查子模块中的 parent的pom文件路径 \<relativePath/\>2、检查打包插件的位置3、检查module是否重复引用 欢迎访问我的个人博客&#xff1a;https://wk-blog.vip 1、检查子模块中的 parent的pom文件路径 <relat…

【果树农药喷洒机器人】Part2:机器人变量喷药系统硬件选型

本专栏介绍&#xff1a;付费专栏&#xff0c;持续更新机器人实战项目&#xff0c;欢迎各位订阅关注。 关注我&#xff0c;带你了解更多关于机器人、嵌入式、人工智能等方面的优质文章&#xff01; 文章目录 一、引言二、变量喷药系统总体要求2.1系统功能要求2.2系统技术要求 三…