下载Open Images Dataset v4数据集
如果你想要构建自己的物体检测器,但没有足够的图像来训练模型。试试Open Images Dataset v4
数据集吧。该数据集OIDv4具有600个类和1,700,000张具有相关边界框的图像可供使用。
比如说我想下载广告牌数据集用做目标检测的训练集,只需要下载对应的Billboard类数据集就可以了。
python3 main.py downloader --classes Billboard --type_csv validation
GitHub项目地址:OIDv4_ToolKit
将OIDv4数据集转成VOC格式
很多目标检测模型训练的时候都是用的VOC格式,当然我们也可以自己写个脚本把OIDv4的label文件夹里面的bbox信息转成我们需要的格式,但换个模型可能又得重新写个脚本了。所以为了方便,我们可以直接将OIDv4的label文件直接转成VOC数据集的XML格式。
python3 OIDv4_to_VOC.py --sourcepath Dataset/train/Billboard --dest_path Dataset/train/Annotation/Billboard
GitHub项目地址:OIDv4_to_VOC
其中,OIDv4_to_VOC.py
代码如下:
from xml.etree.ElementTree import Element, SubElement, Comment
import xml.etree.cElementTree as ET
#from ElementTree_pretty import prettify
import cv2
import os
from pathlib import Path
from shutil import move
import argparse
parser = argparse.ArgumentParser(description = 'Convert OIDV4 dataset to VOC XML format')
parser.add_argument('--sourcepath',type = str, default = 'dataset/', help ='Path of class to convert')
parser.add_argument('--dest_path',type=str, required=True, default='Annotation/',help='Path of Dest XML files')
args = parser.parse_args()
ids = []
for file in os.listdir(args.sourcepath): #Save all images in a list
filename = os.fsdecode(file)
if filename.endswith('.jpg'):
ids.append(filename[:-4])
for fname in ids:
myfile = os.path.join(args.dest_path,fname +'.xml')
myfile = Path(myfile)
if not myfile.exists(): #if file is not existing
txtfile = os.path.join(args.sourcepath, 'Label', fname + '.txt') #Read annotation of each image from txt file
f = open(txtfile,"r")
imgfile = os.path.join(args.sourcepath, fname +'.jpg')
img = cv2.imread(imgfile, cv2.IMREAD_UNCHANGED) #Read image to get image width and height
top = Element('annotation')
child = SubElement(top,'folder')
child.text = 'open_images_volume'
child_filename = SubElement(top,'filename')
child_filename.text = fname +'.jpg'
child_path = SubElement(top,'path')
child_path.text = '/mnt/open_images_volume/' + fname +'.jpg'
child_source = SubElement(top,'source')
child_database = SubElement(child_source, 'database')
child_database.text = 'Unknown'
child_size = SubElement(top,'size')
child_width = SubElement(child_size,'width')
child_width.text = str(img.shape[1])
child_height = SubElement(child_size,'height')
child_height.text = str(img.shape[0])
child_depth = SubElement(child_size,'depth')
if len(img.shape) == 3:
child_depth.text = str(img.shape[2])
else:
child_depth.text = '3'
child_seg = SubElement(top, 'segmented')
child_seg.text = '0'
for x in f: #Iterate for each object in a image.
x = list(x.split())
child_obj = SubElement(top, 'object')
child_name = SubElement(child_obj, 'name')
child_name.text = x[0] #name
child_pose = SubElement(child_obj, 'pose')
child_pose.text = 'Unspecified'
child_trun = SubElement(child_obj, 'truncated')
child_trun.text = '0'
child_diff = SubElement(child_obj, 'difficult')
child_diff.text = '0'
child_bndbox = SubElement(child_obj, 'bndbox')
child_xmin = SubElement(child_bndbox, 'xmin')
child_xmin.text = str(int(float(x[1]))) #xmin
child_ymin = SubElement(child_bndbox, 'ymin')
child_ymin.text = str(int(float(x[2]))) #ymin
child_xmax = SubElement(child_bndbox, 'xmax')
child_xmax.text = str(int(float(x[3]))) #xmax
child_ymax = SubElement(child_bndbox, 'ymax')
child_ymax.text = str(int(float(x[4]))) #ymax
tree = ET.ElementTree(top)
save = fname+'.xml'
tree.write(save)
move(fname+'.xml', myfile)