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技术共同构建的, 支持开发者远程开发的免费 ...
随机推荐
- sort_area_retained_size之tom解释
sort_area_retained_size 摘录一段asktom中tom的解释,对sort内存分配的方式进行了描述: it will allocate up to sort_area_retain ...
- win10 切换网卡的bat
@echo off >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system3 ...
- oracle 子查询的几个种类
1.where型子查询: select cat_id,good_id,good_name from goods where good_id in (selct max(good_id) from go ...
- iOS-Cordova集成开发,已有项目集成cordova
iOS-Cordova集成开发,已有项目集成cordova 项目组准备开发一个APP,要求Android和iOS端页面完全一致,除了一个页面跟业务相关的不同,其他界面基本一致,因此,萌生一个想法,关于 ...
- ES6学习之-let 和const命令
let 和const命令 let命令 用来声明变量,类似于var .let声明的变量 只在let命令所在的代码块内有效. 在for循环里也是如此 每次循环其实都是一个代码块 function fn() ...
- 关于python文件问题
一.python内部的首行命令 #!/usr/bin/env python #_*_coding:utf8_*_ 第一条命令用于Linux系统中的./命令,用于声明用什么Python解释器.第二条命令 ...
- s3c6410 RomCode文档读后总结
最近无意中看到一篇关于s3c6410 RomCode的介绍,结合自己的经验,做个总结. 首先贴张图,具体描述下该芯片的启动方式及具体流程. 因为s3c6410的板子多数是从SD或者Nand方式启动,重 ...
- python3 用户登录验证的小功能
用户登录验证,记录一下,还需要修改黑名单不合理 #!/usr/bin/env python3 ''' 需求:编写登录接口 1.输入用户名和密码 2.验证用户密码成功后输出欢迎消息 3.3次没有验证通过 ...
- 20155227 2016-2017-2《Java程序设计》课程总结
20155227 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:我眼中的师生关系. 预备作业2:一些简单的C语言知识回顾,并总结之前一些成功的 ...
- 20155233 2006-2007-2 《Java程序设计》第4周学习总结
20155233 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 第六章: 继承:子类继承父类,避免重复的行为定义. extends关键词:继承并扩充行为. ...