对于’annotations/person_keypoints_val2017.json’文件,共有5000张图片(images),11004条注释(annotations),1条种类信息(categories)。
对于每个标注文件,都会有anns, cats, imgs,imgToAnns, catToImgs 这5类字典dict数据结构需要生成。
对于’annotations/person_keypoints_val2017.json’文件,anns字典有11004个键值对。cats字典有1个键值对。imgs字典有5000个键值对。imgToAnns有字典有1个键值对,其值为长度为11004的list对象,list中的每个元素都是Img_id,对应每条anns的img_id。imgToAnns有2693个键值对,每个键为img_id,每个值为一个存放了1至多个ann标注信息的list。
anns为标注信息,每条标注数据有一个标注Id,anns通过anns[ann['id']] = ann
实现某一条标注ann的id映射到该ann。即每个ann有一个唯一的id。
imgs为图片信息,每条图片数据有一个图片Id,imgs通过imgs[img['id']] = img
实现图片img_id映射到img。即每张图片有一个唯一的id。
cats为种类信息,每条种类数据有一个种类id,cats通过cats[cat['id']] = cat
实现种类cat_id到某条种类数据的映射。即每个种类都是有唯一的id。
catToImgs为种类id到属于该种类的具体图片的映射,因为一个种类可以对应多张图片,即一个类别映射到图片id,所以该映射的默认值为list,通过catToImgs[ann['category_id']].append(ann['image_id'])
实现。即一个种类可以有多张图片。
imgToAnns为图片的id,即img_id到图片对应的标注信息的映射,因为一张图片可能有多个标注,故该映射的默认值为list,通过imgToAnns[ann['image_id']].append(ann)
实现。即一张图片可以有多个标注。
取coco的几个具体标注例子,简单的代码示例如下:
from collections import defaultdict
anns,cats,imgs = {},{},{}
imgToAnns, catToImgs = defaultdict(list), defaultdict(list)
ann ={'segmentation': [[125.12, 539.69, 140.94, 522.43, 100.67, 496.54, 84.85, 469.21, 73.35, 450.52, 104.99, 342.65, 168.27, 290.88, 179.78, 288, 189.84, 286.56, 191.28, 260.67, 202.79, 240.54, 221.48, 237.66, 248.81, 243.42, 257.44, 256.36, 253.12, 262.11, 253.12, 275.06, 299.15, 233.35, 329.35, 207.46, 355.24, 206.02, 363.87, 206.02, 365.3, 210.34, 373.93, 221.84, 363.87, 226.16, 363.87, 237.66, 350.92, 237.66, 332.22, 234.79, 314.97, 249.17, 271.82, 313.89, 253.12, 326.83, 227.24, 352.72, 214.29, 357.03, 212.85, 372.85, 208.54, 395.87, 228.67, 414.56, 245.93, 421.75, 266.07, 424.63, 276.13, 437.57, 266.07, 450.52, 284.76, 464.9, 286.2, 479.28, 291.96, 489.35, 310.65, 512.36, 284.76, 549.75, 244.49, 522.43, 215.73, 546.88, 199.91, 558.38, 204.22, 565.57, 189.84, 568.45, 184.09, 575.64, 172.58, 578.52, 145.26, 567.01, 117.93, 551.19, 133.75, 532.49]], 'num_keypoints': 10, 'area': 47803.27955, 'iscrowd': 0, 'keypoints': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142, 309, 1, 177, 320, 2, 191, 398, 2, 237, 317, 2, 233, 426, 2, 306, 233, 2, 92, 452, 2, 123, 468, 2, 0, 0, 0, 251, 469, 2, 0, 0, 0, 162, 551, 2], 'image_id': 425226, 'bbox': [73.35, 206.02, 300.58, 372.5], 'category_id': 1, 'id': 183126}
img = {'license': 2, 'file_name': '000000015335.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000015335.jpg', 'height': 480, 'width': 640, 'date_captured': '2013-11-25 14:00:10', 'flickr_url': 'http://farm6.staticflickr.com/5533/10257288534_c916fafd78_z.jpg', 'id': 15335}
cat = {'supercategory': 'person', 'id': 1, 'name': 'person', 'keypoints': ['nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle'], 'skeleton': [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]]}
anns[ann['id']] = ann
imgs[img['id']] = img
cats[cat['id']] = cat
imgToAnns[ann['image_id']].append(ann)
catToImgs[ann['category_id']].append(ann['image_id'])
print(anns)
print(imgs)
print(cats)
print(imgToAnns)
print(catToImgs)
以下代码参考cocoapi:
import json
from collections import defaultdict
annotation_file = '../../annotations/instances_val2017.json'
dataset = json.load(open(annotation_file, 'r'))
# print(dataset)
#共有5类映射关系需要保存
anns, cats, imgs = {}, {}, {}
imgToAnns, catToImgs = defaultdict(list), defaultdict(list)
print(dataset.keys())
if 'annotations' in dataset:
# for ann in dataset['annotations']:
#第1条标注
ann = dataset['annotations'][0]
print(ann)
print(type(ann))
for k in ann.keys():
print(k,ann[k])
imgToAnns[ann['image_id']].append(ann) #根据image_id映射到对应的标注
anns[ann['id']] = ann #根据标注id映射到对应的标注
print(imgToAnns)
print(anns)
print(' ')
if 'images' in dataset:
# for img in dataset['images']:
img = dataset['images'][0]
print(img)
print(type(img))
for k in img.keys():
print(k,img[k])
imgs[img['id']] = img #根据image_id映射到对应的图片信息
print(imgs)
print(' ')
if 'categories' in dataset:
# for cat in dataset['categories']:
cat = dataset['categories'][0]
print(cat)
cats[cat['id']] = cat #根据cat_id映射到对应的cat种类
print(cats)
print(' ')
if 'annotations' in dataset and 'categories' in dataset:
# for ann in dataset['annotations']:
ann = dataset['annotations'][0]
for k in ann.keys():
print(k,ann[k])
catToImgs[ann['category_id']].append(ann['image_id']) #根据种类的id映射到对应的图片的id。
print(catToImgs)