栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

coco数据集json处理

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

coco数据集json处理

1 coco数据集json转成基于每个图像的

from __future__ import print_function
import skimage.io as io
import json
from pycocotools.coco import COCO
json_file = 'C:/Users/DELL/Desktop/annotations/instances_val2017.json'
data = json.load(open(json_file, 'r'))
data_temp = {}
data_temp['info'] = data['info']
data_temp['licenses'] = data['licenses']
#print(len(data['images']))
for i in range(len(data['images'])):
    data_temp['images'] = [data['images'][i]]
    data_temp['categories'] = data['categories']
    annotation = []
    imgID = data_temp['images'][0]['id']
    coco = COCO(json_file)
    img = coco.loadImgs([imgID])
    # load and display image
    I = io.imread('C:/Users/DELL/Desktop/val2017/%s' % img[0]['file_name'])
    for ann in data['annotations']:
        if ann['image_id'] == imgID:
            annotation.append(ann)
    data_temp['annotations'] = annotation
    # 保存到新的json
    json.dump(data_temp, open('C:/Users/DELL/Desktop/val2017json/{}.json'.format(str(img[0]['file_name']).split('.')[0]),'w'), indent=4)

2 可视化处理后的一个文件

from __future__ import print_function
import matplotlib.pyplot as plt
from pycocotools.coco import COCO
import skimage.io as io
annFile = 'C:/Users/DELL/Desktop/val2017json/000000022371.json'  # json文件路径
coco = COCO(annFile)
cats = coco.loadCats(coco.getCatIds())
nms = [cat['name'] for cat in cats]
nms = set([cat['supercategory'] for cat in cats])
imgIds = coco.getImgIds()
img = coco.loadImgs(imgIds[0])[0]
dataType = 'C:/Users/DELL/Desktop/val2017'
I = io.imread('%s/%s' % (dataType, img['file_name']))
catIds = []
for ann in coco.dataset['annotations']:
    if ann['image_id'] == imgIds[0]:
        catIds.append(ann['category_id'])
plt.imshow(I);
plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
plt.show()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/650104.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号