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技术共同构建的, 支持开发者远程开发的免费 ...
随机推荐
- 处理HTML表单(11)
PHP和Web表单 <?php if(isset($_POST["name"])){//isset()函数设置变量是否设置,并且不能为空 $name = $_POST[&qu ...
- Objective-C基础知识之“类”
Objective-C语言是iOS开发的专用语言,虽然现在在逐步被swift语言取代,但是仍可以作为基础学习,学会Objective-C之后入手swift也是相当快速.今天我来简谈一下关于OC中的类. ...
- Python实现创建字典
编写一个名为 make_album() 的函数,它创建一个描述音乐专辑的字典.1.这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典.使用这个函数创建三个表示不同专辑的字典,并打印每个返 ...
- js清除浏览器缓存
浏览器缓存 所有的数据都可以存到服务器中,但这样并不高效,当我们访问网页的时候,一会卡顿,二会浪费服务器的存储空间,三会给服务器造成压力 浏览器缓存,可以提高网站性能和浏览器的速度,但对于需要经常更新 ...
- PHP-----PHP程序设计基础教程----第一章PHP开篇
本章内容知识体系如下所示: 一.PHP基础知识 1.1 Web技术 服务器与客户端 服务器:能够提供某种服务的电脑 客户端:想使用服务器所提供服务的电脑 服务器也是一台电脑,必须安装操作系统.否则就是 ...
- 快速提高谷歌浏览器(Chrome)自带下载器的网速
之前每次下载东西都是复制好下载链接到迅雷中下载,会提高成倍网速,但是时间一长,感觉不方便,废话不多说,上干货~ 由于中国防火墙(GFW)的强大,在线下载Google浏览器的时候速度非常慢,如果只是单独 ...
- The Bitizens Team
bitizens.bitguild.com 首个区块链3D艺术品. https://mybitizens.bitguild.com/#/igo https://www.youtube.com/watc ...
- mysql主键重复,不抱错,只更新的骚操作 (如果没有插入,如果有更新)
平时我们在设计数据库表的时候总会设计 unique 或者 给表加上 primary key 的限制条件. 此时 插入数据的时候 ,经常会有这样的情况: 我们想向数据库插入一条记录: 若数据表中存在以 ...
- Flume(4)-监控模型
一. 监控端口数据 首先启动Flume任务,监控本机44444端口,服务端: 然后通过netcat工具向本机44444端口发送消息,客户端: 最后Flume将监听的数据实时显示在控制台. 1. 安装n ...
- HIVE基本语法以及HIVE分区
HIVE小结 HIVE基本语法 HIVE和Mysql十分类似 建表规则 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name da ...