labelme标注的多分类数据集转化为YOLO数据集(json转txt)

1、如何使用脚本

        (此脚本支持多分类的目标检测数据)

        1、修改dir_json为自己生成的coco数据集文件夹目录

        2、修改dir_txt为要放进去Yolo格式标签的文件夹目录

        3、直接运行代码

import os
import json

# labelme标注的json标签文件目录和保存生成的txt标签的文件夹
dir_json = r'D:/Json/'
dir_txt = r'D:/txt/'
# os.mkdir(dir_txt)

classes2id = {}
num = 0
jsons = os.listdir(dir_json)
for i in jsons:
    json_path = os.path.join(dir_json, i)
    with open(json_path, 'r', encoding="utf-8") as f:
        json_data = json.load(f)
        # print(json_data['shapes'])
        for j in json_data['shapes']:
            if j['label'] not in classes2id:
                classes2id[j['label']] = num
                num += 1
print(classes2id)


def json2txt(path_json, path_txt):  # 可修改生成格式
    with open(path_json, 'r', encoding='utf-8') as path_json:
        jsonx = json.load(path_json)
        with open(path_txt, 'w+') as ftxt:
            shapes = jsonx['shapes']
            # 获取图片长和宽
            width = jsonx['imageWidth']
            height = jsonx['imageHeight']
            # print(shapes)
            cat=shapes[0]['label']
            cat=classes2id[cat]

            for shape in shapes:
                # 获取矩形框两个角点坐标
                x1 = shape['points'][0][0]
                y1 = shape['points'][0][1]
                x2 = shape['points'][1][0]
                y2 = shape['points'][1][1]

                dw = 1. / width
                dh = 1. / height
                x = dw * (x1 + x2) / 2
                y = dh * (y1 + y2) / 2
                w = dw * abs(x2 - x1)
                h = dh * abs(y2 - y1)
                yolo = f"{cat} {x} {y} {w} {h} \n"
                ftxt.writelines(yolo)


list_json = os.listdir(dir_json)
for cnt, json_name in enumerate(list_json):
    if os.path.splitext(json_name)[-1] == ".json":
        path_json = dir_json + json_name
        path_txt = dir_txt + json_name.replace('.json', '.txt')
        json2txt(path_json, path_txt)

2、实现过程

读取所有的.json文件,遍历后生成classes2id字典

classes2id={}
num=0
jsons=os.listdir(dir_json)
for i in jsons:
    json_path=os.path.join(dir_json,i)
    with open(json_path, 'r',encoding="utf-8") as f:
        json_data = json.load(f)
        #print(json_data['shapes'])
        for j in json_data['shapes']:
            if j['label'] not in classes2id:
                classes2id[j['label']]=num
                num+=1
print(classes2id)

 将.json文件转化为.txt文件

def json2txt(path_json, path_txt):  # 可修改生成格式
    with open(path_json, 'r', encoding='utf-8') as path_json:
        jsonx = json.load(path_json)
        with open(path_txt, 'w+') as ftxt:
            shapes = jsonx['shapes']
            # 获取图片长和宽
            width = jsonx['imageWidth']
            height = jsonx['imageHeight']
            # print(shapes)
            cat=shapes[0]['label']
            cat=classes2id[cat]

            for shape in shapes:
                # 获取矩形框两个角点坐标
                x1 = shape['points'][0][0]
                y1 = shape['points'][0][1]
                x2 = shape['points'][1][0]
                y2 = shape['points'][1][1]

                dw = 1. / width
                dh = 1. / height
                x = dw * (x1 + x2) / 2
                y = dh * (y1 + y2) / 2
                w = dw * abs(x2 - x1)
                h = dh * abs(y2 - y1)
                yolo = f"{cat} {x} {y} {w} {h} \n"
                ftxt.writelines(yolo)

  • 16
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 22
    评论
要将labelme标注的数据换成yolo可识别的数据标注集,需要进行以下步骤: 1. 解析labelme标注文件:labelme标注工具会生成一个或多个JSON文件,每个文件对应一张图片的标注信息。首先需要读取这些JSON文件,并解析出图片路径、图片尺寸以及标注框的位置和类别信息。 2. 换坐标格式:labelme标注框的位置信息通常是以像素坐标表示的,而yolo需要的是归一化后的坐标。因此,需要将像素坐标换成相对于图片尺寸的归一化坐标。 3. 换类别标签:labelme标注的类别信息可能是文字形式的,而yolo需要的是数字形式的类别标签。需要将文字形式的类别标签映射成数字形式的类别标签。 4. 生成yolo标注文件:根据yolo标注文件格式,将换后的图片路径、图片尺寸、归一化坐标和类别标签信息写入到一个文本文件中,作为yolo可识别的数据标注集。 下面是一个示例代码,用于将labelme标注的数据换成yolo可识别的数据标注集: ```python import json def convert_labelme_to_yolo(labelme_file, yolo_file): with open(labelme_file, 'r') as f: data = json.load(f) image_path = data['imagePath'] image_width = data['imageWidth'] image_height = data['imageHeight'] with open(yolo_file, 'w') as f: for shape in data['shapes']: label = shape['label'] points = shape['points'] x_min = min(points, key=lambda x: x[0])[0] y_min = min(points, key=lambda x: x[1])[1] x_max = max(points, key=lambda x: x[0])[0] y_max = max(points, key=lambda x: x[1])[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 = 0 # 根据需要自行映射类别标签 line = f"{class_id} {x_center} {y_center} {width} {height}\n" f.write(line) convert_labelme_to_yolo('labelme.json', 'yolo.txt') ``` 请将上述代码保存为一个Python脚本,并将`labelme.json`替换为你的labelme标注文件的路径,将`yolo.txt`替换为你想要生成的yolo标注文件的路径。运行脚本后,即可生成yolo可识别的数据标注集。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TechMasterPlus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值