opencv联合dlib人脸检测例子二(加快检测)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012819339/article/details/82625414

本篇博客是在opencv联合dlib人脸检测例子的基础上改进了下,加快检测流程

观察了下,opencv利用haar级联分类器检测人脸区域的速度要稍快于dlib的frontal_face_detector检测人脸区域的速度。所以这篇博客是利用opencv先检测出人脸区域,然后交给dlib检测人脸各个部位,最后由opencv画出部位点。haar级联分类器是采用opencv自带训练好的分类器

缺点:opencv的haar级联分类器识别人脸区域的正确率以及检出率可能没有dlib高(这个测试不一定)
优点:haar级联分类器的检测速度较dlib的frontal_face_detector快些

下面给出源代码以及对应解释,各位同学可以测试一下代码,看看是不是比上一个人脸检测的例子快些

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/opencv.h>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <vector>
#include <ctime>



//由于dlib和opencv中有相当一部分类同名,故不能同时对它们使用using namespace,否则会出现一些莫名其妙的问题

//using namespace dlib;
using namespace std;
//using namespace cv;


void line_one_face_detections(cv::Mat img, std::vector<dlib::full_object_detection> fs)
{
    int i, j;

    for(j=0; j<fs.size(); j++)
    {
        cv::Point p1, p2;

        for(i = 0; i<67; i++)
        {
            // 下巴到脸颊 0 ~ 16
            //左边眉毛 17 ~ 21
            //右边眉毛 21 ~ 26
            //鼻梁     27 ~ 30
            //鼻孔        31 ~ 35
            //左眼        36 ~ 41
            //右眼        42 ~ 47
            //嘴唇外圈  48 ~ 59
            //嘴唇内圈  59 ~ 67
            switch(i)
            {
                case 16:
                case 21:
                case 26:
                case 30:
                case 35:
                case 41:
                case 47:
                case 59:
                    i++;
                    break;
                default:
                    break;
            }

            p1.x = fs[j].part(i).x();
            p1.y = fs[j].part(i).y();
            p2.x = fs[j].part(i+1).x();
            p2.y = fs[j].part(i+1).y();
            cv::line(img, p1, p2, cv::Scalar(0,0,255), 1);
        }

    }
}


int main(int argc, char *argv[])
{
    time_t start_t, end_t;
    if(argc != 2)
    {
        std::cout<< "you should specified a picture!"<<std::endl;
        return 0;
    }

    cv::Mat frame = cv::imread(argv[1]);
    cv::Mat src;
    cv::imshow("srcframe", frame);
    //提取灰度图
    cv::cvtColor(frame, src, CV_BGR2GRAY);
    //Mat转化为dlib的matrix
    dlib::array2d<dlib::bgr_pixel> dimg;
    dlib::assign_image(dimg, dlib::cv_image<uchar>(src)); 

    //加载训练好的级联分类器,利用haar级联分类器快速找出人脸区域,然后交给dlib检测人脸部位
    cv::CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");
    if(faceDetector.empty())
    {
        std::cout << "face detector is empty!" <<std::endl;
        return 0;
    }

    //加载人脸形状探测器
    dlib::shape_predictor sp;
    dlib::deserialize("./shape_predictor_68_face_landmarks.dat") >> sp;

    //haar级联分类器探测人脸区域,获取一系列人脸所在区域
    std::vector<cv::Rect> objects;
    std::vector<dlib::rectangle> dets;
    faceDetector.detectMultiScale(src, objects);
    for (int i = 0; i < objects.size(); i++)
    {
        cv::rectangle(frame, objects[i], CV_RGB(200,0,0));
        dlib::rectangle r(objects[i].x, objects[i].y, objects[i].x + objects[i].width, objects[i].y + objects[i].height);
        dets.push_back(r);
    }

    if (dets.size() == 0)
        return 0;

    //获取人脸68个特征点部位分布
    std::vector<dlib::full_object_detection> shapes;
    for(int i = 0; i < dets.size(); i++)
    {
        dlib::full_object_detection shape = sp(dimg, dets[i]); 
        shapes.push_back(shape); 
    }

    line_one_face_detections(frame, shapes);

    cv::imshow("frame", frame);
    cv::waitKey(0);
    return 0;
}

效果:
这里写图片描述

在检测这张图片的情况下,上一个人脸检测的例子不仅速度上慢,人脸区域检出率也较低,下面是上一个人脸检测的例子检测的效果,童鞋们自己试试
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u012819339/article/details/82625414
今日推荐