Labelme转为yolov3 数据集

from os import getcwd
import os
import json
import glob
wd = getcwd()
"labelme标注的json 数据集转为keras 版yolov3的训练集"
classes = ["aircraft","oiltank"]

image_ids = glob.glob(r"LabelmeData/*jpg")
print(image_ids)
list_file = open('train.txt', 'w')

def convert_annotation(image_id, list_file):
    jsonfile=open('%s.json' % (image_id))
    in_file = json.load(jsonfile)

    for i in range(0,len(in_file["shapes"])):
        object=in_file["shapes"][i]
        cls=object["label"]
        points=object["points"]
        xmin=int(points[0][0])
        ymin=int(points[0][1])
        xmax=int(points[1][0])
        ymax=int(points[1][1])
        if cls not in classes:
            print("cls not in classes")
            continue
        cls_id = classes.index(cls)
        b = (xmin, ymin, xmax, ymax)
        list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
    jsonfile.close()
for image_id in image_ids:
    list_file.write('%s.jpg' % (image_id.split('.')[0]))
    convert_annotation(image_id.split('.')[0], list_file)
    list_file.write('\n')
list_file.close()
 

你可能感兴趣的:(人工智能,python)