import fiftyone.zoo as foz
# To download the COCO dataset for only the "person" and "car" classes
dataset = foz.load_zoo_dataset(
"coco-2017",
split="train",
label_types=["detections", "segmentations"],
classes=["person", "car"],
# max_samples=50,
)
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)
# 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)
现在有一个名为
fiftyone
的包,您可以使用它下载MS COCO数据集并仅获取特定类的注释。有关安装的更多信息,请访问https://github.com/voxel51/fiftyone#installation安装软件包后,只需运行以下程序即可获得“person”和“car”类:
如果需要,可以注释掉最后一个选项以设置最大样本大小。此外,您可以将“train”拆分更改为“validation”,以获得验证拆分
要可视化下载的数据集,只需运行以下操作:
如果要在要加载的数据的相同函数调用中下载拆分“训练”、“验证”和“测试”,可以执行以下操作:
要从特定类别下载图像,可以使用COCO API。这里有一个demo笔记本,介绍了这个和其他用法。整个过程如下:
下面是一个示例,介绍如何下载包含
person
的图像子集并将其保存在本地文件中:返回包含图像及其url基本信息的词典列表。我们现在可以使用
requests
来GET
图像并将其写入本地文件夹:请注意,这将保存指定类别中的所有图像。因此,您可能希望将
images
列表切片到第一个n
据我个人所知,如果你只谈论COCO数据集,我认为他们没有“船舶”的分类。他们拥有的最接近的类别是“船”。下面是检查可用类别的链接:http://cocodataset.org/#overview
顺便说一句,也有船属于船类
如果您只想选择某个特定COCO类别的图像,您可能需要执行以下操作(从COCO的官方演示中拍摄和编辑):
相关问题 更多 >
编程相关推荐