背景
因项目中使用labelme工具标注可控性比较强,效果可能会更好,就使用了labelme,但是生成出来的是json文件,因为yolov5使用的是txt文件,所以进行了处理
具体代码
import os
import numpy as np
import json
def json2txt(path_json, path_txt):
with open(path_json, 'r', encoding='gb18030') as path_json:
jsonx = json.load(path_json)
with open(path_txt, 'w+') as ftxt:
for shape in jsonx['shapes']:
xy = np.array(shape['points'])
label = str(shape['label'])
if label == 'QB' :
label = '0'
elif label =='MAM':
label = '1'
else:
label = '2'
label += ' '
strxy = ''
for m, n in xy:
strxy += str(m) + ' ' + str(n) + ' '
label += strxy
ftxt.writelines(label + "\n")
dir_json = '/home/hfg/Desktop/image/ng_labelme/json/'
dir_txt = '/home/hfg/Desktop/image/ng_labelme/txt/'
if not os.path.exists(dir_txt):
os.makedirs(dir_txt)
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_txt = dir_txt + json_name.replace('.json', '.txt')
print(path_json, path_txt)
json2txt(path_json, path_txt)