labelme标注格式转yolov5

import json
import os


def convert(img_size, box):
    dw = 1. / (img_size[0])
    dh = 1. / (img_size[1])
    x = (box[0] + box[2]) / 2.0
    y = (box[1] + box[3]) / 2.0
    w = box[2] - box[0]
    h = box[3] - box[1]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    #x1 = box[0]
    #y1 = box[1]
    #x2 = box[2]
    #y2 = box[3]
    return (x, y, w, h)  #return (x1, y1, x2, y2)


def decode_json(json_floder_path, json_name):
    #print(json_name)
    txt_name = 'data/' + json_name[0:-5] + '.txt'  # 生成txt文件你想存放的路径
    txt_file = open(txt_name, 'w')

    json_path = os.path.join(json_floder_path, json_name)
    print(json_path)

    data = json.load(open(json_path, 'r', encoding='utf-8'))
    #exit()
    img_w = data['imageWidth']
    img_h = data['imageHeight']

    for i in data['shapes']:

        if (i['shape_type'] == 'rectangle' and i['label'] == '0'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('0' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '1'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('1' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '2'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('2' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '3'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('3' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '4'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('4' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '5'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('5' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '6'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('6' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '7'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('7' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '8'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('8' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == '9'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('9' + " " + " ".join([str(a) for a in bbox]) + '\n')

        if (i['shape_type'] == 'rectangle' and i['label'] == 'b'):  # 分类的标签
            x1 = float((i['points'][0][0]))
            y1 = float((i['points'][0][1]))
            x2 = float((i['points'][1][0]))
            y2 = float((i['points'][1][1]))

            bb = (x1, y1, x2, y2)
            bbox = convert((img_w, img_h), bb)
            txt_file.write('10' + " " + " ".join([str(a)
                                                  for a in bbox]) + '\n')


if __name__ == "__main__":

    json_floder_path = 'data/'  # json文件的路径
    #json_names = os.listdir(json_floder_path)
    #for json_name in json_names:
    #    decode_json(json_floder_path, json_name)
    for file in os.listdir('data/'):
        if 'json' in file:
            #print(file)
            decode_json(json_floder_path, file)

你可能感兴趣的:(目标检测)