首页 > 解决方案 > FDDB 评估代码

问题描述

我正在研究 opencv 和 dlib,以便在大学项目中使用人脸检测器,而且我对机器学习和计算机视觉的整个事情真的很陌生。如何使用FDDB的评估代码来评估我的人脸检测代码?我正在使用 dlib 的 CNN 方法从图像中检测人脸。

import cv2
import dlib

image = cv2.imread('..\\pessoas\\beatles.jpg')

detector = dlib.cnn_face_detection_model_v1("..\\mmods\\mmod_human_face_detector.dat")
detectedFaces = detector(image)

for face in detectedFaces:
    l, t, r, b, c = (int(face.rect.left()), int(face.rect.top()), int(face.rect.right()), int(face.rect.bottom()),
                 face.confidence)
    cv2.rectangle(image, (l, t), (r, b), (255, 0, 0), 2)

cv2.imshow("CNN Detector", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

如您所见,代码非常简单,但我必须计算精度、召回率和 F1 分数来绘制 ROC 曲线,而且我还不知道该怎么做,项目github上的自述文件没有没有帮助。

标签: opencvface-detectionevaluationdlibevaluate

解决方案


至于我在 ubuntu16 中,我必须通过以下步骤来完成:

  1. 下载你检测到人脸并得到检测结果的fddb原始图像数据集。你可以在这里下载。这我的目录: 在此处输入图像描述

  2. 将所有图像文件路径加入一个 txt 文件,并将所有 fddb 注释加入一个 txt 文件。你可以在这里下载所有文件

在此处输入图像描述

对我而言,我将所有内容FDDB-FOLD-%d.txt移至目录all_file_path,然后将它们加入到一个文件中cat * > filePath.txt

在此处输入图像描述

加入所有FDDB-fold-%d-ellipseList.txt到一个 txt 通过cat *ellipse*.txt > annotFile.txt

请注意,您可能不需要创建它,因为runEvaluate.pl在运行过程中已经为您完成了。

3.创建FDDB evalute exe,在这里下载源代码 然后编译,可以改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

到make文件。

  1. 评估,你可以使用runEvaluate.pl评估它,但至于我(ubuntu16),我不能直接运行它。

    4.1 更改GUNPLOT路径(你应该gnuplot先安装使用它来创建ROC图像)

在此处输入图像描述

4.2 我使用矩形检测模型,所以我$detFormat改为0。

my $detFormat = 0; # 0: rectangle, 1: ellipse 2: pixels

4.3 所有图片的相对路径:

my $listFile ="/home/xy/face_sample/evaluation/compareROC/FDDB-folds/filePath.txt";

4.4 所有图像标注

my $annotFile = "/home/xy/face_sample/evaluation/compareROC/FDDB-folds/annotFile.txt";

4.5 你要生成的roc文件(由evaluate exe创建):

my $gpFile ="/home/xy/face_sample/evaluation/compareROC/createROC.p";

4.6 你的检测文件(我稍后会给出如何创建它)

my $detFile ="/home/xy/face_sample/evaluation/compareROC/detDir/fddb_rect_ret1.txt";

It’s content like that:

在此处输入图像描述

'runEvaluate.pl' 有一些错误,将执行评估更改为以下内容:

system($evaluateBin, "-a", $annotFile, "-d", $detFile, "-f", $detFormat, "-i", $imDir, "-l", $listFile, "-r", $detDir, "-z", ".jpg");

您也可以使用命令来检查它:

在此处输入图像描述

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

使用python创建fddb评估txt文件:

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

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)

# 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


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)

运行上述代码后,您可以创建 FDDB 结果: 在此处输入图像描述

注意:当您在windows中创建上述txt时,如果您在ubuntu中测试它可能会出现以下错误Incompatible annotation and detection files. See output specifications

在此处输入图像描述

只需将内容复制到一个新的 txt 文件(在 ubuntu 中创建)即可解决。

结果如下:

在此处输入图像描述

一些技巧:

  1. 可以看到runEvaluate.pl并不难,上面的改动可能不需要。你也可以修改一些变量runEvaluate.pl,比如$GNUPLOT$imDir等等。添加"-z", ".jpg"到系统($evaluateBin,“-a”,$annotFile,“-d”,$detFile,“-f”,$detFormat,“-i”,$imDir,“-l”,$listFile,“-r ", $detDir);

    系统($evaluateBin,“-a”,$annotFile,“-d”,$detFile,“-f”,$detFormat,“-i”,$imDir,“-l”,$listFile,“-r”, $detDir,“-z”,“.jpg”);

  2. 您还可以阅读evaluate代码(主要evaluate.cpp是易于理解的代码),因此您将对如何评估它有深入的了解。


推荐阅读