Py之pycocotools庫(kù)的簡(jiǎn)介、安裝、使用方法及說(shuō)明
pycocotools庫(kù)的簡(jiǎn)介
pycocotools是什么?即python api tools of COCO。
COCO是一個(gè)大型的圖像數(shù)據(jù)集,用于目標(biāo)檢測(cè)、分割、人的關(guān)鍵點(diǎn)檢測(cè)、素材分割和標(biāo)題生成。
這個(gè)包提供了Matlab、Python和luaapi,這些api有助于在COCO中加載、解析和可視化注釋。
請(qǐng)?jiān)L問(wèn)COCO - Common Objects in Context,可以了解關(guān)于COCO的更多信息,包括數(shù)據(jù)、論文和教程。
COCO網(wǎng)站上也描述了注釋的確切格式。
Matlab和PythonAPI是完整的,LuaAPI只提供基本功能。
除了這個(gè)API,請(qǐng)下載COCO圖片和注釋?zhuān)员氵\(yùn)行演示和使用API。
兩者都可以在項(xiàng)目網(wǎng)站上找到。
- -請(qǐng)下載、解壓縮并將圖像放入:coco/images/
- -請(qǐng)下載并將注釋放在:coco/annotations中/
COCO API: http://cocodataset.org/
pycocotools庫(kù)的安裝
pip install pycocotools==2.0.0 or pip install pycocotools-windows
pycocotools庫(kù)的使用方法
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ù)集信息并進(jìn)行圖片可視化
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、為實(shí)例注釋初始化COCO的API
coco=COCO(annFile)
#3、采用不同函數(shù)獲取對(duì)應(yīng)數(shù)據(jù)或類(lèi)別
ids = coco.getCatIds('person')[0] #采用getCatIds函數(shù)獲取"person"類(lèi)別對(duì)應(yīng)的ID
print(f'"person" 對(duì)應(yīng)的序號(hào): {ids}')
id = coco.getCatIds(['dog'])[0] #獲取某一類(lèi)的所有圖片,比如獲取包含dog的所有圖片
imgIds = coco.catToImgs[id]
print(f'包含dog的圖片共有:{len(imgIds)}張, 分別是:',imgIds)
cats = coco.loadCats(1) #采用loadCats函數(shù)獲取序號(hào)對(duì)應(yīng)的類(lèi)別名稱(chēng)
print(f'"1" 對(duì)應(yīng)的類(lèi)別名稱(chēng): {cats}')
imgIds = coco.getImgIds(catIds=[1]) #采用getImgIds函數(shù)獲取滿足特定條件的圖片(交集),獲取包含person的所有圖片
print(f'包含person的圖片共有:{len(imgIds)}張')
#4、將圖片進(jìn)行可視化
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']) # 獲取該圖像對(duì)應(yīng)的anns的Id
print(f'圖像{imgInfo["id"]}包含{len(anns)}個(gè)ann對(duì)象,分別是:\n{annIds}')
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
print(f'ann{annIds[3]}對(duì)應(yīng)的mask如下:')
mask = coco.annToMask(anns[3])
plt.imshow(mask); plt.axis('off')總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過(guò)Python腳本+Jenkins實(shí)現(xiàn)項(xiàng)目重啟
Jenkins是一個(gè)流行的開(kāi)源自動(dòng)化服務(wù)器,用于快速構(gòu)建、測(cè)試和部署軟件,本文主要介紹了通過(guò)Python腳本+Jenkins實(shí)現(xiàn)項(xiàng)目重啟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Django cookie和session的應(yīng)用場(chǎng)景及如何使用
今天我們來(lái)重點(diǎn)看下Django中session和cookie的用法吧。我們會(huì)介紹cookie和session的工作原理,還會(huì)分享實(shí)際應(yīng)用的案例。2021-04-04
Python numpy數(shù)組轉(zhuǎn)置與軸變換
這篇文章主要介紹了Python numpy數(shù)組轉(zhuǎn)置與軸變換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
python+pandas分析nginx日志的實(shí)例
下面小編就為大家分享一篇python+pandas分析nginx日志的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python爬蟲(chóng) 線程池創(chuàng)建并獲取文件代碼實(shí)例
這篇文章主要介紹了python爬蟲(chóng) 線程池創(chuàng)建并獲取文件代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
python?random庫(kù)的簡(jiǎn)單使用demo
這篇文章主要為大家介紹了python?random庫(kù)的簡(jiǎn)單使用demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Python微醫(yī)掛號(hào)網(wǎng)醫(yī)生數(shù)據(jù)抓取
今天小編就為大家分享一篇關(guān)于Python微醫(yī)掛號(hào)網(wǎng)醫(yī)生數(shù)據(jù)抓取,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Python常見(jiàn)的幾種數(shù)據(jù)加密方式
這篇文章主要分享的是Python常見(jiàn)的幾種數(shù)據(jù)加密方式,主要包括線性散列算法(簽名算法)MD5,sha1、對(duì)稱(chēng)性加密算法?AES?DES、非對(duì)稱(chēng)性加密算法?RSA,具體詳細(xì)內(nèi)容介紹,需要的小伙伴可以參考一下2022-06-06
使用Python對(duì)EXCEL數(shù)據(jù)的預(yù)處理
這篇文章主要介紹了使用Python處理EXCEL基礎(chǔ)操作篇2,如何使用Python對(duì)EXCEL數(shù)據(jù)的預(yù)處理,文中提供了解決思路和部分實(shí)現(xiàn)代碼,一起來(lái)看看吧2023-03-03
Nginx搭建HTTPS服務(wù)器和強(qiáng)制使用HTTPS訪問(wèn)的方法
這篇文章主要介紹了Nginx搭建HTTPS服務(wù)器和強(qiáng)制使用HTTPS訪問(wèn)的方法,即從HTTP跳轉(zhuǎn)到HTTPS,需要的朋友可以參考下2015-08-08

