1.下面这个是我用via标注生成的 一系列图片的 json文件集合,frames0_0.jpg是第一张图片的名字
2.下面是我要生成的labelme的数据集json格式
转换代码如下:
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 4 21:35:04 2020
@author: wfb
"""
import json
import numpy as np
path='C:\\Users\\Administrator\\Desktop\\D11\\via_export_json.json'
with open(path,'r',encoding='utf8')as fp: #打开json文件
json_data = json.load(fp)
for count,L in enumerate(json_data):
print(json_data[L]['filename'])
filename=json_data[L]['filename'][0:-4]
#由于via标注的时候是所有图片的标签集中在一个json文件,我的目标是一个图片生成一个
#所以要把每个图片的名字保存下来
region=json_data[L]['regions']
test=[]
for K in region:
reg=K['shape_attributes']
point_X=reg['all_points_x']
point_Y=reg['all_points_y']
resultpos=list(zip(point_X,point_Y))
resultpos=np.array(resultpos)
resultpos=resultpos.tolist()
#为了跟labelme中的坐标相同, 对xy坐标先合成,转为numpy再转会List
dict1={"label":"car","polygon":resultpos}
test.append(dict1)
print('test=',test)
one = {"imgHeight": 1024, "imgWidth": 2048,"objects":test}
with open('res/'+filename+'.json','w',encoding='utf-8') as f:
f.write(json.dumps(one,ensure_ascii=False,indent=1)) #最后保持