Bootstrap

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)

;