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

相关推荐
xw33734095648 分钟前
目标检测基础
人工智能·yolo
钓了猫的鱼儿3 小时前
无人机航拍数据集|第23期 无人机军事目标检测YOLO数据集1700张yolov11/yolov8/yolov5可训练
yolo·目标检测·猫脸码客·无人机航拍数据集·无人机军事目标检测数据集
迪菲赫尔曼17 小时前
大模型入门实战 | 基于 YOLO 数据集微调 Qwen2.5-VL-3B-Instruct 的目标检测任务
人工智能·yolo·目标检测·大模型·微调·新手入门·qwen2.5
无知的前端1 天前
Flutter 模型转JSON跳过零值/null
flutter·json
钓了猫的鱼儿1 天前
无人机航拍数据集|第32期 无人机采矿区作业目标检测YOLO数据集202张yolov11/yolov8/yolov5可训练
yolo·目标检测·无人机·猫脸码客·无人机航拍数据集·无人机采矿区作业
钓了猫的鱼儿2 天前
无人机航拍数据集|第20期 无人机公路损伤目标检测YOLO数据集3771张yolov11/yolov8/yolov5可训练
yolo·目标检测·无人机·猫脸码客·无人机航拍数据集·无人机公路损伤目标检测数据集
钓了猫的鱼儿2 天前
无人机航拍数据集|第27期 无人机交通目标检测YOLO数据集3717张yolov11/yolov8/yolov5可训练
人工智能·yolo·目标检测
404Clukay2 天前
在VSCode中配置.NET项目的tasks.json以实现清理、构建、热重载和发布等操作
vscode·json·.net
A7bert7772 天前
【YOLOv5部署至RK3588】模型训练→转换RKNN→开发板部署
c++·人工智能·python·深度学习·yolo·目标检测·机器学习
Coovally AI模型快速验证2 天前
YOLOv8-SMOT:基于切片辅助训练与自适应运动关联的无人机视角小目标实时追踪框架
人工智能·深度学习·yolo·计算机视觉·目标跟踪·无人机