读取coco的单张图的GT

COCO的格式不利于查看,因此下面代码是读取只提取单张图的标注信息:

# -*- coding:utf-8 -*-

from __future__ import print_function
from pycocotools.coco import COCO
import os, sys, zipfile
import urllib.request
import shutil
import numpy as np
# import skimage.io as io
import matplotlib.pyplot as plt
import pylab
import json

json_file='person_keypoints_train2017.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'][27]] # 只提取第一张图片
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('./new_instances_val2017.json','w'),indent=4) # indent=4 更加美观显示

 结果:

{
    "info": {
        "description": "COCO 2017 Dataset",
        "url": "http://cocodataset.org",
        "version": "1.0",
        "year": 2017,
        "contributor": "COCO Consortium",
        "date_created": "2017/09/01"
    },
    "licenses": [
        {
            "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
            "id": 1,
            "name": "Attribution-NonCommercial-ShareAlike License"
        },
        {
            "url": "http://creativecommons.org/licenses/by-nc/2.0/",
            "id": 2,
            "name": "Attribution-NonCommercial License"
        },
        {
            "url": "http://creativecommons.org/licenses/by-nc-nd/2.0/",
            "id": 3,
            "name": "Attribution-NonCommercial-NoDerivs License"
        },
        {
            "url": "http://creativecommons.org/licenses/by/2.0/",
            "id": 4,
            "name": "Attribution License"
        },
        {
            "url": "http://creativecommons.org/licenses/by-sa/2.0/",
            "id": 5,
            "name": "Attribution-ShareAlike License"
        },
        {
            "url": "http://creativecommons.org/licenses/by-nd/2.0/",
            "id": 6,
            "name": "Attribution-NoDerivs License"
        },
        {
            "url": "http://flickr.com/commons/usage/",
            "id": 7,
            "name": "No known copyright restrictions"
        },
        {
            "url": "http://www.usa.gov/copyright.shtml",
            "id": 8,
            "name": "United States Government Work"
        }
    ],
    "images": [
        {
            "license": 2,
            "file_name": "000000368402.jpg",
            "coco_url": "http://images.cocodataset.org/train2017/000000368402.jpg",
            "height": 427,
            "width": 640,
            "date_captured": "2013-11-14 22:22:42",
            "flickr_url": "http://farm8.staticflickr.com/7234/6912194164_60704b897c_z.jpg",
            "id": 368402
        }
    ],
    "categories": [
        {
            "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
                ]
            ]
        }
    ],
    "annotations": [
        {
            "segmentation": [
                [
                    211.96,
                    346.66,
                    207.4,
                    265.33,
                    220.2,
                    253.45,
                    202.83,
                    238.82,
                    203.74,
                    183.99,
                    225.68,
                    162.97,
                    220.2,
                    137.39,
                    230.24,
                    114.54,
                    253.09,
                    100.83,
                    273.19,
                    107.23,
                    283.25,
                    134.64,
                    287.82,
                    153.84,
                    284.16,
                    171.19,
                    299.7,
                    178.51,
                    299.7,
                    219.63,
                    246.7,
                    221.46,
                    247.61,
                    243.39,
                    289.65,
                    248.88,
                    290.56,
                    272.64,
                    214.71,
                    349.4
                ]
            ],
            "num_keypoints": 15,
            "area": 14801.0467,
            "iscrowd": 0,
            "keypoints": [
                255,
                136,
                2,
                263,
                131,
                2,
                249,
                132,
                2,
                272,
                138,
                2,
                235,
                138,
                2,
                286,
                187,
                2,
                219,
                181,
                2,
                294,
                234,
                1,
                213,
                242,
                2,
                317,
                227,
                1,
                223,
                220,
                2,
                279,
                278,
                2,
                229,
                280,
                2,
                276,
                358,
                1,
                243,
                366,
                1,
                0,
                0,
                0,
                0,
                0,
                0
            ],
            "image_id": 368402,
            "bbox": [
                202.83,
                100.83,
                96.87,
                248.57
            ],
            "category_id": 1,
            "id": 476223
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/qq_33547191/article/details/92806591