1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import os import json names = ['干电池', '废弃水银温度计', '废旧灯管灯泡', '杀虫剂容器', '电池', '软膏', '过期药物', '除草剂容器'] def calculate(x1,x2,y1,y2,h,w): x = 1.0*(x1+x2)/(2*h) y = 1.0 * (y1 + y2) / (2 * w) width = 1.0*(x2-x1)/w height = 1.0*(y2-y1)/h return x,y,width,height input_path = 'data_set/' output_path = 'output/' json_files = os.listdir(input_path) for json_file in json_files: txt_file_name = json_file.split('.')[0]+'.txt' try: data = json.load(open(input_path+json_file,'r',encoding='UTF-8')) except: print(json_file) continue if not os.path.exists(output_path): os.mkdir(output_path)
txt_file = open(output_path+txt_file_name,'w',encoding='UTF-8') for i in data['labels']: class_id = names.index(i['name']) x1 = i['x1'] x2 = i['x2'] y1 = i['y1'] y2 = i['y2'] h = i['size']['height'] w = i['size']['width'] ans = calculate(x1,x2,y1,y2,h,w) txt_file.write("%s %s %s %s %s\n" % (class_id,ans[0],ans[1],ans[2],ans[3])) txt_file.close()
|