标注的json数据转坐标点文本
json格式类似如下:
{"objects":[{"label":"report_phone","polygon":[[194,411],[787,318],[772,363],[217,455]]},{"label":"chaoyuanweifa","polygon":[[550,284],[799,253],[793,321],[546,345]]},{"label":"hezai","polygon":[[181,330],[541,281],[547,359],[197,409]]}],"rotate":0}
两种读取.json方式都可以
#coding:utf-8
import json
import os
import codecs
def json2txt(path_json,path_save_txt):
txt_1 = json.load(codecs.open(path_json, 'r', 'utf-8-sig'))
#txt = json.loads(open(path_json).readline())
with open(path_save_txt,'a') as fw:
a = txt_1['objects']
for obj in a:
polygons = obj['polygon']
for i,pt in enumerate(polygons):
if i != len(polygons)-1:
fw.write(str(pt[0]))
fw.write(',')
fw.write(str(pt[1]))
fw.write(',')
else:
fw.write(str(pt[0]))
fw.write(',')
fw.write(str(pt[1]))
fw.write('\n')
dir_json = "/media/data_2/2019biaozhushuju/20190722_pentu/data/json/"
save_txt_dir = os.path.dirname(os.path.dirname(dir_json)) + '/json2txt/'
if not os.path.exists(save_txt_dir):
os.makedirs(save_txt_dir)
list_json = os.listdir(dir_json)
for cnt,json_name in enumerate(list_json):
print("cnt=%d,name=%s"%(cnt,json_name))
path_json = dir_json + json_name
path_save_txt = save_txt_dir + json_name.replace(".jpg.json",'.txt')
json2txt(path_json,path_save_txt)
生成:
194,411,787,318,772,363,217,455
550,284,799,253,793,321,546,345
181,330,541,281,547,359,197,409
好记性不如烂键盘---点滴、积累、进步!