問題描述
我正在開發一個對象檢測模型來使用 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 的基本信息.我們現在可以使用 requests
來 GET
圖像并將它們寫入本地文件夾:
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 的特定部分?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!