Introduction to the pycocotools library
pycocotoolswhat??assume (office)python api tools of COCO。
COCO is a large image dataset for target detection, segmentation, human keypoint detection, material segmentation and caption generation.
This package provides Matlab, Python and lua api's that help to load, parse and visualize annotations in COCO.
please visitCOCO - Common Objects in Context, you can learn more about COCO, including data, papers, and tutorials.
The exact format of the annotations is also described on the COCO website.
Matlab and PythonAPI are complete, LuaAPI provides only basic functionality.
In addition to this API, please download the COCO images and annotations in order to run the demo and use the API.
Both can be found on the program website.
- -Please download, unzip and place the images in: coco/images/
- -Please download and place the comments in: coco/annotations /
COCO API: /
Installation of the pycocotools library
pip install pycocotools==2.0.0 or pip install pycocotools-windows
How to use the pycocotools library
1、from 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 / 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 (. 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 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: / # Code written by Piotr Dollar and Tsung-Yi Lin, 2014. # Licensed under the Simplified BSD License [see ]
2、Output COCO dataset information and picture visualization
from import COCO import as plt import cv2 import os import numpy as np import random #1 Define the dataset path cocoRoot = "F:/File_Python/Resources/image/COCO" dataType = "val2017" annFile = (cocoRoot, f'annotations/instances_{dataType}.json') print(f'Annotation file: {annFile}') #2. initialize the COCO API for example annotations coco=COCO(annFile) #3. Use different functions to get the corresponding data or categories ids = ('person')[0] #Use getCatIds function to get the IDs of the "person" category. print(f'"person" Corresponding serial number: {ids}') id = (['dog'])[0] # Get all the images in a category, e.g. get all the images that contain dog imgIds = [id] print(f'embodydogThe total number of images in the:{len(imgIds)}sheet of paper, were...:',imgIds) cats = (1) # Use the loadCats function to get the name of the category corresponding to the serial number. print(f'"1" Corresponding category name: {cats}') imgIds = (catIds=[1]) # Use the getImgIds function to get images that meet specific conditions (intersection), get all images that contain person print(f'embodypersonThe total number of images in the:{len(imgIds)}sheet of paper') #4 Visualize the pictures imgId = imgIds[10] imgInfo = (imgId)[0] print(f'imagery{imgId}The information is as follows:\n{imgInfo}') imPath = (cocoRoot, 'images', dataType, imgInfo['file_name']) im = (imPath) ('off') (im) () (im); ('off') annIds = (imgIds=imgInfo['id']) # Get the Id of the anns corresponding to this image print(f'imagery{imgInfo["id"]}embody{len(anns)}classifier for individual things or people, general, catch-all classifierannboyfriend,were...:\n{annIds}') anns = (annIds) (anns) print(f'ann{annIds[3]}correspondingmaskas below:') mask = (anns[3]) (mask); ('off')
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.