由于标注文件是用json文件保存的,yolov3不能直接用来训练,需要进行转换。
import json
from collections import defaultdict
name_box_id = defaultdict(list)
id_name = dict()
f = open(
"./datasets/train_restriction.json",
encoding='utf-8')
data = json.load(f)
annotations = data['annotations']
for ant in annotations:
id = ant['image_id']
name = './datasets/restricted/%d.jpg' % id
cat = ant['category_id']
name_box_id[name].append([ant['bbox'], cat])
#name_box_id[name].append([ant['bbox'], cat])
f = open('./datasets/train.txt', 'w')
for key in name_box_id.keys():
f.write(key)
box_infos = name_box_id[key]
for info in box_infos:
x_min = int(info[0][0])
y_min = int(info[0][1])
x_max = x_min + int(info[0][2])
y_max = y_min + int(info[0][3])
box_info = " %d,%d,%d,%d,%d" % (
x_min, y_min, x_max, y_max, int(info[1]))
f.write(box_info)
f.write('\n')
f.close()
每一行代表一张图