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

相关推荐
qq_25294131683 小时前
混凝土损伤分割与检测数据集 | 混凝土损伤 实例分割 裂缝检测 裸露钢筋 结构健康监测9261期
人工智能·yolo·目标检测·计算机视觉·混泥土
rich_Chan.3 小时前
阿里云百炼联网搜索教程:web_search 工具开启方法、web_extractor 网页抓取与 json 检索轨迹
前端·阿里云·json
a puppy slide 葱4 小时前
ICML | 2026 | MFH‑NAS:面向多模态融合目标检测的混合神经架构搜索框架
人工智能·yolo·目标检测·计算机视觉·架构·nas·神经网络架构搜索
2601_9679873716 小时前
用Python+OpenCV+YOLO写个游戏脚本:从自动瞄准到自动采集的保姆级教程
python·opencv·yolo·目标检测·游戏自动化
SEO_juper17 小时前
Node.js 重定向链检测工具:301 跳了几跳、有没有成环,一次看清
xml·开发语言·python·node.js·json·bash
代码丰18 小时前
跨系统传输大 JSON:直接压缩,还是把数据放进 Redis?
redis·后端·json
迪菲赫尔曼18 小时前
【基础篇 02】文件、文件夹和路径:先把学习资料放对地方
yolo·计算机视觉·论文·教程·ultralyt
司曜珩21 小时前
构建AI友好型网站:用JSON-LD给企业官网上“户口”的实战指南
人工智能·json
小豆庄1 天前
AI换脸能骗过检测,视频脱敏为什么该回到本地
人工智能·yolo·阿里云·ai·ocr·视频
Jmyd01231 天前
智慧广交会常态化运营下,企业如何用3D展厅把展会流量变成365天询盘
json