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

简介: 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. 
5. # Microsoft COCO is a large image dataset designed for object detection,
6. # segmentation, and caption generation. pycocotools is a Python API that
7. # assists in loading, parsing and visualizing the annotations in COCO.
8. # Please visit http://mscoco.org/ for more information on COCO, including
9. # for the data, paper, and tutorials. The exact format of the annotations
10. # is also described on the COCO website. For example usage of the pycocotools
11. # please see pycocotools_demo.ipynb. In addition to this API, please download both
12. # the COCO images and annotations in order to run the demo.
13. 
14. # An alternative to using the API is to load the annotations directly
15. # into Python dictionary
16. # Using the API provides additional utility functions. Note that this API
17. # supports both *instance* and *caption* annotations. In the case of
18. # captions not all functions are defined (e.g. categories are undefined).
19. 
20. # The following API functions are defined:
21. #  COCO       - COCO api class that loads COCO annotation file and prepare data structures.
22. #  decodeMask - Decode binary mask M encoded via run-length encoding.
23. #  encodeMask - Encode binary mask M using run-length encoding.
24. #  getAnnIds  - Get ann ids that satisfy given filter conditions.
25. #  getCatIds  - Get cat ids that satisfy given filter conditions.
26. #  getImgIds  - Get img ids that satisfy given filter conditions.
27. #  loadAnns   - Load anns with the specified ids.
28. #  loadCats   - Load cats with the specified ids.
29. #  loadImgs   - Load imgs with the specified ids.
30. #  annToMask  - Convert segmentation in an annotation to binary mask.
31. #  showAnns   - Display the specified annotations.
32. #  loadRes    - Load algorithm results and create API for accessing them.
33. #  download   - Download COCO images from mscoco.org server.
34. # Throughout the API "ann"=annotation, "cat"=category, and "img"=image.
35. # Help on each functions can be accessed by: "help COCO>function".
36. 
37. # See also COCO>decodeMask,
38. # COCO>encodeMask, COCO>getAnnIds, COCO>getCatIds,
39. # COCO>getImgIds, COCO>loadAnns, COCO>loadCats,
40. # COCO>loadImgs, COCO>annToMask, COCO>showAnns
41. 
42. # Microsoft COCO Toolbox.      version 2.0
43. # Data, paper, and tutorials available at:  http://mscoco.org/
44. # Code written by Piotr Dollar and Tsung-Yi Lin, 2014.
45. # 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. 
8. 
9. #1、定义数据集路径
10. cocoRoot = "F:/File_Python/Resources/image/COCO"
11. dataType = "val2017"
12. annFile = os.path.join(cocoRoot, f'annotations/instances_{dataType}.json')
13. print(f'Annotation file: {annFile}')
14. 
15. #2、为实例注释初始化COCO的API
16. coco=COCO(annFile)
17. 
18. 
19. #3、采用不同函数获取对应数据或类别
20. ids = coco.getCatIds('person')[0]    #采用getCatIds函数获取"person"类别对应的ID
21. print(f'"person" 对应的序号: {ids}') 
22. id = coco.getCatIds(['dog'])[0]      #获取某一类的所有图片,比如获取包含dog的所有图片
23. imgIds = coco.catToImgs[id]
24. print(f'包含dog的图片共有:{len(imgIds)}张, 分别是:',imgIds)
25. 
26. 
27. cats = coco.loadCats(1)               #采用loadCats函数获取序号对应的类别名称
28. print(f'"1" 对应的类别名称: {cats}')
29. 
30. imgIds = coco.getImgIds(catIds=[1])    #采用getImgIds函数获取满足特定条件的图片(交集),获取包含person的所有图片
31. print(f'包含person的图片共有:{len(imgIds)}张')
32. 
33. 
34. 
35. #4、将图片进行可视化
36. imgId = imgIds[10]
37. imgInfo = coco.loadImgs(imgId)[0]
38. print(f'图像{imgId}的信息如下:\n{imgInfo}')
39. 
40. imPath = os.path.join(cocoRoot, 'images', dataType, imgInfo['file_name'])                     
41. im = cv2.imread(imPath)
42. plt.axis('off')
43. plt.imshow(im)
44. plt.show()
45. 
46. 
47. plt.imshow(im); plt.axis('off')
48. annIds = coco.getAnnIds(imgIds=imgInfo['id'])      # 获取该图像对应的anns的Id
49. print(f'图像{imgInfo["id"]}包含{len(anns)}个ann对象,分别是:\n{annIds}')
50. anns = coco.loadAnns(annIds)
51. 
52. coco.showAnns(anns)
53. print(f'ann{annIds[3]}对应的mask如下:')
54. mask = coco.annToMask(anns[3])
55. plt.imshow(mask); plt.axis('off')


相关文章
|
JSON 编解码 人工智能
labelme 安装使用及常见问题汇总
labelme批量使用、安装、问题解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xff in position 0 module 'labelme.utils' has no attribute 'draw_label' Polygon must have points more than 2 ModuleNotFoundError: No module named 'skimage' Inconsistent use of tabs and s
labelme 安装使用及常见问题汇总
|
安全 算法 API
支付宝支付加密规则梳理,写的太好了!
前言 支付是一个安全等级很高的场景,系统间交互的每一条数据的泄露都有可能造成及其大的损失。因此支付时系统间交互的每一
支付宝支付加密规则梳理,写的太好了!
|
10月前
|
机器学习/深度学习 人工智能
Diffusion-DPO:一种基于直接偏好优化的扩散模型对齐新方法
本文介绍了一种名为 Diffusion-DPO 的创新方法,该方法基于直接偏好优化(DPO)原理,简化了扩散模型与人类偏好的对齐过程。相比传统的基于人类反馈的强化学习(RLHF)方法,Diffusion-DPO 避免了显式奖励模型的训练,通过数学近似简化实现流程,并在处理开放词汇表场景时展现出更强的能力。实验结果表明,该方法在 Stable Diffusion 1.5 和 SDXL-1.0 等主流模型上显著提升了生成图像的质量和可控性,为未来扩散模型的发展提供了新的思路。
829 14
Diffusion-DPO:一种基于直接偏好优化的扩散模型对齐新方法
|
负载均衡 监控 网络协议
深入理解并实现负载均衡技术
【5月更文挑战第23天】本文探讨了负载均衡技术,旨在应对互联网高并发需求。负载均衡通过分散请求至多台服务器,提升系统性能和可靠性。核心是负载均衡器,其工作流程包括接收请求、解析、选择服务器、转发及返回响应。负载均衡技术分类包括反向代理(如Nginx、HAProxy)、DNS、IP(如LVS)和应用层负载均衡。实现时,以Nginx为例,需安装、配置反向代理、分发策略并启动服务。监控和优化是持续过程。负载均衡技术将持续发展,适应云计算和大数据时代。
|
XML 机器学习/深度学习 数据格式
YOLOv8训练自己的数据集+常用传参说明
YOLOv8训练自己的数据集+常用传参说明
21989 3
|
存储 自然语言处理 关系型数据库
谷粒商城笔记+踩坑(9)——上架商品spu到ES索引库
ES回顾、【查询模块】保存ES文档、【库存模块】库存量查询、【商品模块】上架单个spu
谷粒商城笔记+踩坑(9)——上架商品spu到ES索引库
|
存储 人工智能 运维
ChaosMeta for AI:混沌工程让AI稳定性更上一层楼
1.混沌工程不仅仅是技术过关的利器,更是AI系统完美运转的“防火墙”。ChaosMeta通过全方位、多层次的故障注入和演练,帮助AI系统在复杂多变的环境中维持高稳定性。 2.结合混沌工程的思想,我们不仅可以在开发阶段找到和修复问题,还能在运维阶段持续提升系统的鲁棒性。在这个高速发展的AI年代,ChaosMeta将为AI系统提供稳定性保障,让AI系统走得更远、更稳。 3.抽空试试ChaosMeta,也许下一个故障发生时,你会发现,原来一切尽在掌握。
897 0
ChaosMeta for AI:混沌工程让AI稳定性更上一层楼
|
机器学习/深度学习 计算机视觉 数据可视化
YOLOv5改进系列(0)——重要性能指标、训练结果评价及分析、影响mAP指标的因素、优化mAP的方法
YOLOv5改进系列(0)——重要性能指标、训练结果评价及分析、影响mAP指标的因素、优化mAP的方法
9899 0
YOLOv5改进系列(0)——重要性能指标、训练结果评价及分析、影响mAP指标的因素、优化mAP的方法