import json,os
import numpy as np
def labelme2coco(path):
# path='./json'
files=[]
for file in os.listdir(path):
if file[-5:]=='.json':
files.append(file)
# break
print(json.load(open(path+files[0])))
via_region_data={}
for file in files:
one_json=json.load(open(path+file))
one_image={}
one_image['filename']=file.split('.')[0]+'.jpg'
shape=one_json['shapes']
regions={}
for i in range(len(shape)):
points=np.array(shape[i]['points'])
all_points_x=points[:,0].tolist()
all_points_y=points[:,1].tolist()
regions[str(i)]={}
regions[str(i)]['region_attributes']={}
regions[str(i)]['shape_attributes']={}
regions[str(i)]['shape_attributes']['all_points_x'] = all_points_x
regions[str(i)]['shape_attributes']['all_points_y'] = all_points_y
regions[str(i)]['shape_attributes']['name'] = shape[i]['label']
one_image['regions']=regions
one_image['size']=0
via_region_data[file]=one_image
with open('via_region_data.json','w') as f:
json.dump(via_region_data,f,sort_keys=False,ensure_ascii=True)
if __name__ == '__main__':
labelme2coco('./json/')
matterport/MaskRCNN labelme转coco文件
最新推荐文章于 2021-12-06 18:05:43 发布