lableme的 json (矩形框标注)转换为yolo 格式的txt --- (类别 x,y,w,h形式)

LabelMetoYOLO转换脚本:将标注数据转换为YOLO所需的格式
import json
import os

def convert_labelme_to_yolo(json_path, txt_path, label_dict):
    with open(json_path, 'r') as file:
        data = json.load(file)

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

    with open(txt_path, 'w') as file:
        for shape in data['shapes']:
            label = shape['label']
            points = shape['points']
            x_min, y_min = points[0]
            x_max, y_max = points[1]

            x_center = (x_min + x_max) / 2 / image_width
            y_center = (y_min + y_max) / 2 / image_height
            width = (x_max - x_min) / image_width
            height = (y_max - y_min) / image_height

            class_id = label_dict[label]
            file.write(f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n")

def batch_convert(input_folder, output_folder, label_dict):
    os.makedirs(output_folder, exist_ok=True)
    for file_name in os.listdir(input_folder):
  
在目标检测任务中,x-anylabeling生成的旋目标检测标注数据为json格式,而yolo系列项目支持yolo格式txt训练数据。为了将x-anylabeling的旋目标检测标注数据json转换yolo格式txt训练数据,可参考以下思路: 首先,要了解两种数据格式的特点。x-anylabeling生成的json格式数据包含了旋目标检测的详细标注信息,yolo格式txt文件则每行代表一个目标,包含类别编号、中心点坐标、宽高以及旋角度等信息(对于旋目标检测)。 可以编写Python代码来实现转换。以下是一个示例代码框架: ```python import json import os def convert_json_to_yolo(json_file_path, output_dir, class_mapping): # 读取json文件 with open(json_file_path, 'r', encoding='utf-8') as f: data = json.load(f) # 获取图像尺寸 image_width = data['imageWidth'] image_height = data['imageHeight'] # 初始化输出的txt文件内容 txt_content = [] # 遍历所有标注 for shape in data['shapes']: label = shape['label'] # 获取类别编号 class_id = class_mapping[label] # 获取旋矩形标注信息,这里假设标注信息存储在points中 points = shape['points'] # 计算中心点坐标、宽高和旋角度(具体计算根据实际标注格式调整) # 以下是一个简化示例,实际需要根据旋矩形的表示方式进行计算 x_center = sum([point[0] for point in points]) / len(points) / image_width y_center = sum([point[1] for point in points]) / len(points) / image_height width = max([point[0] for point in points]) - min([point[0] for point in points]) / image_width height = max([point[1] for point in points]) - min([point[1] for point in points]) / image_height angle = 0 # 这里假设旋角度为0,实际需要根据标注信息计算 # 构建yolo格式的一行数据 line = f"{class_id} {x_center} {y_center} {width} {height} {angle}" txt_content.append(line) # 生成txt文件名 txt_file_name = os.path.splitext(os.path.basename(json_file_path))[0] + '.txt' txt_file_path = os.path.join(output_dir, txt_file_name) # 写入txt文件 with open(txt_file_path, 'w', encoding='utf-8') as f: f.write('\n'.join(txt_content)) # 示例使用 json_file_path = 'path/to/your/json/file.json' output_dir = 'path/to/output/directory' # 定义类别映射,将类别名称映射到编号 class_mapping = {'class1': 0, 'class2': 1} convert_json_to_yolo(json_file_path, output_dir, class_mapping) ``` 上述代码通过读取x-anylabeling的json标注文件,解析其中的标注信息,将其转换yolo格式txt文件内容,并写入到对应的txt文件中。需要注意的是,代码中的旋矩形信息计算部分是简化示例,实际应用中需要根据x-anylabeling具体的标注格式进行调整。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值