Py之pycocotools:pycocotools库的简介、安装、使用方法之详细攻略

目录

pycocotools库的简介

pycocotools库的安装

pycocotools库的使用方法

1、from pycocotools.coco import COCO

2、输出COCO数据集信息并进行图片可视化


pycocotools库的简介

pycocotools是什么?即python api tools of COCO。COCO是一个大型的图像数据集,用于目标检测、分割、人的关键点检测、素材分割和标题生成。这个包提供了Matlab、Python和luaapi,这些api有助于在COCO中加载、解析和可视化注释。请访问COCO - Common Objects in Context,可以了解关于COCO的更多信息,包括数据、论文和教程。COCO网站上也描述了注释的确切格式。Matlab和PythonAPI是完整的,LuaAPI只提供基本功能。
       除了这个API,请下载COCO图片和注释,以便运行演示和使用API。两者都可以在项目网站上找到。

  • -请下载、解压缩并将图像放入:coco/images/
  • -请下载并将注释放在:coco/annotations中/

COCO API: http://cocodataset.org/

pycocotools库的安装

pip install pycocotools==2.0.0# 网友燕尾服假面 2023.04.11提供
conda install -c conda-forge pycocotools

pycocotools库的使用方法

1、from pycocotools.coco import COCO

__author__ = 'tylin'
__version__ = '2.0'
# Interface for accessing the Microsoft COCO dataset.# Microsoft COCO is a large image dataset designed for object detection,
# segmentation, and caption generation. pycocotools is a Python API that
# assists in loading, parsing and visualizing the annotations in COCO.
# Please visit http://mscoco.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. For example usage of the pycocotools
# please see pycocotools_demo.ipynb. In addition to this API, please download both
# the COCO images and annotations in order to run the demo.# An alternative to using the API is to load the annotations directly
# into Python dictionary
# Using the API provides additional utility functions. Note that this API
# supports both *instance* and *caption* annotations. In the case of
# captions not all functions are defined (e.g. categories are undefined).# The following API functions are defined:
#  COCO       - COCO api class that loads COCO annotation file and prepare data structures.
#  decodeMask - Decode binary mask M encoded via run-length encoding.
#  encodeMask - Encode binary mask M using run-length encoding.
#  getAnnIds  - Get ann ids that satisfy given filter conditions.
#  getCatIds  - Get cat ids that satisfy given filter conditions.
#  getImgIds  - Get img ids that satisfy given filter conditions.
#  loadAnns   - Load anns with the specified ids.
#  loadCats   - Load cats with the specified ids.
#  loadImgs   - Load imgs with the specified ids.
#  annToMask  - Convert segmentation in an annotation to binary mask.
#  showAnns   - Display the specified annotations.
#  loadRes    - Load algorithm results and create API for accessing them.
#  download   - Download COCO images from mscoco.org server.
# Throughout the API "ann"=annotation, "cat"=category, and "img"=image.
# Help on each functions can be accessed by: "help COCO>function".# See also COCO>decodeMask,
# COCO>encodeMask, COCO>getAnnIds, COCO>getCatIds,
# COCO>getImgIds, COCO>loadAnns, COCO>loadCats,
# COCO>loadImgs, COCO>annToMask, COCO>showAnns# Microsoft COCO Toolbox.      version 2.0
# Data, paper, and tutorials available at:  http://mscoco.org/
# Code written by Piotr Dollar and Tsung-Yi Lin, 2014.
# Licensed under the Simplified BSD License [see bsd.txt]

2、输出COCO数据集信息并进行图片可视化

from pycocotools.coco import COCO
import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
import random#1、定义数据集路径
cocoRoot = "F:/File_Python/Resources/image/COCO"
dataType = "val2017"
annFile = os.path.join(cocoRoot, f'annotations/instances_{dataType}.json')
print(f'Annotation file: {annFile}')#2、为实例注释初始化COCO的API
coco=COCO(annFile)#3、采用不同函数获取对应数据或类别
ids = coco.getCatIds('person')[0]    #采用getCatIds函数获取"person"类别对应的ID
print(f'"person" 对应的序号: {ids}')
id = coco.getCatIds(['dog'])[0]      #获取某一类的所有图片,比如获取包含dog的所有图片
imgIds = coco.catToImgs[id]
print(f'包含dog的图片共有:{len(imgIds)}张, 分别是:',imgIds)cats = coco.loadCats(1)               #采用loadCats函数获取序号对应的类别名称
print(f'"1" 对应的类别名称: {cats}')imgIds = coco.getImgIds(catIds=[1])    #采用getImgIds函数获取满足特定条件的图片(交集),获取包含person的所有图片
print(f'包含person的图片共有:{len(imgIds)}张')#4、将图片进行可视化
imgId = imgIds[10]
imgInfo = coco.loadImgs(imgId)[0]
print(f'图像{imgId}的信息如下:\n{imgInfo}')imPath = os.path.join(cocoRoot, 'images', dataType, imgInfo['file_name'])
im = cv2.imread(imPath)
plt.axis('off')
plt.imshow(im)
plt.show()plt.imshow(im); plt.axis('off')
annIds = coco.getAnnIds(imgIds=imgInfo['id'])      # 获取该图像对应的anns的Id
print(f'图像{imgInfo["id"]}包含{len(anns)}个ann对象,分别是:\n{annIds}')
anns = coco.loadAnns(annIds)coco.showAnns(anns)
print(f'ann{annIds[3]}对应的mask如下:')
mask = coco.annToMask(anns[3])
plt.imshow(mask); plt.axis('off')

Py之pycocotools:pycocotools库的简介、安装、使用方法之详细攻略续篇相关推荐

  1. Py之glob: glob库文件名模式匹配+返回所有匹配的文件路径列表库的简介、使用方法之详细攻略

    Py之glob: glob库文件名模式匹配+返回所有匹配的文件路径列表库的简介.使用方法之详细攻略 目录 glob库的简介 glob库的使用方法 1.单个字符通配符:用问号(?)匹配任何单个的字符.

  2. Python语言学习:Python常用自带库(imageio、pickle)简介、使用方法之详细攻略

    Python语言学习:Python常用自带库(imageio.pickle)简介.使用方法之详细攻略 目录 imageio简介及其常见使用方法 pickle简介及其常见使用方法 简介 使用方法 简介及 ...

  3. Py之matplotlib.pyplot:matplotlib.pyplot的plt.legend函数的简介、使用方法之详细攻略

    Py之matplotlib.pyplot:matplotlib.pyplot的plt.legend函数的简介.使用方法之详细攻略 目录 matplotlib.pyplot的plt.legend函数的简 ...

  4. Python:numpy库中的一些函数简介、使用方法之详细攻略

    Python:numpy库中的一些函数简介.使用方法之详细攻略 目录 numpy库中的一些函数简介.使用方法 1.np.concatenate() 1.1.函数案例 1.2.函数用法 numpy库中的 ...

  5. sklearn之XGBModel:XGBModel之feature_importances_、plot_importance的简介、使用方法之详细攻略

    sklearn之XGBModel:XGBModel之feature_importances_.plot_importance的简介.使用方法之详细攻略 目录 feature_importances_ ...

  6. Python之 sklearn:sklearn中的RobustScaler 函数的简介及使用方法之详细攻略

    Python之 sklearn:sklearn中的RobustScaler 函数的简介及使用方法之详细攻略 目录 sklearn中的RobustScaler 函数的简介及使用方法 sklearn中的R ...

  7. ML之sklearn:sklearn.linear_mode中的LogisticRegression函数的简介、使用方法之详细攻略

    ML之sklearn:sklearn.linear_mode中的LogisticRegression函数的简介.使用方法之详细攻略 目录 sklearn.linear_mode中的LogisticRe ...

  8. sklearn:sklearn.preprocessing.StandardScaler函数的fit_transform、transform、inverse_transform简介、使用方法之详细攻略

    sklearn:sklearn.preprocessing.StandardScaler函数的fit_transform.transform.inverse_transform简介.使用方法之详细攻略 ...

  9. Computer:互联网开放平台项目知识补充之开发-运维-网络-网关等术语(DMZ、负载均衡、F5、Nginx、容器)的简介、使用方法之详细攻略

    Computer:互联网开放平台项目知识补充之开发-运维-网络-网关等术语(DMZ.负载均衡.F5.Nginx.容器)的简介.使用方法之详细攻略 目录 DMZ(隔离区)的简介及其使用方法 1.DMZ区 ...

  10. Python编程语言学习:包导入和模块搜索路径简介、使用方法之详细攻略

    Python编程语言学习:包导入和模块搜索路径简介.使用方法之详细攻略 目录 包导入和模块搜索路径简介 1.Pyhon搜索模块路径的机制 2.自定义配置搜索路径

最新文章

  1. gif加文字 php,gif动态图片添加文字 gif制作软件 怎样给gif动态图片添加文字
  2. 中科院博士因论文致谢走红后,回到母校演讲再刷屏!网友:是对寒门学子最好的激励...
  3. 概率图模型PGM——D map, I map, perfect map
  4. Python黑帽编程 3.1 ARP欺骗
  5. 卡夫卡(kafka)
  6. java 阻塞队列 LinkedBlockingQueue ArrayBlockingQueue 分析
  7. 【Python】吊打pyecharts,又一超级棒的开源可视化库
  8. Poj2586 每五个月都是亏
  9. .h 与 .hpp 文件
  10. 研华数据采集卡如何采集压力信号转化为数字信号_感知世界的模拟量信号
  11. 我和老公清北毕业,我能接受自己的孩子读三流学校吗?
  12. React Native 参考资料 (转自简书)
  13. perf常用用法简介
  14. Xamarin.Forms 之我的花园 - 2.从照片库选择图片和拍照功能
  15. windows10修复引导
  16. element ui table中想使得其中一列数据拥有横向滚动条的效果
  17. 男厕所的小便斗 [ 光影人像 东海陈光剑 的博客 ]
  18. windows 下 wkhtmltopdf html编码正确转换后pdf 部分中文乱码问题
  19. firebird数据库安装连接的一些常见错误及解决方法
  20. macOS安装Redis

热门文章

  1. Markdown转Word文档在线工具
  2. 【计算理论】图灵机 ( 图灵机引入 | 公理化 | 希尔伯特纲领 | 哥德尔不完备定理 | 原始递归函数 )
  3. s:if test标签出错,内容不显示
  4. 齐岳供应TCPP-Fe(3+)四羧基苯基卟啉铁;TCPP-Zn(2+)四羧基苯基卟啉锌
  5. java远控_利用Java实现远程控制
  6. paypal的发展 总结笔记
  7. 计算机注销之后一直黑屏,Win7旗舰版电脑注销完黑屏怎么解决?
  8. I2C器件的从设备地址的设置(以AT24C02为例)
  9. 小明有一些矩形的材料,他要从这些矩形材料中切割出一些正方形。 当他面对一块矩形材料时,他总是从中间切割一刀,切出一块最大的正方 形
  10. SAP 凭证跳号分析