使用FiftyOne下载Open Images指定类别数据

文章目录

  • 前言
  • 一、FiftyOne、Open Images官网链接
  • 二、步骤
    • 1.安装FiftyOne
    • 2.下载Open Images部分类别数据
    • 3.从detections.csv中获取下载图片的标注文件
    • 4.可视化图片与标注信息
    • 5.将子数据集制作成COCO数据集格式或VOC数据集格式送入网络训练


前言

实际需求中往往是检测某种类别或某几类的目标,不同检测整个数据集中的所有目标,因此使用FiftyOne下载数据集中某类数据。


一、FiftyOne、Open Images官网链接

FiftyOne:https://voxel51.com/docs/fiftyone/index.html
Open Images:https://storage.googleapis.com/openimages/web/index.html

二、步骤

1.安装FiftyOne

pip install fiftyone

测试代码:

import fiftyone as fo
import fiftyone.zoo as foz

# 载入quickstart数据集
dataset = foz.load_zoo_dataset("quickstart")
# 可视化数据集
session = fo.launch_app(dataset)

2.下载Open Images部分类别数据

import fiftyone.zoo as foz

dataset = foz.load_zoo_dataset(
    "open-images-v6",
    split="train",  # 下载训练集
    label_types=["detections"],  # 下载目标检测标注文件
    classes=["Person", "Truck", "Car"],  # 下载数据集中的某几类别
    max_samples=50,  # 下载图片数目
    only_matching=True,  # 只下载匹配到类别的图片
    dataset_dir=".",  # 下载到当前目录
)

下载结果:

train
	--data
		--下载下来的图片
	--labels
		--detections.csv Open Images训练数据集中所有图片的检测标注文件
	--metedata
		--classes.csv Open Images所有种类目标的缩写
		--hierarchy.json 类别之间的关系
		--image_ids.csv 图片的来源信息

3.从detections.csv中获取下载图片的标注文件

import csv
from tqdm import tqdm
import os
# 标注文件路径
csv_file_path = r"D:\datasets\Open_Images\train\labels\detections.csv"
# 图像文件夹路径
images_file_path = r"D:\datasets\Open_Images\train\data"
images_name = os.listdir(images_file_path)
images_name = [x.split(".")[0] for x in images_name]

# 保存标注文件路径
data_annotation_csv = r"D:\datasets\Open_Images\train\labels\detections_ces.csv"
with open(csv_file_path, 'r', encoding='utf-8') as f:
    with open(data_annotation_csv, "w", encoding='utf-8') as ff:
        csv_f = csv.reader(f)
        bar = tqdm(csv_f)
        for row in bar:
            if row[0] in images_name:
                # print("get image {}".format(row[0]))
                for index in range(len(row)):
                    ff.write(row[index])
                    if (index != (len(row) - 1)):
                        ff.write(",")
                ff.write("\n")

4.可视化图片与标注信息

import csv
import cv2
import os
import random
# 当前子数据集的标注文件地址
annotation_path = r"D:\datasets\Open_Images\train\labels\detections_ces.csv"
# 图片文件夹路径
image_path = r"D:\datasets\Open_Images\train\data"
# 类别缩写文件
class_name_csv = r"D:\datasets\Open_Images\train\metadata\classes.csv"

annotation_data = []
with open(annotation_path,"r") as f:
    annotation_files = csv.reader(f)
    for data in annotation_files:
        annotation_data.append(data)
images_name_list = os.listdir(image_path)
images_path_list = [os.path.join(image_path,image_name) for image_name in images_name_list]

classes_dict = {}
with open(class_name_csv,"r") as f:
    annotation_files = csv.reader(f)
    for data in annotation_files:
        classes_dict[data[0]] = data[1]
print(classes_dict)

import numpy as np
np.random.seed(12)
np.random.shuffle(images_path_list)
np.random.seed(12)
np.random.shuffle(images_name_list)

for i, image_path in enumerate(images_path_list):
    image_src = cv2.imread(image_path)
    image_name = images_name_list[i].split(".")[0]
    image_row = image_src.shape[0]
    image_col = image_src.shape[1]
    #print(image_row,image_col)
    for image_annotation in annotation_data:
        if image_annotation[0] == image_name:
            x = float(image_annotation[4]) * image_col
            x2 = float(image_annotation[5]) * image_col
            y = float(image_annotation[6]) * image_row
            y2 = float(image_annotation[7]) * image_row
            class_name = classes_dict[image_annotation[2]]
            cv2.rectangle(image_src,(int(x),int(y)),(int(x2),int(y2)),[0,255,0],2)
            cv2.putText(image_src,class_name,(int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 255), 2)
    cv2.imshow("src",image_src)
    cv2.waitKey(1000)

5.将子数据集制作成COCO数据集格式或VOC数据集格式送入网络训练

你可能感兴趣的:(深度学习,目标检测,计算机视觉)