yolo txt 转 labelme json 格式

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 json
    txts = 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_fmt
        labelme_json['imagePath'] = image_name
        image_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'] = h
        labelme_json['imageWidth'] = w
        with 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) * w
                top_left_y = (float(content[2]) - object_height / 2) * h
                bottom_right_x = (float(content[1]) + object_width / 2) * w
                bottom_right_y = (float(content[2]) + object_height / 2) * h
                shape = {
                    '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

相关推荐
仙女修炼史2 小时前
ultralytics-yolov8的数据增强
人工智能·opencv·yolo
浪浪山小野猪4 小时前
JSON 常见错误大全:尾逗号、单引号、注释,一次讲透
json
YOLO数据集集合8 小时前
无人机检测与追踪数据集 | 无人机检测 反无人机 低空安防 目标追踪 YOLO格式9010期
人工智能·yolo·目标检测·计算机视觉·无人机·无人机检测
AC赳赳老秦8 小时前
个保法下数据处理:OpenClaw 自动过滤公开数据中的个人信息,保障采集分析合规性
java·python·sqlite·json·php·deepseek·openclaw
Web - Anonymous9 小时前
Vue3 + Element Plus 集成 JSON Editor 实现实时校验 JSON 编辑器(高亮/暗亮主题/分栏回显/组件复用) - 附完整示例
vue.js·编辑器·json
码云骑士9 小时前
116-Python调用GPT-4V分析图片-Base64编码-多图-JSON结构化输出
开发语言·python·json
qq_252941316810 小时前
山体滑坡目标检测数据集 | 山体滑坡检测 地质灾害识别 遥感监测 目标检测 YOLO格式
人工智能·yolo·目标检测·计算机视觉·视觉检测·自然灾害·滑坡数据集
龙虾PRO1 天前
大模型稳定输出 JSON 的四层防线:2026 生产环境落地避坑指南
json
空堂与归1 天前
实时目标检测怎么做到又快又准?YOLOv12架构深度解析
人工智能·yolo·目标检测·计算机视觉·架构
FL16238631291 天前
电力场景变电站火灾检测数据集VOC+YOLO格式564张2类别
人工智能·yolo·机器学习