coco json to txt

import json
from collections import defaultdict

name_box_id = defaultdict(list)
id_name = dict()
f = open("D:/deeplearning/datasets/coco/annotations/instances_train2017.json", encoding='utf-8')
data = json.load(f)

annotations = data['annotations']
for ant in annotations:
    id = ant['image_id']
    name = 'D:/deeplearning/datasets/coco/train2017/%012d.jpg' % id
    cat = ant['category_id']

    if cat >= 1 and cat <= 11:
        cat = cat - 1
    elif cat >= 13 and cat <= 25:
        cat = cat - 2
    elif cat >= 27 and cat <= 28:
        cat = cat - 3
    elif cat >= 31 and cat <= 44:
        cat = cat - 5
    elif cat >= 46 and cat <= 65:
        cat = cat - 6
    elif cat == 67:
        cat = cat - 7
    elif cat == 70:
        cat = cat - 9
    elif cat >= 72 and cat <= 82:
        cat = cat - 10
    elif cat >= 84 and cat <= 90:
        cat = cat - 11

    name_box_id[name].append([ant['bbox'], cat])

f = open('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()
import json
import cv2
from collections import defaultdict

name_box_id = defaultdict(list)
id_name = dict()
f = open("D:/deeplearning/datasets/coco/annotations/instances_train2017.json", encoding='utf-8')
data = json.load(f)

annotations = data['annotations']
for ant in annotations:
    id = ant['image_id']
    name = 'D:/deeplearning/datasets/coco/train2017/%012d.jpg' % id
    cat = ant['category_id']

    if cat >= 1 and cat <= 11:
        cat = cat - 1
    elif cat >= 13 and cat <= 25:
        cat = cat - 2
    elif cat >= 27 and cat <= 28:
        cat = cat - 3
    elif cat >= 31 and cat <= 44:
        cat = cat - 5
    elif cat >= 46 and cat <= 65:
        cat = cat - 6
    elif cat == 67:
        cat = cat - 7
    elif cat == 70:
        cat = cat - 9
    elif cat >= 72 and cat <= 82:
        cat = cat - 10
    elif cat >= 84 and cat <= 90:
        cat = cat - 11

    name_box_id[name].append([ant['bbox'], cat])

for key in name_box_id.keys():
    img = cv2.imread(key)
    H, W, D = img.shape
    box_infos = name_box_id[key]
    imgjpg = key.split('/')[-1]
    f = open('labels/{}.txt'.format(imgjpg[:len(imgjpg) - 4]), 'w')
    for info in box_infos:
        x_min = info[0][0]
        y_min = info[0][1]
        x_max = x_min + info[0][2]
        y_max = y_min + info[0][3]
        width = float((x_max - x_min)/W)
        height = float((y_max - y_min)/H)
        x_center = float((x_max + x_min)/(2*W))
        y_center = float((y_max + y_min)/(2*H))
        # Each row is class x_center y_center width height format.
        box_info = "{} {:.6f} {:.6f} {:.6f} {:.6f}\n".format(int(info[1]), x_center, y_center, width, height)
        f.write(box_info)
        print(box_info)
    f.close()

# 2020-10-02 guangjinzheng

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值