import json
import os
def convert(img_size, box):
dw = 1. / (img_size[0])
dh = 1. / (img_size[1])
x = (box[0] + box[2]) / 2.0 - 1
y = (box[1] + box[3]) / 2.0 - 1
w = abs(box[2] - box[0])
h = abs(box[3] - box[1])
x = x * dw
w = w * dw
y = y * dh
h = h * dh
# x1 = box[0]
# y1 = box[1]
# x2 = box[2]
# y2 = box[3]
print(x,y,w,h)
return (x,y,w,h)
def decode_json(json_floder_path, json_name):
base_path = '/home/bavon/datasets/city_manager/json'
relative_path = os.path.relpath(json_floder_path, base_path)
txt_name = '/home/bavon/datasets/city_manager/text/'+relative_path+'/' + json_name[0:-5] + '.txt'
print(txt_name)
os.makedirs(os.path.dirname(txt_name), exist_ok=True)
txt_file = open(txt_name, 'w')
json_path = os.path.join(json_floder_path, json_name)
print(json_path)
data = json.load(open(json_path, 'r', encoding='utf-8'))
img_w = data['imageWidth']
img_h = data['imageHeight']
for i in data['shapes']:
if (i['shape_type'] == 'rectangle' and i['label'] == '1'):
x1 = float((i['points'][0][0])) #/ img_w
y1 = float((i['points'][0][1])) #/ img_h
x2 = float((i['points'][1][0])) #/ img_w
y2 = float((i['points'][1][1])) #/ img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
txt_file.write('1' + " " + " ".join([str(a) for a in bbox]) + '\n')
#100
if (i['shape_type'] == 'rectangle' and i['label'] == 'damaged'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#101
if (i['shape_type'] == 'rectangle' and i['label'] == 'water'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#102
if (i['shape_type'] == 'rectangle' and i['label'] == 'sewage'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#103
if (i['shape_type'] == 'rectangle' and i['label'] == 'rainwater'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#104
if (i['shape_type'] == 'rectangle' and i['label'] == 'electric'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#105
if (i['shape_type'] == 'rectangle' and i['label'] == 'streetlamp'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#106
if (i['shape_type'] == 'rectangle' and i['label'] == 'communication'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#107
if (i['shape_type'] == 'rectangle' and i['label'] == 'television'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#108
if (i['shape_type'] == 'rectangle' and i['label'] == 'fuelgas'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#109
if (i['shape_type'] == 'rectangle' and i['label'] == 'publiccover'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#110
if (i['shape_type'] == 'rectangle' and i['label'] == 'noman'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
labelorder = i['label'] + 100
txt_file.write('{labelorder}' + " " + " ".join([str(a) for a in bbox]) + '\n')
#111
if (i['shape_type'] == 'rectangle' and i['label'] == 'wgcs'):
x1 = float((i['points'][0][0])) / img_w
y1 = float((i['points'][0][1])) / img_h
x2 = float((i['points'][1][0])) / img_w
y2 = float((i['points'][1][1])) / img_h
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
txt_file.write('111' + " " + " ".join([str(a) for a in bbox]) + '\n')
# esle:
# xxx
# label = i['label'] + 100
# txt_file.write(label + " " + " ".join([str(a) for a in bbox]) + '\n')
if __name__ == "__main__":
# json_floder_path = '/home/bavon/datasets/city_manager/json'
# json_names = os.listdir(json_floder_path)
# for json_name in json_names:
# decode_json(json_floder_path, json_name)
json_folder_path = '/home/bavon/datasets/city_manager/json'
for root, dirs, json_names in os.walk(json_folder_path):
for json_name in json_names:
if json_name.endswith('.json'):
# print(json_name)
# print(root)
# print(os.path.join(root,json_name))
decode_json(root,json_name)
json2txt将json格式转成txt
最新推荐文章于 2024-07-31 16:47:11 发布