pycocotools库怎么安装和使用

其他教程   发布日期:2023年06月26日   浏览次数:470

这篇“pycocotools库怎么安装和使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“pycocotools库怎么安装和使用”文章吧。

    pycocotools库的简介

    pycocotools是什么?即python api tools of COCO。

    COCO是一个大型的图像数据集,用于目标检测、分割、人的关键点检测、素材分割和标题生成。

    这个包提供了Matlab、Python和luaapi,这些api有助于在COCO中加载、解析和可视化注释。

    COCO网站上也描述了注释的确切格式。

    Matlab和PythonAPI是完整的,LuaAPI只提供基本功能。

    pycocotools库的安装

    1. pip install pycocotools==2.0.0
    2. or
    3. pip install pycocotools-windows

    pycocotools库的使用方法

    1、from pycocotools.coco import COCO

    1. __author__ = 'tylin'
    2. __version__ = '2.0'
    3. # Interface for accessing the Microsoft COCO dataset.
    4. # Microsoft COCO is a large image dataset designed for object detection,
    5. # segmentation, and caption generation. pycocotools is a Python API that
    6. # assists in loading, parsing and visualizing the annotations in COCO.
    7. # Please visit http://mscoco.org/ for more information on COCO, including
    8. # for the data, paper, and tutorials. The exact format of the annotations
    9. # is also described on the COCO website. For example usage of the pycocotools
    10. # please see pycocotools_demo.ipynb. In addition to this API, please download both
    11. # the COCO images and annotations in order to run the demo.
    12. # An alternative to using the API is to load the annotations directly
    13. # into Python dictionary
    14. # Using the API provides additional utility functions. Note that this API
    15. # supports both *instance* and *caption* annotations. In the case of
    16. # captions not all functions are defined (e.g. categories are undefined).
    17. # The following API functions are defined:
    18. # COCO - COCO api class that loads COCO annotation file and prepare data structures.
    19. # decodeMask - Decode binary mask M encoded via run-length encoding.
    20. # encodeMask - Encode binary mask M using run-length encoding.
    21. # getAnnIds - Get ann ids that satisfy given filter conditions.
    22. # getCatIds - Get cat ids that satisfy given filter conditions.
    23. # getImgIds - Get img ids that satisfy given filter conditions.
    24. # loadAnns - Load anns with the specified ids.
    25. # loadCats - Load cats with the specified ids.
    26. # loadImgs - Load imgs with the specified ids.
    27. # annToMask - Convert segmentation in an annotation to binary mask.
    28. # showAnns - Display the specified annotations.
    29. # loadRes - Load algorithm results and create API for accessing them.
    30. # download - Download COCO images from mscoco.org server.
    31. # Throughout the API "ann"=annotation, "cat"=category, and "img"=image.
    32. # Help on each functions can be accessed by: "help COCO>function".
    33. # See also COCO>decodeMask,
    34. # COCO>encodeMask, COCO>getAnnIds, COCO>getCatIds,
    35. # COCO>getImgIds, COCO>loadAnns, COCO>loadCats,
    36. # COCO>loadImgs, COCO>annToMask, COCO>showAnns
    37. # Microsoft COCO Toolbox. version 2.0
    38. # Data, paper, and tutorials available at: http://mscoco.org/
    39. # Code written by Piotr Dollar and Tsung-Yi Lin, 2014.
    40. # Licensed under the Simplified BSD License [see bsd.txt]

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

    1. from pycocotools.coco import COCO
    2. import matplotlib.pyplot as plt
    3. import cv2
    4. import os
    5. import numpy as np
    6. import random
    7. #1、定义数据集路径
    8. cocoRoot = "F:/File_Python/Resources/image/COCO"
    9. dataType = "val2017"
    10. annFile = os.path.join(cocoRoot, f'annotations/instances_{dataType}.json')
    11. print(f'Annotation file: {annFile}')
    12. #2、为实例注释初始化COCO的API
    13. coco=COCO(annFile)
    14. #3、采用不同函数获取对应数据或类别
    15. ids = coco.getCatIds('person')[0] #采用getCatIds函数获取"person"类别对应的ID
    16. print(f'"person" 对应的序号: {ids}')
    17. id = coco.getCatIds(['dog'])[0] #获取某一类的所有图片,比如获取包含dog的所有图片
    18. imgIds = coco.catToImgs[id]
    19. print(f'包含dog的图片共有:{len(imgIds)}张, 分别是:',imgIds)
    20. cats = coco.loadCats(1) #采用loadCats函数获取序号对应的类别名称
    21. print(f'"1" 对应的类别名称: {cats}')
    22. imgIds = coco.getImgIds(catIds=[1]) #采用getImgIds函数获取满足特定条件的图片(交集),获取包含person的所有图片
    23. print(f'包含person的图片共有:{len(imgIds)}张')
    24. #4、将图片进行可视化
    25. imgId = imgIds[10]
    26. imgInfo = coco.loadImgs(imgId)[0]
    27. print(f'图像{imgId}的信息如下:
    28. {imgInfo}')
    29. imPath = os.path.join(cocoRoot, 'images', dataType, imgInfo['file_name'])
    30. im = cv2.imread(imPath)
    31. plt.axis('off')
    32. plt.imshow(im)
    33. plt.show()
    34. plt.imshow(im); plt.axis('off')
    35. annIds = coco.getAnnIds(imgIds=imgInfo['id']) # 获取该图像对应的anns的Id
    36. print(f'图像{imgInfo["id"]}包含{len(anns)}个ann对象,分别是:
    37. {annIds}')
    38. anns = coco.loadAnns(annIds)
    39. coco.showAnns(anns)
    40. print(f'ann{annIds[3]}对应的mask如下:')
    41. mask = coco.annToMask(anns[3])
    42. plt.imshow(mask); plt.axis('off')

    以上就是pycocotools库怎么安装和使用的详细内容,更多关于pycocotools库怎么安装和使用的资料请关注九品源码其它相关文章!