欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Py之pycocotools庫的簡介、安裝、使用方法及說明

 更新時間:2023年02月22日 09:13:06   投稿:jingxian  
這篇文章主要介紹了Py之pycocotools庫的簡介、安裝、使用方法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

pycocotools庫的簡介

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

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

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

請訪問COCO - Common Objects in Context,可以了解關于COCO的更多信息,包括數(shù)據(jù)、論文和教程。

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

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

除了這個API,請下載COCO圖片和注釋,以便運行演示和使用API。

兩者都可以在項目網(wǎng)站上找到。

  • -請下載、解壓縮并將圖像放入: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數(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ù)獲取對應數(shù)據(jù)或類別
ids = coco.getCatIds('person')[0]    #采用getCatIds函數(shù)獲取"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函數(shù)獲取序號對應的類別名稱
print(f'"1" 對應的類別名稱: {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'])      # 獲取該圖像對應的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')

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python實現(xiàn)線性回歸算法

    python實現(xiàn)線性回歸算法

    這篇文章主要為大家詳細介紹了python實現(xiàn)線性回歸算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]

    Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]

    這篇文章主要介紹了Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n],文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 一篇文章弄懂Python中的內建函數(shù)

    一篇文章弄懂Python中的內建函數(shù)

    Python學習,內建函數(shù)是你必須要掌握的一部分,下面這篇文章主要給大家介紹了關于Python中內建函數(shù)的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2021-08-08
  • python opencv 圖像拼接的實現(xiàn)方法

    python opencv 圖像拼接的實現(xiàn)方法

    高級圖像拼接也叫作基于特征匹配的圖像拼接,拼接時消去兩幅圖像相同的部分,實現(xiàn)拼接合成全景圖。這篇文章主要介紹了python opencv 圖像拼接,需要的朋友可以參考下
    2019-06-06
  • Python處理excel與txt文件詳解

    Python處理excel與txt文件詳解

    大家好,本篇文章主要講的是Python處理excel與txt文件詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 詳解如何理解并正確使用Python中的f字符串

    詳解如何理解并正確使用Python中的f字符串

    Python中的f字符串是一種字符串格式化語法,它可以將變量、表達式和函數(shù)等動態(tài)地嵌入到字符串中,本文就來詳細講講如何理解并正確使用它吧
    2023-06-06
  • python:目標檢測模型預測準確度計算方式(基于IoU)

    python:目標檢測模型預測準確度計算方式(基于IoU)

    今天小編就為大家分享一篇python:目標檢測模型預測準確度計算方式(基于IoU),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python打包生成so文件的實現(xiàn)

    python打包生成so文件的實現(xiàn)

    這篇文章主要介紹了python打包生成so文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • vue.js實現(xiàn)輸入框輸入值內容實時響應變化示例

    vue.js實現(xiàn)輸入框輸入值內容實時響應變化示例

    這篇文章主要介紹了vue.js實現(xiàn)輸入框輸入值內容實時響應變化,結合實例形式分析了vue.js使用v-model屬性進行數(shù)據(jù)綁定的相關操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python中利用函數(shù)裝飾器實現(xiàn)備忘功能

    Python中利用函數(shù)裝飾器實現(xiàn)備忘功能

    這篇文章主要介紹了Python中利用函數(shù)裝飾器實現(xiàn)備忘功能,同時還降到了利用裝飾器來檢查函數(shù)的遞歸、確保參數(shù)傳遞的正確,需要的朋友可以參考下
    2015-03-03

最新評論