Py之pycocotools库的简介、安装、使用方法及说明

 更新时间:2023年02月22日 09:13:06   投稿:jingxian  
这篇文章主要介绍了Py之pycocotools库的简介、安装、使用方法及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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
or
pip install pycocotools-windows

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')

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 通过Python脚本+Jenkins实现项目重启

    通过Python脚本+Jenkins实现项目重启

    Jenkins是一个流行的开源自动化服务器,用于快速构建、测试和部署软件,本文主要介绍了通过Python脚本+Jenkins实现项目重启,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • Django cookie和session的应用场景及如何使用

    Django cookie和session的应用场景及如何使用

    今天我们来重点看下Django中session和cookie的用法吧。我们会介绍cookie和session的工作原理,还会分享实际应用的案例。
    2021-04-04
  • Python numpy数组转置与轴变换

    Python numpy数组转置与轴变换

    这篇文章主要介绍了Python numpy数组转置与轴变换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • python+pandas分析nginx日志的实例

    python+pandas分析nginx日志的实例

    下面小编就为大家分享一篇python+pandas分析nginx日志的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • python爬虫 线程池创建并获取文件代码实例

    python爬虫 线程池创建并获取文件代码实例

    这篇文章主要介绍了python爬虫 线程池创建并获取文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • python random库的简单使用demo

    python random库的简单使用demo

    这篇文章主要为大家介绍了python random库的简单使用demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Python微医挂号网医生数据抓取

    Python微医挂号网医生数据抓取

    今天小编就为大家分享一篇关于Python微医挂号网医生数据抓取,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Python常见的几种数据加密方式

    Python常见的几种数据加密方式

    这篇文章主要分享的是Python常见的几种数据加密方式,主要包括线性散列算法(签名算法)MD5,sha1、对称性加密算法 AES DES、非对称性加密算法 RSA,具体详细内容介绍,需要的小伙伴可以参考一下
    2022-06-06
  • 使用Python对EXCEL数据的预处理

    使用Python对EXCEL数据的预处理

    这篇文章主要介绍了使用Python处理EXCEL基础操作篇2,如何使用Python对EXCEL数据的预处理,文中提供了解决思路和部分实现代码,一起来看看吧
    2023-03-03
  • Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法

    Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法

    这篇文章主要介绍了Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法,即从HTTP跳转到HTTPS,需要的朋友可以参考下
    2015-08-08

最新评论