【COCO数据集】COCO API入门 超详细注解

这篇博客全面介绍了COCO API的使用,包括数据集配置、训练集和验证集的构建、实例标注、关键点标注以及语义标注的加载和显示。通过示例代码展示了如何获取和显示COCO数据集中图片的各种标注信息。
摘要由CSDN通过智能技术生成

A Glance through COCO

Presented By R.G.

关于COCO

COCO的官网:https://cocodataset.org/
COCO API : https://github.com/cocodataset/cocoapi

以下是 COCO API 的 README

COCO is a large image dataset designed for object detection, segmentation, person keypoints detection, stuff segmentation, and caption generation. This package provides Matlab, Python, and Lua APIs that assists in loading, parsing, and visualizing the annotations in COCO. Please visit http://cocodataset.org/ for more information on COCO, including for the data, paper, and tutorials. The exact format of the annotations is also described on the COCO website. The Matlab and Python APIs are complete, the Lua API provides only basic functionality.

In addition to this API, please download both the COCO images and annotations in order to run the demos and use the API. Both are available on the project website.
-Please download, unzip, and place the images in: coco/images/
-Please download and place the annotations in: coco/annotations/
For substantially more details on the API please see http://cocodataset.org/#download.

After downloading the images and annotations, run the Matlab, Python, or Lua demos for example usage.

To install:

  • For Matlab, add coco/MatlabApi to the Matlab path (OSX/Linux binaries provided)
  • For Python, run “make” under coco/PythonAPI
  • For Lua, run “luarocks make LuaAPI/rocks/coco-scm-1.rockspec” under coco/

关于COCO API的安装

参考我的另一篇文章:
【COCO数据集】python使用pip安装COCO API

COCO数据集配置

配置COCO数据集的路径

import sys,os
# 配置COCO数据集的路径
COCOPATH = os.path.join(os.getcwd(), 'COCO') # 这里配置你COCO数据集的所在路径,我是放在当前文件夹路径内
COCOPATH
'/Users/rgmax/Desktop/COCO_ABC/COCO'

查看路径(文件夹)下内容,可以看到由于是mac的缘故,多了一个’.DS_Store’

os.listdir(COCOPATH) 
['image_info_test2017',
 '.DS_Store',
 'val2017',
 'panoptic_annotations_trainval2017',
 'test2017',
 'stuff_annotations_trainval2017',
 'unlabeled2017',
 'annotations_trainval2017',
 'train2017',
 'image_info_unlabeled2017']

剔除mac自带的.DS_Store文件夹,并将COCO数据集所含的所有文件夹路径保存在COCODIRS字典中

COCODIRS = os.listdir(COCOPATH)
try: # 剔除mac自带的.DS_Store文件夹
    COCODIRS.remove('.DS_Store')
except:
    pass
# COCODIRS = [os.path.join(COCOPATH, d) for d in COCODIRS] # 生成完整文件夹路径
COCODIRS = {
     # 改用字典比较好访问
    d : os.path.join(COCOPATH, d) for d in COCODIRS
}
COCODIRS
{'image_info_test2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/image_info_test2017',
 'val2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/val2017',
 'panoptic_annotations_trainval2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/panoptic_annotations_trainval2017',
 'test2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/test2017',
 'stuff_annotations_trainval2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/stuff_annotations_trainval2017',
 'unlabeled2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/unlabeled2017',
 'annotations_trainval2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/annotations_trainval2017',
 'train2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/train2017',
 'image_info_unlabeled2017': '/Users/rgmax/Desktop/COCO_ABC/COCO/image_info_unlabeled2017'}

构建训练集对象train_set

# 构建训练集
train_set = [os.path.join(COCODIRS['train2017'],p) for p in os.listdir(COCODIRS['train2017'])]
train_set[:5] # 查看部分训练集(图片)
['/Users/rgmax/Desktop/COCO_ABC/COCO/train2017/000000109622.jpg',
 '/Users/rgmax/Desktop/COCO_ABC/COCO/train2017/000000160694.jpg',
 '/Users/rgmax/Desktop/COCO_ABC/COCO/train2017/000000308590.jpg',
 '/Users/rgmax/Desktop/COCO_ABC/COCO/train2017/000000327573.jpg',
 '/Users/rgmax/Desktop/COCO_ABC/COCO/train2017/000000062929.jpg']

构建验证集对象val_set

val_set = [os.path.join(COCODIRS['val2017'],p) for p in os.listdir(COCODIRS['val2017'])]
val_set[:5]
['/Users/rgmax/Desktop/COCO_ABC/COCO/val2017/000000182611.jpg',
 '/Users/rgmax/Desktop/COCO_ABC/COCO/val2017/000000335177.jpg',
 '/Users/rgmax/Desktop/C
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值