溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

pycocotools庫怎么安裝和使用

發(fā)布時間:2023-02-22 10:48:05 來源:億速云 閱讀:109 作者:iii 欄目:開發(fā)技術(shù)

這篇“pycocotools庫怎么安裝和使用”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“pycocotools庫怎么安裝和使用”文章吧。

    pycocotools庫的簡介

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

    COCO是一個大型的圖像數(shù)據(jù)集,用于目標檢測、分割、人的關(guān)鍵點檢測、素材分割和標題生成。

    這個包提供了Matlab、Python和luaapi,這些api有助于在COCO中加載、解析和可視化注釋。

    COCO網(wǎng)站上也描述了注釋的確切格式。

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

    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數(shù)據(jù)集信息并進行圖片可視化

    from pycocotools.coco import COCO
    import matplotlib.pyplot as plt
    import cv2
    import os
    import numpy as np
    import random
     
     
    #1、定義數(shù)據(jù)集路徑
    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、采用不同函數(shù)獲取對應(yīng)數(shù)據(jù)或類別
    ids = coco.getCatIds('person')[0]    #采用getCatIds函數(shù)獲取"person"類別對應(yīng)的ID
    print(f'"person" 對應(yīng)的序號: {ids}') 
    id = coco.getCatIds(['dog'])[0]      #獲取某一類的所有圖片,比如獲取包含dog的所有圖片
    imgIds = coco.catToImgs[id]
    print(f'包含dog的圖片共有:{len(imgIds)}張, 分別是:',imgIds)
     
     
    cats = coco.loadCats(1)               #采用loadCats函數(shù)獲取序號對應(yīng)的類別名稱
    print(f'"1" 對應(yīng)的類別名稱: {cats}')
     
    imgIds = coco.getImgIds(catIds=[1])    #采用getImgIds函數(shù)獲取滿足特定條件的圖片(交集),獲取包含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'])      # 獲取該圖像對應(yīng)的anns的Id
    print(f'圖像{imgInfo["id"]}包含{len(anns)}個ann對象,分別是:\n{annIds}')
    anns = coco.loadAnns(annIds)
     
    coco.showAnns(anns)
    print(f'ann{annIds[3]}對應(yīng)的mask如下:')
    mask = coco.annToMask(anns[3])
    plt.imshow(mask); plt.axis('off')

    以上就是關(guān)于“pycocotools庫怎么安裝和使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI