import json
import os
def convert(img_size, box):
x1 = box[0]
y1 = box[1]
x2 = box[2]
y2 = box[3]
center_x = (x1 + x2) * 0.5 / img_size[0]
center_y = (y1 + y2) * 0.5 / img_size[1]
w = abs((x2 - x1)) * 1.0 / img_size[0]
h = abs((y2 - y1)) * 1.0 / img_size[1]
return (center_x, center_y, w, h)
def decode_json(jsonfloder_path, json_name):
txt_name = 'fish/labels/val/' + json_name[0:-5] + '.txt'
# txt保存位置
txt_file = open(txt_name, 'w') # te files
json_path = os.path.join(json_folder_path, json_name)
data = json.load(open(json_path, 'r'))
img_w = data['imageWidth']
img_h = data['imageHeight']
for i in data['shapes']:
if (i['shape_type'] == 'rectangle'): # 仅适用矩形框标注
x1 = float(i['points'][0][0])
y1 = float(i['points'][0][1])
x2 = float(i['points'][1][0])
y2 = float(i['points'][1][1])
if x1 < 0 or x2 < 0 or y1 < 0 or y2 < 0:
continue
else:
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
if i['label'] == "HongYanBaiHuaChang":
txt_file.write("0 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "EQueShan":
txt_file.write("1 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "JuGuSheYu":
txt_file.write("2 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "YinLongYu":
txt_file.write("3 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "HongWeiNian":
txt_file.write("4 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "HongWeiHuangGuan":
txt_file.write("5 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "ZhenZhuHongYu":
txt_file.write("6 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "FeiFeng":
txt_file.write("7 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "SiZuLu":
txt_file.write("8 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "JinFengYu":
txt_file.write("9 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "YinFengYu":
txt_file.write("10 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "LanDaoDiao":
txt_file.write("11 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "HuangDaoDiao":
txt_file.write("12 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "GongZiXiaoChou":
txt_file.write("13 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "HongXiaoChou":
txt_file.write("14 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "RenZiDie":
txt_file.write("15 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "YueGuangDie":
txt_file.write("16 " + " ".join([str(a) for a in bbox]) + '\n')
elif i['label'] == "WangWenDie":
txt_file.write("17 " + " ".join([str(a) for a in bbox]) + '\n')
else:
txt_file.write("18 " + " ".join([str(a) for a in bbox]) + '\n')
if __name__ == "__main__":
json_folder_path = 'fish/annotations/val' # json文件夹路径
json_names = os.listdir(json_folder_path) # file name
for json_name in json_names: # output all files
if json_name[-5:] == '.json': # just work for json files
decode_json(json_folder_path, json_name)
需要改的地方
将下方txt_name里的路径改为自己想要保存txt文件路径
将下方json_folder_path改为需要转换的json文件夹路径
根据自己的标签更改下方内容,如我的只有一类标签‘Citrus’,那么txt第一列全是 0 。
如果有两类标签,增加一次elif语句,以此类推,下方为增加‘Apple’后的样式