VS2017+Dlib19.19配置见上篇博客,opencv配置网上相关教程也很多,在此不做过多的赘述
实现步骤
一,人脸检测
①opencv的级联器检测
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
/** Global variables */
#define CV_WINDOW_NORMAL cv::WINDOW_NORMAL
#define CV_HAAR_SCALE_IMAGE cv::CASCADE_SCALE_IMAGE
#define CV_HAAR_FIND_BIGGEST_OBJECT cv::CASCADE_FIND_BIGGEST_OBJECT
/* Note, either copy these two files from opencv/data/haarscascades
to your current folder,or change these locations*/
cv::String face_cascade_name = ".../haarcascade_frontalface_alt.xml";
cv::CascadeClassifier face_cascade;
std::string main_window_name = "Face detection";
int main()
{
cv::Mat frame;
if (!face_cascade.load(face_cascade_name))
{
printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n");
return -1;
};
cv::namedWindow(main_window_name, CV_WINDOW_NORMAL);
cv::moveWindow(main_window_name, 400, 100);
cv::VideoCapture capture(0);
if (capture.isOpened()) {
while (true) {
capture.read(frame);
if (!frame.empty())
{
//convert to frame_gray
std::vector<cv::Rect> faces;
std::vector<cv::Mat> rgbChannels(3);
cv::split(frame, rgbChannels);
cv::Mat frame_gray = rgbChannels[2];
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE | CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150, 150));
//-- draw the region
for (int i = 0; i < faces.size(); i++)
{
cv::rectangle(frame, faces[i], 1234);
}
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}
imshow(main_window_name, frame);
int c = cv::waitKey(10);
if ((char)c == 'c') { break; }
}
}
}
②dlib人脸检测
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/image_io.h>
#include <dlib/opencv/cv_image.h>
#include <dlib/opencv.h>
using namespace std;
using namespace cv;
using namespace dlib;
/** Global variables */
#define CV_WINDOW_NORMAL cv::WINDOW_NORMAL
#define CV_HAAR_SCALE_IMAGE cv::CASCADE_SCALE_IMAGE
#define CV_HAAR_FIND_BIGGEST_OBJECT cv::CASCADE_FIND_BIGGEST_OBJECT
//Define the detector
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
std::string main_window_name = "Face detection";
//convert Rectangle(Dlib) to Rect(OpenCV)
static cv::Rect dlibRectangleToOpenCV(dlib::rectangle r)
{
return cv::Rect(cv::Point2i(r.left(), r.top()), cv::Point2i(r.right() + 1, r.bottom() + 1));
}
int main()
{
cv::Mat frame;
cv::namedWindow(main_window_name, CV_WINDOW_NORMAL);
cv::moveWindow(main_window_name, 400, 100);
cv::VideoCapture capture(0);
if (capture.isOpened()) {
while (true) {
capture.read(frame);
if (!frame.empty())
{
//convert Mat to array2D
cv_image<bgr_pixel> cimg(frame);
//-- Detect faces
std::vector<dlib::rectangle> faces = detector(cimg);
//-- Show what you got
for (int i = 0; i < faces.size(); i++)
{
cv::rectangle(frame, dlibRectangleToOpenCV(faces[0]), 1234);
}
}
else {
printf(" --(!) No captured frame -- Break!");
break;
}
//show windows
cv::imshow(main_window_name, frame);
int c = cv::waitKey(10);
if ((char)c == 'c') { break; }
}
}
}
总结:Dlib的人脸检测准确度要高于Opencv的人脸检测,但是Dlib的检测性能要低于Opencv,网上有相关的帧率测试。
踩过的坑:
1.Opencv获取的图像为Mat类型,而Dlib获取的图像为array2D类型,两者结合使用需要进行类型转换
2.同理,Opencv的Rect矩形框贺Dlib的Rectangle的矩形框坐标规定不一样也需要进行相应的转换。
3.调用Dlib库的时候一定要编译成Release版本,对应的VS2017也要设置成
相应的环境。
2019-2020第二学期的学习总结,乘着没事整理一下!