keras-yolov3数据准备 json文件转成txt文件

由于标注文件是用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()

每一行代表一张图  

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值