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

相关推荐
江西省遂川县常驻深圳大使41 分钟前
openclaw.json配置示例
服务器·json·openclaw
爱睡懒觉的焦糖玛奇朵1 小时前
【工业级落地算法之人员摔倒检测算法详解】
人工智能·python·深度学习·神经网络·算法·yolo·目标检测
先做个垃圾出来………2 小时前
JSON序列化问题
数据库·json
reset20214 小时前
YOLOv8 图像分类过拟合解决方案
人工智能·yolo
亚历克斯神5 小时前
Flutter 三方库 jwt_io 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、全能的 JSON Web Token (JWT) 加解密与身份安全验证引擎
flutter·json·harmonyos
code_pgf8 小时前
yolo系列综述
yolo
羊羊小栈8 小时前
基于「YOLO目标检测 + 多模态AI分析」的桥梁缺陷智能分析监测预警系统
人工智能·yolo·目标检测·计算机视觉·毕业设计·大作业
咏&志9 小时前
目标检测之YOLOV2论文简读
人工智能·yolo·目标检测
冉佳驹9 小时前
Linux ——— 网络开发核心知识与协议实现详解
linux·http·https·udp·json·tcp·端口号
尘中客10 小时前
Postman进阶实战:优雅调试带 GZIP 压缩与百KB级复杂嵌套 JSON 的 RESTful API
json·postman·restful·php开发·gzip解压