txt 转 coco json格式

txt 转 coco json格式

# -*- coding: UTF-8 -*-
import cv2
import json
import sys
 
 
# process bar
def process_bar(count, total, status=''):
    bar_len = 60
    filled_len = int(round(bar_len * count / float(total)))
 
    percents = round(100.0 * count / float(total), 1)
    bar = '=' * filled_len + '-' * (bar_len - filled_len)
 
    sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
    sys.stdout.flush()
 
 
root_path = "/Users/mot/val/"
images, categories, annotations = [], [], []
 
category_dict = {"people": 1}
 
for cat_n in category_dict:
    categories.append({"supercategory": "", "id": category_dict[cat_n], "name": cat_n})
 
# with open("train_clean.txt", "r") as f:
with open('val_clean.txt', 'r') as f:
    img_id = 0
    anno_id_count = 0
    count = 1
    # total = len(f.readlines())
    # print(111,total)
    lines = f.readlines()
    total = len(lines)
    # print(333,lines)
    for line in lines:
        # print(2222,line)
        # process_bar(count, total)
 
        print('{}/{}'.format(count,total))
        count += 1
        line = line.split(' ')
        img_name = line[0]
        bbox_num = int(line[1])
        img_cv2 = cv2.imread(root_path + img_name)
        [height, width, _] = img_cv2.shape
        bbox = line[1:]
        x = bbox[1::5]
        y = bbox[2::5]
        w = bbox[3::5]
        h = bbox[4::5]
        # img_name+=' '
        # fp.write(img_name)
        # for i in range(len(x)):
 
        # images info
        images.append({"file_name": img_name, "height": height, "width": width, "id": img_id})
 
        """
        annotation info:
        id : anno_id_count
        category_id : category_id
        bbox : bbox
        segmentation : [segment]
        area : area
        iscrowd : 0
        image_id : image_id
        """
        category_id = category_dict["people"]
        for i in range(0, len(x)):
            x1, y1, weight, height = int(x[i]), int(y[i]), int(w[i]), int(h[i])
            x2,y2 = x1+weight,y1+height
            # box = '{} {},{},{},{}\n'.format(img_name, x1, y1, x2, y2)
            # x1 = float(line[i * 5 + 3])
            # y1 = float(line[i * 5 + 4])
            # x2 = float(line[i * 5 + 3]) + float(line[i * 5 + 5])
            # y2 = float(line[i * 5 + 4]) + float(line[i * 5 + 6])
            # width = float(line[i * 5 + 5])
            # height = float(line[i * 5 + 6])
 
            bbox = [x1, y1, width, height]
            segment = [x1, y1, x2, y1, x2, y2, x1, y2]
            area = width * height
 
            anno_info = {'id': anno_id_count, 'category_id': category_id, 'bbox': bbox, 'segmentation': [segment],
                         'area': area, 'iscrowd': 0, 'image_id': img_id}
            annotations.append(anno_info)
            anno_id_count += 1
 
        img_id = img_id + 1
 
all_json = {"images": images, "annotations": annotations, "categories": categories}
 
with open("val.json", "w") as outfile:
    json.dump(all_json, outfile)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值