COCO数据集格式(详解)及COCO标注可视化。json转
COCO等代码
coco数据集JSON⽂件格式分为⼀下⼏个字段。
{
"info": info, # dict
"licenses": [license], # list ,内部是dict
"images": [image], # list ,内部是dict
"annotations": [annotation], # list ,内部是dict
"categories": # list ,内部是dict
}
注意 :对于深度学习训练,实际有⽤的字段,只有以下三个字段。
"images": [image], # list ,内部是dict
"annotations": [annotation], # list ,内部是dict
"categories": # list ,内部是dict
info字段内的内容
"info":{#数据信息描述
"description":"COCO 2017 Dataset",# 数据描述
"url":"*****.org",#下载地址
"version":"1.0",#版本
"year":"2017",#年份
"contributor":"COCO Consortium",#提供者
"data_created":"2017/09/01",#创建⽇期}
这个字段可以忽略,可以为空。
licenses字段
"licenses":[
{
'url': '/licenses/by-nc-sa/2.0/',
'id': 1,
'name': 'Attribution-NonCommercial-ShareAlike License'
}
....
....
]
这个字段可以忽略,可以为空。
images
"images": [
{
"license":4 #可以忽略
"file_name":000.jpg #可以忽略
"coco_url":"****" #可以忽略
"id": 1,
"file_name": "000.tif",
"width": 48.0,
"height": 112.0
"date_captured":"2022-02-02 17:02:02" #可以忽略 "flickl_url":"****" #可以忽略
}
...
...
]
annotations
这个字段⾥⾯都是有⽤信息
categories
这个⾥⾯也是有⽤的信息
将⼀个⼤的json⽂件⽣成只有⼀张图⽚的json:
from __future__ import print_function
import COCO
import os, sys, zipfile
quest
import shutil
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
import json
json_file='/media/fire/d/share_data/datasets/coco/annotations/person_keypoints_val2017.json' #
# Object Instance 类型的标注
# person_keypoints_val2017.json
# Object Keypoint 类型的标注格式
# captions_val2017.json
# Image Caption的标注格式
data=json.load(open(json_file,'r'))
data_2={}
data_2['info']=data['info']
data_2['licenses']=data['licenses']
data_2['images']=[data['images'][0]] # 只提取第⼀张图⽚
data_2['categories']=data['categories']
annotation=[] # 通过imgID 到其所有对象
imgID=data_2['images'][0]['id']
for ann in data['annotations']:
if ann['image_id']==imgID:
annotation.append(ann)
data_2['annotations']=annotation # 保存到新的JSON⽂件,便于查看数据特点
json.dump(data_2,open('/media/fire/d/share_data/datasets/coco/annotations/test_person_keypoints_val2017.json','w'),indent=4) # indent=4 更加美观显⽰⼆、COCO数据集可视化
from __future__ import print_function
import COCO
import os, sys, zipfile
quest
import shutil
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
annFile='/media/fire/d/share_data/datasets/coco/annotations/person_keypoints_val2017.json' coco=COCO(annFile) # display COCO categories and supercategories
cats = coco.CatIds())
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms)))
nms = set([cat['supercategory'] for cat in cats])
print('COCO supercategories: \n{}'.format(' '.join(nms)))
# imgIds = ImgIds(imgIds = [324158])
imgIds = ImgIds()
img = coco.loadImgs(imgIds[0])[0]
dataDir = '/media/fire/d/share_data/datasets/coco'
dataType = 'val2017'
I = io.imread('%s/%s/%s'%(dataDir,dataType,img['file_name']))
#plt.axis('off')
plt.imshow(I)
plt.show()
显⽰图⽚
加载肢体关键点:
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 = AnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
print(anns)
coco.showAnns(anns)
plt.imshow(I); plt.axis('off'); plt.show()
加载instances mask:
coco = COCO("/media/fire/d/share_data/datasets/coco/annotations/instances_val2017.json")
img_ids = ImgIds()
print(len(img_ids))
cat_ids = []
for ann in coco.dataset["annotations"]:
if ann["image_id"] == img_ids[0]:
cat_ids.append(ann["category_id"])
ann_ids = AnnIds(imgIds=img_ids[0], catIds = cat_ids)
ann_ids2 = AnnIds(imgIds=img_ids[0], catIds = cat_ids)
plt.imshow(I)
print(ann_ids)
print(ann_ids2)
anns = coco.loadAnns(ann_ids)
coco.showAnns(anns)代码转换
plt.imshow(I)
plt.show()
图像效果
三、不同标注数据转换到COCO格式
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论