Py之pycocotools:pycocotools库的简介、安装、使用方法之详细攻略续篇


答答
答答 2022-09-19 10:30:31 49215
分类专栏: 资讯

 Py之pycocotools:pycocotools库的简介、安装、使用方法之详细攻略

目录

pycocotools库的简介

pycocotools库的安装

pycocotools库的使用方法

1、from pycocotools.coco import COCO

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


pycocotools库的简介

       pycocotools是什么?即python api tools of COCO。COCO是一个大型的图像数据集,用于目标检测、分割、人的关键点检测、素材分割和标题生成。这个包提供了Matlab、Python和luaapi,这些api有助于在COCO中加载、解析和可视化注释。请访问COCO - Common Objects in Context,可以了解关于COCO的更多信息,包括数据、论文和教程。COCO网站上也描述了注释的确切格式。Matlab和PythonAPI是完整的,LuaAPI只提供基本功能。
       除了这个API,请下载COCO图片和注释,以便运行演示和使用API。两者都可以在项目网站上找到。

  • -请下载、解压缩并将图像放入:coco/images/
  • -请下载并将注释放在:coco/annotations中/

COCO API http://cocodataset.org/

pycocotools库的安装

pip install pycocotools==2.0.0

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_-subst">{dataType}.json')
  11. print(f'Annotation file: -subst">{annFile}')
  12. 2、为实例注释初始化COCO的API
  13. coco=COCO(annFile)
  14. 3、采用不同函数获取对应数据或类别
  15. ids = coco.getCatIds('person')[0] 采用getCatIds函数获取"person"类别对应的ID
  16. print(f'"person" 对应的序号: -subst">{ids}')
  17. id = coco.getCatIds(['dog'])[0] 获取某一类的所有图片,比如获取包含dog的所有图片
  18. imgIds = coco.catToImgs[id]
  19. print(f'包含dog的图片共有:-subst">{len(imgIds)}张, 分别是:',imgIds)
  20. cats = coco.loadCats(1) 采用loadCats函数获取序号对应的类别名称
  21. print(f'"1" 对应的类别名称: -subst">{cats}')
  22. imgIds = coco.getImgIds(catIds=[1]) 采用getImgIds函数获取满足特定条件的图片(交集),获取包含person的所有图片
  23. print(f'包含person的图片共有:-subst">{len(imgIds)}张')
  24. 4、将图片进行可视化
  25. imgId = imgIds[10]
  26. imgInfo = coco.loadImgs(imgId)[0]
  27. print(f'图像-subst">{imgId}的信息如下:\n-subst">{imgInfo}')
  28. imPath = os.path.join(cocoRoot, 'images', dataType, imgInfo['file_name'])
  29. im = cv2.imread(imPath)
  30. plt.axis('off')
  31. plt.imshow(im)
  32. plt.show()
  33. plt.imshow(im); plt.axis('off')
  34. annIds = coco.getAnnIds(imgIds=imgInfo['id']) 获取该图像对应的anns的Id
  35. print(f'图像-subst">{imgInfo["id"]}包含-subst">{len(anns)}个ann对象,分别是:\n-subst">{annIds}')
  36. anns = coco.loadAnns(annIds)
  37. coco.showAnns(anns)
  38. print(f'ann-subst">{annIds[3]}对应的mask如下:')
  39. mask = coco.annToMask(anns[3])
  40. plt.imshow(mask); plt.axis('off')
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树预备知识Python简介123796 人正在系统学习中

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=1787
赞同 0
评论 0 条
答答L0
粉丝 0 发表 5 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2959
【软件正版化】软件正版化工作要点  2878
统信UOS试玩黑神话:悟空  2843
信刻光盘安全隔离与信息交换系统  2737
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1270
grub引导程序无法找到指定设备和分区  1235
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  165
点击报名 | 京东2025校招进校行程预告  164
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  163
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  159
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!