YOLO格式标注数据转COCO标注数据

这里仅仅考虑person类别,如果考虑其他类别,则需要增加类别信息,稍作调整即可。

import json
import os
import imagesize
import copy


def txt_to_json(img_dir,annotation_dir,json_path,img_format='.jpg',annotation_format='.txt'):
    # json 文件主要两项内容
    json_dict = dict()
    annotations = list()
    images = list()
    categories = list()
    # 一个标签和一张图
    one_annotation = dict()
    one_image = dict()
    annotation_bbox_id = 0
    for file in os.listdir(annotation_dir):
        if file.endswith(annotation_format):
            # 读取图片信息:长宽,整合到one_image当中
            one_image['file_name'] = file.split('.')[0]+img_format
            one_image['id'] = file.split('.')[0]
            one_image['width'],one_image['height'] = imagesize.get(os.path.join(img_dir,one_image['file_name']))
            # 读取txt文件的内容,for循环整合到one_annotation当中
            with open(os.path.join(annotation_dir,file), 'r') as f:
                for line in f.readlines():
                    line = line.strip('\n')  # 去掉每一行中的换行符
                    [categories_id,x,y,w,h] = line.split(' ')
                    w = one_image['width']*float(w)
                    h = one_image['height']*float(h)
                    x_min = one_image['width']*float(x) - w/2.0
                    y_min = one_image['height']*float(y) - h/2.0
                    one_annotation['segmentation'] = []
                    one_annotation['area'] = w*h
                    one_annotation['iscrowd'] = 0
                    one_annotation['image_id'] = one_image['id']
                    one_annotation["bbox"] = [x_min,y_min,w,h]
                    one_annotation["category_id"] = 1
                    one_annotation["id"] = annotation_bbox_id                   # 这里的id就是一个编号,每一个人的编号都不相同
                    annotation_bbox_id = annotation_bbox_id + 1
                    annotations.append(copy.deepcopy(one_annotation))
            images.append(copy.deepcopy(one_image))
    category = dict()
    category['supercategory'] = 'person'
    category['person'] = 'person'
    category['id'] = 1
    categories.append(category)
    json_dict['annotations'] = annotations
    json_dict['images'] = images
    json_dict['categories'] = categories
    # 将获取的xml内容写入到json文件中
    with open(json_path, 'w') as f:
        f.write(json.dumps(json_dict, indent=1, separators=(',', ':')))


if __name__ == '__main__':
    img_dir = './train/'
    annotation_dir = './train/'
    json_path = './annotations/train.json'
    txt_to_json(img_dir,annotation_dir,json_path)
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值