对labelme生成的文件进行指定的修改
import json
import os
import re
import operator
def file_updata(file):
try:
with open(file,'r') as f:
file_data=f
str_json_book=json.load(file_data)
str_json_book['shapes'][0]['label']='freespace'
str_json_book['shapes'][0]['shape_type']='polygon'
points_0_len = len(str_json_book['shapes'][0]['points'])
points_0_start_x=str_json_book['shapes'][0]['points'][0][0]
points_0_end_x=str_json_book['shapes'][0]['points'][points_0_len-1][0]
height = str_json_book['imageHeight']
width = str_json_book['imageWidth']
if len(str_json_book['shapes'])>1:
print("2")
points_1_len = len(str_json_book['shapes'][1]['points'])
points_1_start_x=str_json_book['shapes'][1]['points'][0][0]
points_1_end_x=str_json_book['shapes'][1]['points'][points_1_len-1][0]
if abs(points_1_start_x-points_0_end_x) < abs(points_1_end_x-points_0_end_x) :
for json_line2 in range(0,points_1_len):
list_temp=str_json_book['shapes'][1]['points'][json_line2]
str_json_book['shapes'][0]['points'].append(list_temp)
else:
for json_line2 in range(0,points_1_len):
list_temp=str_json_book['shapes'][1]['points'][json_line2]
str_json_book['shapes'][0]['points'].insert(0+json_line2,list_temp)
str_json_book['shapes'].pop(1)
list1=[width-1,height-1]
list2=[0,height-1]
list3=[str_json_book['shapes'][0]['points'][points_0_len-1][0],str_json_book['shapes'][0]['points'][points_0_len-1][1]]
if str_json_book['shapes'][0]['points'][points_0_len-1][0] > 600 and operator.eq(list1,list3)==False and operator.eq(list2,list3)==False:
str_json_book['shapes'][0]['points'].append(list1)
str_json_book['shapes'][0]['points'].append(list2)
elif operator.eq(list2,list3)==False and operator.eq(list1,list3)==False:
str_json_book['shapes'][0]['points'].append(list2)
str_json_book['shapes'][0]['points'].append(list1)
except:
print("error main","第:",file)
finally:
file_data = json.dumps(str_json_book,sort_keys=False,indent=3,separators=(',',': '))
f.close()
with open(file,'w') as f:
try:
f.write(file_data)
print(file)
except:
print("write error")
finally:
f.close()
if __name__ == "__main__":
file_num = 1
file_dir=json.dumps(os.listdir('./'),indent=1)
res=re.compile(r'"([0-9].*\.json)"')
for line in re.findall(res,file_dir):
if(os.path.exists(line)):
file_updata(line)