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

相关推荐
极智视界6 小时前
无人机场景 - 目标检测数据集 - 停车场停车位检测数据集下载
yolo·目标检测·数据集·无人机·voc·coco·算法训练
前网易架构师-高司机10 小时前
带标注信息的手机识别数据集,92.8%识别率,可识别户外公共场所的人是否带手机,支持yolo, coco json,pascal voc xml格式
yolo·手机·数据集·公共·户外·携带
说给风听.11 小时前
零基础学 JSON:原理、语法与应用全解析
json
Faker66363aaa11 小时前
基于YOLOv8-P2的稻田杂草智能分割与识别系统
yolo
极智视界12 小时前
目标检测数据集 - 空中固定翼无人机检测数据集下载
yolo·目标检测·数据集·无人机·voc·coco·算法训练
ASD123asfadxv14 小时前
YOLOv10n-RepVit实现螺钉螺母智能检测与计数系统
yolo
天若有情67314 小时前
XiangJsonCraft v1.2.0重大更新解读:本地配置优先+全量容错,JSON解耦开发体验再升级
前端·javascript·npm·json·xiangjsoncraft
你再说一遍?36416 小时前
计算机视觉实训作业记录:基于 YOLOv12 的水下目标检测模型优化与实现
yolo·目标检测·计算机视觉
LASDAaaa123117 小时前
红外图像中的鸟类目标检测:YOLOv5-SPDConv改进实践
yolo·目标检测·目标跟踪
Sylvia33.17 小时前
足球“文字直播/事件流”API详解:解码比赛的数字DNA
java·服务器·前端·json