本文主要内容如下:
1. ubuntu16环境下,如何使用FDDB评价人脸检测的效果,以及对应的ROC曲线的生成
2. 使用python
生成FDDB对比的文件。
在测试人脸检测效果前,最好看下论文FDDB: A Benchmark for Face Detection in Unconstrained Settings
,以及FDDB主页,了解人脸评估的基本原理
1. 数据集准备
1.1 在 http://tamaraberg.com/faceDataset/originalPics.tar.gz 下载原始的FDDB人脸数据集
1.2 在http://vis-www.cs.umass.edu/fddb/FDDB-folds.tgz 下载原始的FDDB人脸书籍的标注(使用椭圆的方式),以及所有的文件名。
将FDDB-FOLD-%d.txt
合成一个文件: cat * > filePath.txt
将所有的标注文件FDDB-fold-%d-ellipseList.txt
合成一个文件 cat *ellipse*.txt > annotFile.txt
2. 生成FDDB evaluate
可执行文件
2.1 在 http://vis-www.cs.umass.edu/fddb/evaluation.tgz 下载源码, 编译的时候要注意 http://vis-www.cs.umass.edu/fddb/faq.html ,makefile要加上:
INCS = -I/usr/local/include/opencv
LIBS = -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui
-lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d
-lopencv_objdetect -lopencv_contrib -lopencv_legacy
3. Evaluate & runEvaluate.pl
部分修改
3.1 GUNPLOT
变量修改(gnuplot路径),确保已经安装gnuplot
3.2 $detFormat
人脸检测的的类型,椭圆还是矩形,我用矩形评价,因此变量修改:
my $detFormat = 0; # 0: rectangle, 1: ellipse 2: pixels
3.3 $detFile
自己的检测文件
my $detFile =
"/home/xy/face_sample/evaluation/compareROC/detDir/fddb_rect_ret1.txt";
3.4 runEvaluate.pl
,执行语句修改,主要是添加"-z", ".jpg"
:
system($evaluateBin, "-a", $annotFile, "-d", $detFile, "-f", $detFormat, "-i", $imDir, "-l", $listFile, "-r", $detDir, "-z", ".jpg");
3.5 evaluate
使用验证:
具体命令如下:
xy@xy:~/face_sample/evaluation/compareROC$ ./evaluate \
> -a /home/xy/face_sample/evaluation/compareROC/FDDB-folds/annotFile.txt \
> -d /home/xy/face_sample/evaluation/compareROC/detDir/fddb_rect_ret1.txt \
> -f 0 \
> -i /home/xy/face_sample/evaluation/compareROC/originalPics/ \
> -l /home/xy/face_sample/evaluation/compareROC/FDDB-folds/filePath.txt \
> -r /home/xy/face_sample/evaluation/compareROC/detDir/ \
> -z .jpg
4. 使用opencv 人脸检测算法 + python 生成FDDB待评价的文件
4.1 读取所有的图片的相对路径:
def get_img_relative_path():
"""
:return: ['2002/08/11/big/img_344', '2002/08/02/big/img_473', ......]
"""
f_name = 'E:/face_rec/face__det_rec_code/face_det/FDDB-folds/all_img_files.txt'
lst_name = open(f_name).read().split('\n')
return lst_name
4.2 最终的结果写到txt文件中去
def write_lines_to_txt(lst):
# lst = ['line1', 'line2', 'line3']
f_path = 'fddb_rect_ret.txt'
with open(f_path, 'w') as fp:
for line in lst:
fp.write("%s\n" % line)
4.3 opencv中的人脸检测(这里可以改为你自己的人脸检测算法)
# For example use opencv to face detection
def detect_face_lst(img):
"""
:param img: opencv image
:return: face rectangles [[x, y, w, h], ..........]
"""
m_path = 'D:/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier(m_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
return faces
4.4 主要的生成代码如下:
def generate_fddb_ret():
# The directory from which we get the test images from FDDB
img_base_dir = 'E:/face_rec/face__det_rec_code/face_det/originalPics/'
# All the images relative path, like '['2002/08/11/big/img_344', '2002/08/02/big/img_473', ......]'
lst_img_name = get_img_relative_path()
# Store detect result, like:
# ['2002/08/11/big/img_344', '1', '10 10 50 50 1', .............]
lst_write2_fddb_ret = []
try:
for img_name in lst_img_name:
img_full_name = img_base_dir + img_name + '.jpg'
img = cv2.imread(img_full_name)
if img == None:
print 'error %s not exists, can not generate complete fddb evaluate file' % img_full_name
return -1
lst_face_rect = detect_face_lst(img)
# append img name like '2002/08/11/big/img_344'
lst_write2_fddb_ret.append(img_name)
face_num = len(lst_face_rect)
# append face num, note if no face 0 should be append
lst_write2_fddb_ret.append(str(face_num))
if face_num > 0:
# append each face rectangle x y w h score
for face_rect in lst_face_rect:
# append face rectangle x, y, w, h score
# note: opencv hava no confidence so use 1 here
s_rect = " ".join(str(item) for item in face_rect) + " 1"
lst_write2_fddb_ret.append(s_rect)
except Exception as e:
print 'error %s , can not generate complete fddb evaluate file' % e
return -1
# Write all the result to txt for FDDB evaluation
write_lines_to_txt(lst_write2_fddb_ret)