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

相关推荐
weixin_457340215 小时前
旋转OBB数据集标注查看器
图像处理·人工智能·python·yolo·目标检测·数据集·旋转
糖果罐子♡5 小时前
在 openEuler 上部署 YOLOv8 实现实时目标检测
人工智能·yolo·目标检测
plmm烟酒僧6 小时前
OpenVINO 推理 YOLO Demo 分享 (Python)
图像处理·人工智能·python·yolo·openvino·runtime·推理
Coding茶水间8 小时前
基于深度学习的水稻虫害检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
踏浪无痕8 小时前
你真的懂泛型吗?手写 MyBatis-Plus + Jackson,揭秘框架设计的精髓
后端·json·mybatis
超龄超能程序猿9 小时前
LabelImage+YOLOv8 图片单一目标检测模型训练
人工智能·yolo·目标检测
AI-嘉文哥哥10 小时前
ADAS自动驾驶-前车碰撞预警(追尾预警、碰撞检测)系统
人工智能·深度学习·yolo·目标检测·数据分析·课程设计·qt5
Appreciate(欣赏)10 小时前
Spark解析JSON字符串
大数据·spark·json
wtsolutions10 小时前
JSON转Excel工具新增WPS插件功能,将JSON转换成WPS表格工作表数据
json·excel·wps·插件·转换·加载项·wtsolutions