label.json
{
"shapes": [
{
"line_color": null,
"points": [
[
717,
327
],
[
1250,
389
],
[
1080,
789
],
[
598,
660
]
],
"fill_color": null,
"label": "123"
}
],
"lineColor": [
0,
255,
0,
128
],
"imagePath": "..\\Pictures\\xubuntu.png",
"flags": {},
"fillColor": [
255,
0,
0,
128
],
"imageData": "iVBO"
}
DOTA labelTxt format
json2txt.py
import json
import os
from glob import glob
# convert labelme json to DOTA txt format
def custombasename(fullname):
return os.path.basename(os.path.splitext(fullname)[0])
IN_PATH = 'C:/Users/Shuai/Desktop/bbb/labelJson'
OUT_PATH = 'C:/Users/Shuai/Desktop/bbb/labelTxt'
file_list = glob(IN_PATH + '/*.json')
for i in range(len(file_list)):
with open(file_list[i]) as f:
label_str = f.read()
label_dict = json.loads(label_str) # json文件读入dict
# 输出 txt 文件的路径
out_file = OUT_PATH + '/' + custombasename(file_list[i]) + '.txt'
# 写入 poly 四点坐标 和 label
fout = open(out_file, 'w')
out_str = ''
for shape_dict in label_dict['shapes']:
points = shape_dict['points']
for p in points:
out_str += (str(p[0]) + ' ' + str(p[1]) + ' ')
out_str += shape_dict['label'] + ' 0\n'
fout.write(out_str)
fout.close()
print('%d/%d' % (i + 1, len(file_list)))
网友评论