【问题标题】:FDDB evaluation codeFDDB 评估代码
【发布时间】:2018-11-05 15:42:40
【问题描述】:

我正在研究 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 没有帮助。

【问题讨论】:

    标签: opencv face-detection evaluation dlib evaluate


    【解决方案1】:

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

    1. 下载fddb原始图像数据集,你检测到人脸并得到检测结果。你可以下载它here。这是我的目录:

    2. 将所有图片文件路径加入一个txt文件,将所有fddb注解加入一个txt文件。 您可以下载所有文件here

    对我来说,我将所有FDDB-FOLD-%d.txt 移动到目录all_file_path,然后通过cat * > filePath.txt 将它们加入到一个文件中

    将所有FDDB-fold-%d-ellipseList.txt 加入cat *ellipse*.txt > annotFile.txt 的一个txt

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

    3.创建FDDB evalute exe,这里下载源代码here 然后编译,可以改makefile,看原因here,添加

    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);

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

    2. 您也可以阅读evaluate代码(主要是容易理解的evaluate.cpp),这样您就可以深入了解如何评估它。

    【讨论】:

      【解决方案2】:

      你能解释一下你所处的步骤吗?

      您需要从以下位置下载标记数据: http://vis-www.cs.umass.edu/fddb/ 上面写着:下载数据库

      之后需要下载结果源代码: http://vis-www.cs.umass.edu/fddb/results.html

      然后你需要修改你的程序,使输出看起来像这样:

      2002/08/11/big/img_591
      1
      191 88 164 163 0
      2002/08/26/big/img_265
      3
      52 39 95 95 0
      282 59 114 114 0
      

      首先是图像的名称, 然后是该图像中的面孔数量, 然后每个面的坐标并重复...

      我建议您在 linux 上构建 评估,因为它要容易得多(至少对我来说是这样)。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2013-02-01
        • 1970-01-01
        • 2012-10-25
        • 2014-07-26
        • 2018-12-11
        • 2011-01-25
        • 1970-01-01
        • 2016-12-23
        • 2013-03-25
        相关资源
        最近更新 更多