fiftyOne

address: https://towardsdatascience.com/googles-open-images-now-easier-to-download-and-evaluate-with-fiftyone-615ce0482c02

GitHub - voxel51/fiftyone: The open-source tool for building high-quality datasets and computer vision models

另一个数据查找网站 Data Search | Bifrost Data Search

如果想可视化界面:

在pip install fiftyone-desktop 后,在安装目录大概是site-packeges 下找到 安装文件,双击安装!

import fiftyone as fo
import fiftyone.zoo as foz
打开可视化界面
dataset = foz.load_zoo_dataset("imagenet-sample")

session = fo.launch_app(dataset)
session.wait()
打印所以数据集:
print(foz.list_zoo_datasets())
按数据集按类别下载数据:
if __name__ == '__main__':
    dataset_test = foz.load_zoo_dataset(
        "open-images-v6",
        split="train",
        classes=["Cat furniture"],
        max_samples=10000,
        shuffle=True,
        only_matching=True,
        label_types=["detections"],  # 指定下载目标检测的类型,detections,
        dataset_dir="/get_my_data/Cat furniture",# 保存的路径
        num_workers=6,  # 指定工作进程数
    )

下载好后,解析detections.csv文件,生成detections_ces.csv 标注文件内容。
import csv
from tqdm import tqdm
import os
# 标注文件路径
csv_file_path = '/home/yao/Documents/51/sadal/labels/detections.csv'
# 图像文件夹路径
images_file_path = r"/home/yao/Documents/51/sadal/data"
images_name = os.listdir(images_file_path)
images_name = [x.split(".")[0] for x in images_name]

# 保存标注文件路径
data_annotation_csv = r"/home/yao/Documents/51/sadal/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")

可视化labels,以及转化为yolo格式:

import csv
import cv2
import os
import random
# 当前子数据集的标注文件地址
annotation_path = "/home/yao/Documents/51/4cls/labels/detections_ces.csv"
# 图片文件夹路径
image_path = "/home/yao/Documents/51/4cls/data"
# 类别缩写文件
class_name_csv = "/home/yao/Documents/51/4cls/metadata/classes.csv"
savePath = '/home/yao/Documents/51/4cls/label'
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)
classes = ['Bottle', 'Footwear', 'Houseplant', 'Person','Man','Woman','Boy','Girl','Human body']
names = ['bottles', 'clothing', 'wires','shoes','trash_cans','plants','person']

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)
    fileName = os.path.join(savePath,image_name+'.txt')

    f = open(fileName,'w')
    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
            cx = (float(image_annotation[5])  - float(image_annotation[4]))/2 + float(image_annotation[4])
            cy = (float(image_annotation[7]) - float(image_annotation[6]))/2 + float(image_annotation[6])
            w = float(image_annotation[5])  - float(image_annotation[4])
            h = float(image_annotation[7]) - float(image_annotation[6])
            class_name = classes_dict[image_annotation[2]]
            if class_name in classes:
                if class_name == 'Bottle':
                    clsID = 0
                if class_name == 'Footwear':
                    clsID = 3
                if class_name == 'Houseplant':
                    clsID = 5
                if class_name == 'Person':
                    clsID = 6
                if class_name == 'Man':
                    clsID = 6

                if class_name == 'Woman':
                    clsID = 6
                    # print('2222')
                if class_name == 'Boy':
                    clsID = 6
                if class_name == 'Human body':
                    clsID = 6
                f.write("{} {} {} {} {}\n".format(clsID,cx,cy,w,h))

    f.close()
            # 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)
            ##
            # writePath = os.path.join('/home/yao/Documents/51/Bottle/anno',image_name+'.jpg')
            # cv2.imwrite(writePath,image_src)
    # cv2.imshow("src",image_src)
    # cv2.waitKey(0)

你可能感兴趣的:(目标检测,fiftyone)