Reference:

  1. https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb
  2. https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py
  3. 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 学习版的更多相关文章

  1. Sql Server 2008开发版(Developer Edition)过期升级企业版(Enterprise Edition)失败后安装学习版(Express Edition)

    最近一个多月,甚是悠哉,无事可做.上线的网站系统也没接到客户的反馈,反而觉得无聊之极了.上周五早上,一上QQ,就收到客户发来消息,管理平台无法登陆了.心里一惊,立马开始查找故障原因.翻看了系统日志,提 ...

  2. 学习版pytest内核测试平台开发万字长文入门篇

    前言 2021年,测试平台如雨后春笋般冒了出来,我就是其中一员,写了一款pytest内核测试平台,在公司落地.分享出来后,有同学觉得挺不错,希望能开源,本着"公司代码不要传到网上去,以免引起 ...

  3. 学习笔记︱Nvidia DIGITS网页版深度学习框架——深度学习版SPSS

    DIGITS: Deep Learning GPU Training System1,是由英伟达(NVIDIA)公司开发的第一个交互式深度学习GPU训练系统.目的在于整合现有的Deep Learnin ...

  4. oracle 11g XE 学习版添加scott用户方法全步骤

    安装企业版的orcale是不是太费时费力了?若只是学习用途的话,不妨试试轻便版的XE版本,同样是官网下载的,但是这个安装起来比完整版简便多了. 首先,你得先安装好orcale 11g XE 版本:(这 ...

  5. oracleXE(oracle学习版)在windows的安装配置

    oracleXE其实安装基本就是一路下一步. 在安装前记得先把tomcat打开,不然oracleXE会占用8080端口且安装过程无法更改端口. 如果需要远程连接此oracle XE的话,要关闭这台电脑 ...

  6. UIAutomator 学习版

    1.要写UIAutomator的testcase,首先要用Eclipse创建一个Java Project 需要将Junit 的lib加入到工程里 2.添加android.jar和uiautomator ...

  7. python3--装饰器高级学习版

    __author__ = "Aaron Fan"import time #导入time模块user,passwd = 'alex','abc123' #用户名密码def auth( ...

  8. Spring事务管理----------整合学习版

    作者:学无先后 达者为先 Spring提供了一流的事务管理.在Spring中可以支持声明式事务和编程式事务. 一  spring简介 1 Spring的事务       事务管理在应用程序中起着至关重 ...

  9. 深度学习菜鸟的信仰地︱Supervessel超能云服务器、深度学习环境全配置

    并非广告~实在是太良心了,所以费时间给他们点赞一下~ SuperVessel云平台是IBM中国研究院和中国系统与技术中心基于POWER架构和OpenStack技术共同构建的, 支持开发者远程开发的免费 ...

随机推荐

  1. 【Dubbo源码阅读系列】服务暴露之远程暴露

    引言 什么叫 远程暴露 ?试着想象着这么一种场景:假设我们新增了一台服务器 A,专门用于发送短信提示给指定用户.那么问题来了,我们的 Message 服务上线之后,应该如何告知调用方服务器,服务器 A ...

  2. Page Object 设计模式-PO

    1.传统测试用例实现的弊端: 易读性差 复用性差 可维护性差 扩展性差 2.PO 设计模式图: 3.Page Object 的核心要素: 抽象封装一个 BasePage 基类,基类应该拥有一个只想 w ...

  3. .net core 实践笔记(三)--封装底层

    前言: 有了前面的工作,简单的架子基本搭建起来了,因为条件有限,只能先测试SqlServer的了,源码放出来,也希望有兴趣的伙伴可以一起改善,相信可以成为未来进阶架构师的第一步,自己有小项目的时候可以 ...

  4. Easyui之accordion修改Title样式,字体等

    .accordion{background:#fff;overflow:hidden;}.accordion .accordion-header{background:#efefef;border-t ...

  5. react路由传参

    方法1 <刷新页面参数会消失> <Link className="item" to={{pathname:'/order',params:{index :&quo ...

  6. C++ C# VC VC.net以及VC++有什么区别和联系?

    C/C++是编程语言,C是C++的爸爸,也就是说C++从C发展而来,而C++完全兼容C的语法.国际上有一个专门管理C++的机构,它们负责C++的标准制定. VC++是微软公司的C++编译环境,使用它可 ...

  7. pyntho经典面试题

    Python基础篇 1:为什么学习Python 2:通过什么途径学习Python 3:谈谈对Python和其他语言的区别 Python的优势: 4:简述解释型和编译型编程语言 5:Python的解释器 ...

  8. MySQL:如何导入导出数据表和如何清空有外建关联的数据表

    1.导入导出 导入数据库:前提:数据库和数据表要存在(已经被创建) (1)将数据表 test_user.sql 导入到test 数据库的test_user 表中 [root@test ~]# mysq ...

  9. python使用tablib库生成xls表格

    参考文档:http://python-tablib.org Tablib是一个MIT许可的格式不可知的表格数据集库.它允许您导入,导出和操作表格数据集.高级功能包括隔离,动态列,标签和过滤,以及无缝格 ...

  10. 关于使用iframe,父元素无法获得子iframe对的元素

    首先确定自己写的方法对不对: $(document.getElementById('iframe的ID').contentWindow.document.body).find("要获得的元素 ...