解决问题

1.将coco数据集中,annotations的json文件,读取,进行转为ID保持一致的txt文件。
2.解决COCO数据集中,类别不连续的问题

方法

from __future__ import print_function
import os, sys, zipfile
import json
 
class_num = 0
def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = box[0] + box[2] / 2.0
    y = box[1] + box[3] / 2.0
    w = box[2]
    h = box[3]
 
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)
 
 
json_file = '/Data/coco/annotations/instances_val2017.json'  # # Object Instance 类型的标注
 
data = json.load(open(json_file, 'r'))
ana_txt_save_path = "/Data/coco/test2017"  # 保存的路径
if not os.path.exists(ana_txt_save_path):
    os.makedirs(ana_txt_save_path)

index = 0
cat_id_map = {}
for img in data['images']:
    filename = img["file_name"]
    img_width = img["width"]
    img_height = img["height"]
    img_id = img["id"]
    ana_txt_name = filename.split(".")[0] + ".txt"  # 对应的txt名字,与jpg一致
    index +=1
    print(str(index) +'   '+str(ana_txt_name))
    f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')# 保存的文件



    for ann in data['annotations']:
        if ann['image_id'] == img_id:
            if ann['category_id'] not in cat_id_map:
                cat_id_map[ann['category_id']] = class_num
                class_num += 1
            box = convert((img_width, img_height), ann["bbox"])
            f_txt.write("%s %s %s %s %s\n" % (cat_id_map[ann['category_id']], box[0], box[1], box[2], box[3]))
        
    f_txt.close()
fileObject = open('/Data/coco/val_cat_id_map.txt', 'w') # 类别进行重新映射
for cat_id in cat_id_map:  
    fileObject.write(str(cat_id))
    fileObject.write('\n')
fileObject.close()
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐