MSCOCO - pycocoDemo 学习版
Reference:
- https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb
- https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py
- https://blog.csdn.net/yeyang911/article/details/78675942
Here is my code. But there is still a problem that when I use the function 'showAnns()' to show the instances' annotations of the image, it cannot show the figure though the code run successfully. If you have any solutions, please leave a message in the comment area. Thanks!
import matplotlib
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
from pycocotools.coco import COCO
pylab.rcParams['figure.figsize'] = (8.0, 10.0) # image pixel # if skimage.io declares before pycocotools.coco, the backend will be chosen as Qt5Agg
# if pycocotools.coco declares before skimage.io, the backend will be chosen as Agg
print(matplotlib.get_backend()) dataDir = r'G:\MSCOCO 2017\annotations_trainval2017'
dataType = r'val2017'
annFile = r'{}\annotations\instances_{}.json'.format(dataDir, dataType) # initialize COCO api for instance annotations
coco = COCO(annotation_file=annFile) # COCO api class that loads COCO annotation file and prepare data structures.
# loading annotations into memory...
# Done (t=0.80s)
# creating index...
# index created! # print(coco.info()) # Print information about the annotation file.
# description: COCO 2017 Dataset
# url: http://cocodataset.org
# version: 1.0
# year: 2017
# contributor: COCO Consortium
# date_created: 2017/09/01
# None # display COCO categories and supercategories
# coco.loadCats(self, ids=[]): Load cats with the specified ids.
cats = coco.loadCats(coco.getCatIds())
# print(cats)
# [{'supercategory': 'person', 'id': 1, 'name': 'person'},
# {'supercategory': 'vehicle', 'id': 2, 'name': 'bicycle'},
# {'supercategory': 'vehicle', 'id': 3, 'name': 'car'},
# {...}, {...}, {...}, ... ...,
# {'supercategory': 'indoor', 'id': 90, 'name': 'toothbrush'}]
nms = [cat['name'] for cat in cats]
# print('COCO categories: \n{}\n'.format(' '.join(nms)))
# person bicycle car motorcycle airplane ... hair drier toothbrush
nms = set([cat['supercategory'] for cat in cats])
# print('COCO supercategories: \n{}'.format(' '.join(nms)))
# vehicle person kitchen electronic appliance indoor accessory animal food furniture outdoor sports # get all images containing given categories, select one at random
catIds = coco.getCatIds(catNms=['person', 'dog', 'skateboard'])
# print(catIds) # [1, 18, 41]
imgIds = coco.getImgIds(catIds=catIds) # param catIds (int array): get images with all given categories
# imgIds = coco.getImgIds(imgIds=[324158]) # param imgIds (int array): get images for given ids
index = np.random.randint(0, len(imgIds))
# print('randomly selected imgId: %d' % index)
img = coco.loadImgs(imgIds[index])[0] # Load images with specified ids.
# print(img)
# if index=2, print:
# {'license': 2, 'file_name': '000000279278.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000279278.jpg',
# 'height': 429, 'width': 640, 'date_captured': '2013-11-15 01:07:24',
# 'flickr_url': 'http://farm7.staticflickr.com/6101/6275412942_f8dc734c3f_z.jpg', 'id': 279278} # load and display image
# i = io.imread('%s/image/%s/%s' % (dataDir, dataType, img['file_name']))
# use url to load image
# print(img['coco_url']) # http://images.cocodataset.org/val2017/000000279278.jpg
i = io.imread(img['coco_url'])
plt.axis('off')
plt.imshow(i)
# print(plt.imshow(i)) # AxesImage(100, 110; 620x770)
plt.show() # load and display instance annotations
plt.imshow(i)
plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns) # initialize COCO api for person keypoints annotations
annFile = '{}/annotations/person_keypoints_{}.json'.format(dataDir, dataType)
coco_kps = COCO(annFile) # load and display keypoints annotations
plt.imshow(i)
plt.axis('off')
ax = plt.gca()
annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco_kps.loadAnns(annIds)
coco_kps.showAnns(anns) # initialize COCO api for caption annotations
annFile = '{}/annotations/captions_{}.json'.format(dataDir, dataType)
coco_caps = COCO(annFile) # load and display caption annotations
annIds = coco_caps.getAnnIds(imgIds=img['id'])
anns = coco_caps.loadAnns(annIds)
coco_caps.showAnns(anns)
plt.imshow(i)
plt.axis('off')
plt.show()
MSCOCO - pycocoDemo 学习版的更多相关文章
- Sql Server 2008开发版(Developer Edition)过期升级企业版(Enterprise Edition)失败后安装学习版(Express Edition)
最近一个多月,甚是悠哉,无事可做.上线的网站系统也没接到客户的反馈,反而觉得无聊之极了.上周五早上,一上QQ,就收到客户发来消息,管理平台无法登陆了.心里一惊,立马开始查找故障原因.翻看了系统日志,提 ...
- 学习版pytest内核测试平台开发万字长文入门篇
前言 2021年,测试平台如雨后春笋般冒了出来,我就是其中一员,写了一款pytest内核测试平台,在公司落地.分享出来后,有同学觉得挺不错,希望能开源,本着"公司代码不要传到网上去,以免引起 ...
- 学习笔记︱Nvidia DIGITS网页版深度学习框架——深度学习版SPSS
DIGITS: Deep Learning GPU Training System1,是由英伟达(NVIDIA)公司开发的第一个交互式深度学习GPU训练系统.目的在于整合现有的Deep Learnin ...
- oracle 11g XE 学习版添加scott用户方法全步骤
安装企业版的orcale是不是太费时费力了?若只是学习用途的话,不妨试试轻便版的XE版本,同样是官网下载的,但是这个安装起来比完整版简便多了. 首先,你得先安装好orcale 11g XE 版本:(这 ...
- oracleXE(oracle学习版)在windows的安装配置
oracleXE其实安装基本就是一路下一步. 在安装前记得先把tomcat打开,不然oracleXE会占用8080端口且安装过程无法更改端口. 如果需要远程连接此oracle XE的话,要关闭这台电脑 ...
- UIAutomator 学习版
1.要写UIAutomator的testcase,首先要用Eclipse创建一个Java Project 需要将Junit 的lib加入到工程里 2.添加android.jar和uiautomator ...
- python3--装饰器高级学习版
__author__ = "Aaron Fan"import time #导入time模块user,passwd = 'alex','abc123' #用户名密码def auth( ...
- Spring事务管理----------整合学习版
作者:学无先后 达者为先 Spring提供了一流的事务管理.在Spring中可以支持声明式事务和编程式事务. 一 spring简介 1 Spring的事务 事务管理在应用程序中起着至关重 ...
- 深度学习菜鸟的信仰地︱Supervessel超能云服务器、深度学习环境全配置
并非广告~实在是太良心了,所以费时间给他们点赞一下~ SuperVessel云平台是IBM中国研究院和中国系统与技术中心基于POWER架构和OpenStack技术共同构建的, 支持开发者远程开发的免费 ...
随机推荐
- 一些有用的社区论坛,wiki网站(持续更新)
1. IBM开发者(IBM developerwork): 这是一个比较全面的网站,上面有关于linux 管理.linux内核设计.存储等各个方面的知识,内容广泛,参考价值很高 http://www. ...
- JS模拟Dictionary
function Map() { this.keys = new Array(); this.data = new Array(); //添加键值对 this.set = function (key, ...
- 仿手机QQ消息小红点动画2
前言 上一篇把动画的实现步骤大致理清,需要确认小尾巴的绘制区域,关键就是确定4个顶点的位置.大家可以根据需要,选择不同的计算方式. 今天,要实现: 文字的添加 尾巴拉长用弧形代替直线 下面是现在的效果 ...
- python 正则匹配手机号
import rephone = str(input('请输入手机号:'))# b = str(12345678912)t = re.compile(r'^1(3\d|4[4-9]|5[0-35-9] ...
- React学习(一)
一. 允许HTML和JavaScript代码混写,使用JSX语法:遇到HTML标签就用HTML规则解析,遇到{}的代码块就用js解析 var names = ['Alice', 'Emily', 'K ...
- 浏览器内多个标签页之间的通信之storage
在一个标签页里面使用 localStorage.setItem(key,value)添加(修改.删除)内容: 在另一个标签页里面监听 storage 事件. 即可得到 localstorge 存储的值 ...
- colemak,你用了吗?
为了输入代码的感觉更好,我学习了colemak键盘布局,这个布局它是在QWERTY的基础上改了10多个键. 开始的三天,感觉非常不好,每按一个键都要思考很长时间,干脆在网上找了个在线打字的网页去练,感 ...
- STM32 HAL库学习系列第4篇 定时器TIM----- 开始定时器与PWM输出配置
基本流程: 1.配置定时器 2.开启定时器 3.动态改变pwm输出,改变值 HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); 函数总结: __HAL_TIM ...
- C#串口通信及数据表格存储
1.开发环境 系统:win10 开发工具:Visual Studio 2017 2.界面设计 串口通信的界面大致如此,在此基础上添加项目所需的调试指令与数据存储功能,界面排布方面可参考其他教程. 3. ...
- aiohttp爬虫的模板,类的形式
import asyncio import aiohttp import async_timeout from lxml import html from timeit import default_ ...