如何下载 Coco Dataset 的特定部分?

How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
本文介绍了如何下载 Coco Dataset 的特定部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个对象检测模型来使用 YOLO 检测船舶.我想使用 COCO 数据集.有没有办法只下载带有注释的图像?

I am developing an object detection model to detect ships using YOLO. I want to use the COCO dataset. Is there a way to download only the images that have ships with the annotations?

推荐答案

要下载特定类别的图片,可以使用 COCO API.这是一个 demo 笔记本,通过这个和其他用法.整体流程如下:

To download images from a specific category, you can use the COCO API. Here's a demo notebook going through this and other usages. The overall process is as follows:

  • 安装pycocotools
  • 从 COCO 数据集
  • 下载其中一个注释 json
  • Install pycocotools
  • Download one of the annotations jsons from the COCO dataset

下面是一个示例,说明我们如何下载包含 person 的图像子集并将其保存在本地文件中:

Now here's an example on how we could download a subset of the images containing a person and saving it in a local file:

from pycocotools.coco import COCO
import requests

# instantiate COCO specifying the annotations json path
coco = COCO('...path_to_annotations/instances_train2014.json')
# Specify a list of category names of interest
catIds = coco.getCatIds(catNms=['person'])
# Get the corresponding image ids and images using loadImgs
imgIds = coco.getImgIds(catIds=catIds)
images = coco.loadImgs(imgIds)

它返回一个字典列表,其中包含有关图像及其 url 的基本信息.我们现在可以使用 requestsGET 图像并将它们写入本地文件夹:

Which returns a list of dictionaries with basic information on the images and its url. We can now use requests to GET the images and write them into a local folder:

# Save the images into a local folder
for im in images:
    img_data = requests.get(im['coco_url']).content
    with open('...path_saved_ims/coco_person/' + im['file_name'], 'wb') as handler:
        handler.write(img_data)

请注意,这将保存指定类别中的所有张图片.因此,您可能希望将 images 列表切片为第一个 n.

Note that this will save all images from the specified category. So you might want to slice the images list to the first n.

这篇关于如何下载 Coco Dataset 的特定部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reading *.mhd/*.raw format in python(在 python 中读取 *.mhd/*.raw 格式)
Count number of cells in the image(计算图像中的单元格数)
How to detect paragraphs in a text document image for a non-consistent text structure in Python OpenCV(如何在 Python OpenCV 中检测文本文档图像中的段落是否存在不一致的文本结构)
How to get the coordinates of the bounding box in YOLO object detection?(YOLO物体检测中如何获取边界框的坐标?)
Divide an image into 5x5 blocks in python and compute histogram for each block(在 python 中将图像划分为 5x5 块并计算每个块的直方图)
Extract cow number from image(从图像中提取奶牛编号)