labelme 图像分割数据标注后格式转换 json->txt

#coding=utf-8
import json
import os


def convert_labelme_to_yolo(json_file, output_txt_file, label_to_class_id):
    with open(json_file, 'r') as file:
        data = json.load(file)

    image_width = data['imageWidth']
    image_height = data['imageHeight']

    with open(output_txt_file, 'w') as file:
        for shape in data['shapes']:
            points = shape['points']
            class_id = label_to_class_id.get(shape['label'], -1)
            normalized_points = []
            for x, y in points:
                nx = round(x / image_width, 6)
                ny = round(y / image_height, 6)
                normalized_points.append(f"{nx} {ny}")
            file.write(f"{class_id} " + " ".join(normalized_points) + "\n")


def batch_convert(json_folder, output_folder, label_to_class_id):
    # 确保输出文件夹存在
    os.makedirs(output_folder, exist_ok=True)

    # 遍历文件夹中的所有JSON文件
    for filename in os.listdir(json_folder):
        if filename.endswith
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值