wider_face
Description
WIDER FACE dataset is a face detection benchmark dataset, of which images are selected from the publicly available WIDER dataset. We choose 32,203 images and label 393,703 faces with a high degree of variability in scale, pose and occlusion as depicted in the sample images. WIDER FACE dataset is organized based on 61 event classes. For each event class, we randomly select 40%/10%/50% data as training, validation and testing sets. We adopt the same evaluation metric employed in the PASCAL VOC dataset. Similar to MALF and Caltech datasets, we do not release bounding box ground truth for the test images. Users are required to submit final prediction files, which we shall proceed to evaluate.
转格式代码
'''
face_labels.py
'''
import os,cv2
import numpy as np
from os import listdir, getcwd
rootdir="./wider_face" #widerface数据集所在目录
convet2yoloformat=True
minsize2select=20#widerface中有大量小人脸,只取20以上的来训练
usepadding=True
datasetprefix="/home/zhao/Documents/face_wider/VOCdevkit/VOC2007"
def convertimgset(img_set):
imgdir=rootdir+"/WIDER_"+img_set+"/images"
gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"
imagesdir=datasetprefix+"/images"
labelsdir=datasetprefix+"/labels"
if not os.path.exists(imagesdir):
os.mkdir(imagesdir)
if convet2yoloformat:
if not os.path.exists(labelsdir):
os.mkdir(labelsdir)
img_f=open(rootdir+"/"+img_set+".txt","w")
with open(gtfilepath,'r') as gtfile:
while(True ):
filename=gtfile.readline()[:-1]
if(filename==""):
break
imgpath=imgdir+"/"+filename
if not os.path.exists(imgpath):
continue
img=cv2.imread(imgpath)
imgheight=img.shape[0]
imgwidth=img.shape[1]
maxl=max(imgheight,imgwidth)
paddingleft=(maxl-imgwidth)>>1
paddingright=(maxl-imgwidth)>>1
paddingbottom=(maxl-imgheight)>>1
paddingtop=(maxl-imgheight)>>1
saveimg=cv2.copyMakeBorder(img,paddingtop,paddingbottom,paddingleft,paddingright,cv2.BORDER_CONSTANT,value=0)
showimg=saveimg.copy()
numbbox=int(gtfile.readline())
bboxes=[]
for i in range(numbbox):
line=gtfile.readline()
line=line.split()
line=line[0:4]
if(int(line[3])<=0 or int(line[2])<=0):
continue
x=int(line[0])+paddingleft
y=int(line[1])+paddingtop
width=int(line[2])
height=int(line[3])
bbox=(x,y,width,height)
x2=x+width
y2=y+height
#face=img[x:x2,y:y2]
if width>=minsize2select or height>=minsize2select:
bboxes.append(bbox)
#cv2.rectangle(showimg,(x,y),(x2,y2),(0,255,0))
#maxl=max(width,height)
#x3=(int)(x+(width-maxl)*0.5)
#y3=(int)(y+(height-maxl)*0.5)
#x4=(int)(x3+maxl)
#y4=(int)(y3+maxl)
#cv2.rectangle(img,(x3,y3),(x4,y4),(255,0,0))
else:
pass#cv2.rectangle(showimg,(x,y),(x2,y2),(0,0,255))
#
filename=filename.replace("/","_")
if convet2yoloformat:
height=saveimg.shape[0]
width=saveimg.shape[1]
img_save_path=datasetprefix+'/images'+"/"+filename
txtpath = labelsdir + "/" + filename
txtpath=txtpath[:-3]+"txt"
if len(bboxes):
cv2.imwrite(img_save_path, showimg)
ftxt=open(txtpath,'w')
for i in range(len(bboxes)):
bbox=bboxes[i]
xcenter=(bbox[0]+bbox[2]*0.5)/width
ycenter=(bbox[1]+bbox[3]*0.5)/height
wr=bbox[2]*1.0/width
hr=bbox[3]*1.0/height
txtline="0 "+str(xcenter)+" "+str(ycenter)+" "+str(wr)+" "+str(hr)+"\n"
ftxt.write(txtline)
ftxt.close()
img_f.close()
def convertdataset():
img_sets=["train",'val']
for img_set in img_sets:
convertimgset(img_set)
def img2txt():
train_file_txt = ''
val_file_txt = ''
train_file = '/home/zhao/Documents/face_wider/VOCdevkit/VOC2007/train.txt'
val_file = '/home/zhao/Documents/face_wider/VOCdevkit/VOC2007/val.txt'
path = '/home/zhao/Documents/face_wider/VOCdevkit/VOC2007/images'
img_list = os.listdir(path)
for i in img_list[:int(0.85*int(len(img_list)))]:
train_file_txt = train_file_txt + path+ '/' + i + '\n'
for i in img_list[int(0.85*int(len(img_list))):]:
val_file_txt = val_file_txt + path+ '/' + i + '\n'
with open(train_file, 'w') as outfile:
outfile.write(train_file_txt)
with open(val_file, 'w') as outfile:
outfile.write(val_file_txt)
if __name__=="__main__":
convertdataset()
img2txt()
第二次用这代码,改了下
import os,cv2
import numpy as np
from os import listdir, getcwd
"""
train:val = 0.85:0.15
wider_face/
---/wider_face_split
---/WIDER_train
---/images
---/labels
---/wider_face2yolo.py
---/train.txt
---/val.txt
"""
train_file = '/home/zhao/Documents/wider_face/train.txt'
val_file = '/home/zhao/Documents/wider_face/val.txt'
path = '/home/zhao/Documents/wider_face/images'#复杂全部图片到此文件夹
rootdir="/home/zhao/Documents/wider_face" #widerface解压后数据集所在目录
datasetprefix="/home/zhao/Documents/wider_face"#把数据集images和labels复制到那个文件夹
#以上是要改的内容
convet2yoloformat=False
minsize2select=20#widerface中有大量小人脸,只取20以上的来训练
def convertimgset(img_set):
process = 0
imgdir=rootdir+"/WIDER_"+img_set+"/images/"
gtfilepath=rootdir+"/wider_face_split/wider_face_"+img_set+"_bbx_gt.txt"
imagesdir=datasetprefix+"/images"
labelsdir=datasetprefix+"/labels"
if not os.path.exists(imagesdir):
os.mkdir(imagesdir)
if not os.path.exists(labelsdir):
os.mkdir(labelsdir)
img_f=open(rootdir+"/"+img_set+".txt","w")
with open(gtfilepath,'r') as gtfile:
while(True ):
process+=1
print("process:"+str(process)+"\n")
filename=gtfile.readline()[:-1]
if(filename==""):
break
imgpath=imgdir+"/"+filename
if not os.path.exists(imgpath):
continue
img=cv2.imread(imgpath)
imgheight=img.shape[0]
imgwidth=img.shape[1]
maxl=max(imgheight,imgwidth)
paddingleft=(maxl-imgwidth)>>1
paddingright=(maxl-imgwidth)>>1
paddingbottom=(maxl-imgheight)>>1
paddingtop=(maxl-imgheight)>>1
saveimg=cv2.copyMakeBorder(img,0,0,0,0,cv2.BORDER_CONSTANT,value=0)
#cv2.imshow("img",saveimg)
#cv2.waitKey(0)
showimg=saveimg.copy()
#cv2.imwrite("./test.jpg", saveimg)
numbbox=int(gtfile.readline())
bboxes=[]
for i in range(numbbox):
line=gtfile.readline()
line=line.split()
line=line[0:4]
if(int(line[3])<=0 or int(line[2])<=0):
continue
x=int(line[0])+0
y=int(line[1])+0
width=int(line[2])
height=int(line[3])
bbox=(x,y,width,height)
x2=x+width
y2=y+height
#face=img[x:x2,y:y2]
if width>=minsize2select or height>=minsize2select:
bboxes.append(bbox)
#cv2.rectangle(showimg,(x,y),(x2,y2),(0,255,0))
#maxl=max(width,height)
#x3=(int)(x+(width-maxl)*0.5)
#y3=(int)(y+(height-maxl)*0.5)
#x4=(int)(x3+maxl)
#y4=(int)(y3+maxl)
#cv2.rectangle(img,(x3,y3),(x4,y4),(255,0,0))
else:
pass#cv2.rectangle(showimg,(x,y),(x2,y2),(0,0,255))
#
filename=filename.replace("/","_")
if convet2yoloformat:
height=saveimg.shape[0]
width=saveimg.shape[1]
img_save_path=datasetprefix+'/images'+"/"+filename
txtpath = labelsdir + "/" + filename
txtpath=txtpath[:-3]+"txt"
if len(bboxes):
cv2.imwrite(img_save_path, showimg)
ftxt=open(txtpath,'w')
for i in range(len(bboxes)):
bbox=bboxes[i]
xcenter=(bbox[0]+bbox[2]*0.5)/width
ycenter=(bbox[1]+bbox[3]*0.5)/height
wr=bbox[2]*1.0/width
hr=bbox[3]*1.0/height
txtline="0 "+str(xcenter)+" "+str(ycenter)+" "+str(wr)+" "+str(hr)+"\n"
ftxt.write(txtline)
ftxt.close()
img_f.close()
def convertdataset():
img_sets=["train",'val']
for img_set in img_sets:
convertimgset(img_set)
def img2txt():
train_file_txt = ''
val_file_txt = ''
img_list = os.listdir(path)
for i in img_list[:int(0.85*int(len(img_list)))]:
train_file_txt = train_file_txt + path+ '/' + i + '\n'
for i in img_list[int(0.85*int(len(img_list))):]:
val_file_txt = val_file_txt + path+ '/' + i + '\n'
with open(train_file, 'w') as outfile:
outfile.write(train_file_txt)
with open(val_file, 'w') as outfile:
outfile.write(val_file_txt)
if __name__=="__main__":
convertdataset()
img2txt()
测试
VOC
复制下面链接,使用迅雷下载,速度很快
http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
前两个分别是2012和2007年的训练验证集,第三个是2007的测试集。通常将前两个作为训练验证。最后一个作为测试集。
# coding:utf-8
from __future__ import print_function
import os
import random
import glob
import xml.etree.ElementTree as ET
'''
train:val:test = 7:1:2
./images/*.jpg
./labels_voc/*.xml
./labels/*.txt
./voc_classes.names
./voc2yolo.py
./train.txt
./val.txt
./test.txt
'''
def xml_reader(filename):
""" Parse a PASCAL VOC xml file """
tree = ET.parse(filename)
size = tree.find('size')
width = int(size.find('width').text)
height = int(size.find('height').text)
objects = []
for obj in tree.findall('object'):
obj_struct = {}
obj_struct['name'] = obj.find('name').text
bbox = obj.find('bndbox')
obj_struct['bbox'] = [int(bbox.find('xmin').text),
int(float(bbox.find('ymin').text)),
int(bbox.find('xmax').text),
int(bbox.find('ymax').text)]
objects.append(obj_struct)
return width, height, objects
def voc2yolo(filename):
classes_dict = {}
with open(ROOT+"/voc_classes.names") as f:
for idx, line in enumerate(f.readlines()):
class_name = line.strip()
classes_dict[class_name] = idx
width, height, objects = xml_reader(filename)
lines = []
for obj in objects:
x, y, x2, y2 = obj['bbox']
class_name = obj['name']
label = classes_dict[class_name]
cx = (x2 + x) * 0.5 / width
cy = (y2 + y) * 0.5 / height
w = (x2 - x) * 1. / width
h = (y2 - y) * 1. / height
line = "%s %.6f %.6f %.6f %.6f\n" % (label, cx, cy, w, h)
lines.append(line)
txt_name = filename.replace(".xml", ".txt").replace("labels_voc", "labels")
with open(txt_name, "w") as f:
f.writelines(lines)
def get_image_list(image_dir, suffix=['jpg', 'jpeg', 'JPG', 'JPEG', 'png']):
'''get all image path ends with suffix'''
if not os.path.exists(image_dir):
print("PATH:%s not exists" % image_dir)
return []
imglist = []
for root, sdirs, files in os.walk(image_dir):
if not files:
continue
for filename in files:
filepath = os.path.join(root, filename) + "\n"
if filename.split('.')[-1] in suffix:
imglist.append(filepath)
return imglist
# def imglist2file(imglist):
# random.shuffle(imglist)
# train_list = imglist[:-100]
# valid_list = imglist[-100:]
# with open("train.txt", "w") as f:
# f.writelines(train_list)
# with open("valid.txt", "w") as f:
# f.writelines(valid_list)
def imglist2file(imglist):
random.shuffle(imglist)
all_len = len(imglist)
train_len = 7*all_len//10
val_len = train_len+all_len // 10
with open("train.txt", "w") as f:
f.writelines(imglist[:train_len])
with open("val.txt", "w") as f:
f.writelines(imglist[train_len:val_len])
with open("test.txt", "w") as f:
f.writelines(imglist[val_len:])
if __name__ == "__main__":
ROOT = os.getcwd()
xml_path_list = glob.glob(ROOT+"/labels_voc/*.xml")
for xml_path in xml_path_list:
voc2yolo(xml_path)
imglist = get_image_list(ROOT+"/images")
imglist2file(imglist)