如何下载 Coco 数据集的特定部分?

新手上路,请多包涵

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

原文由 Shobhit Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
1 个回答

要从特定类别下载图像,您可以使用 COCO API 。这是一个 演示 笔记本,介绍了这个和其他用法。整体流程如下:

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

 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 图像并将它们写入本地文件夹:

 # 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

原文由 yatu 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏