import json import os import json import numpy as np #from pyasn1.compat.octets import null #import simplejson #path = "input/obs_det.txt_stats_tool_out.bin_11.jpg_000010.txt" #path="input/obs_det.txt_stats_tool_out.bin_12.jpg_000011.txt" import glob path="txt_1838/" save_path = "json_1838/" #path_txt=path+"obs_det.txt_stats_tool_out.*.txt" path_txt=path+"gen_json.txt_stats_tool_out.bin_*.txt" class_names = {0: "bicycle", 1: "traffic_cone", 2: "parking_lock", 3: "carton", 4: "seat"} for i in glob.glob(path_txt): print(i) filename=i.split("/")[1].split(".")[2].split("bin_")[1] print("filename:",filename) with open(i, "r", encoding='utf-8') as f: # 打开文本 datas = f.readlines() # 读取文本 shapes = {} js = {} js['version'] = "4.5.13" js['flags'] = {} js['shapes'] = [] filename = filename js['imagePath'] = filename + ".jpg" # data['imageData'] = "null" js['imageData'] = None jj=[] js['imageHeight'] = 960 js['imageWidth'] = 1280 for data in datas: #for data in range(len(datas)): print("data:", data) data = data.split(" ") label_id = int(data[4]) class_names[label_id] minx1 = min(float(data[9]), float(data[7])) maxx1 = max(float(data[9]), float(data[7])) miny1 = min(float(data[8]), float(data[10])) maxy1 = max(float(data[8]), float(data[10])) print("lab:", label_id) print("minx1:", type(maxx1)) print("minx1:", minx1, "maxx1:", maxx1, "miny1:", miny1, "maxy1:", maxy1) shapes['points'] = [] shapes['group_id'] = None shapes['shape_type'] = "rectangle" shapes['flags'] = {} shapes['label'] = class_names[label_id] # 重写label shapes['points'].append([minx1, miny1]) shapes['points'].append([maxx1, maxy1]) jj.append(shapes) js["shapes"]=jj article = json.dumps(js,indent=2) print("type:", type(article)) with open(os.path.join(save_path, filename+'.json'), "w", encoding='utf-8') as f1: f1.write(article)