您当前的位置:首页 > 计算机 > 编程开发 > Python

txt 转 coco json格式

时间:01-16来源:作者:点击数:

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)
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门