Label打标数据转成YOLO数据格式
# from ultralytics.yolo.data.converter import convert_coco
#
# convert_coco(labels_dir=r'D:\github\ultralytics\111')
import cv2
import json
import os
source_path = r"D:\github\ultralytics\111"
dest_path = r"D:\github\ultralytics\222"
class_list = [ 'cut_corner' ,'with_corner']
for jsonfile in os.listdir(source_path):
if jsonfile.endswith(".json"):
with open(source_path + '//' + jsonfile, 'r') as f: #打开json文件,读取数据
data = json.load(f)
#
class_name = data.get("shapes")[0].get("label")
points = data.get("shapes")[0].get("points")
imageHeight = data.get("imageHeight")
imageWidth = data.get("imageWidth")
Center_X = float(points[0][0] + points[1][0]) /2
Center_Y = float(points[0][1] + points[1][1]) /2
Width = abs(float(points[0][0]) - float(points[1][0]))
Height = abs(float(points[0][1]) - float(points[1][1]))
#算出类别、四个点的坐标
#新建一个txt同名文件
file = open(dest_path + "\\"+ jsonfile.split(".json")[0] + '.txt', 'a')
file.write(str(class_list.index(class_name)) + ' ' + str(Center_X / imageWidth) + ' ' + str(Center_Y / imageHeight) + ' ' + str(Width / imageWidth) + ' ' + str(Height / imageHeight))