代码之家  ›  专栏  ›  技术社区  ›  Erick

FDDB评估码

  •  1
  • Erick  · 技术社区  · 6 年前

    我正在学习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 没用。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Jayhello    6 年前

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

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

    2. 将所有图像文件路径连接到一个txt文件,并将所有fddb注释连接到一个txt文件。 你可以下载所有的文件 here

    enter image description here

    至于我,我把所有的 FDDB-FOLD-%d.txt 转到目录 all_file_path ,然后按顺序将它们加入到一个文件中 cat * > filePath.txt

    enter image description here

    加入所有 FDDB-fold-%d-ellipseList.txt 一个接一个 cat *ellipse*.txt > annotFile.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
    

    制作文件。

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

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

    enter image description here

    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文件(由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:
    

    enter image description here

    跑步者需要评估。如果出现一些错误,请将执行评估更改为以下内容:

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

    您还可以使用以下命令进行检查:

    enter image description here

    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结果: enter image description here

    注意:当你在windows中创建上面的txt文件时,如果你在ubuntu中测试它,你可能会得到以下错误 Incompatible annotation and detection files. See output specifications :

    enter image description here

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

    结果如下:

    enter image description here

    一些提示:

    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 这很容易理解),所以你将对如何评估它有深刻的理解。

        2
  •  0
  •   AlexX    6 年前

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

    您需要从以下位置下载带标签的数据: 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
    

    其中first是图像的名称, 然后图像中的人脸数量, 然后对每一张脸进行协调并重复。。。

    我建议你建造 评价 在linux上,因为它容易得多(至少对我来说是这样)。

    希望有帮助。