Dlib库中实现正脸人脸检测的测试代码
2016-12-06 21:05
711 查看
Dlib库中提供了正脸人脸检测的接口,这里参考dlib/examples/face_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸检测的测试代码,测试代码如下:
执行结果如下图:
人脸检测结果如下:
GitHub:https://github.com/fengbingchun/Face_Test
#include "funset.hpp" #include <string> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.h> #include <opencv2/opencv.hpp> /* reference: dlib/examples/face_detection_ex.cpp This face detector is made using the now classic Histogram of Oriented Gradients (HOG) feature combined with a linear classifier, an image pyramid, and sliding window detection scheme. This type of object detector is fairly general and capable of detecting many types of semi-rigid objects in addition to human faces. */ int test_face_detect() { dlib::frontal_face_detector detector = dlib::get_frontal_face_detector(); dlib::image_window win; std::vector<std::string> images{ "1.jpg", "2.jpg", "3.jpg", "4.jpeg", "5.jpeg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpeg", "12.jpg", "13.jpeg", "14.jpg", "15.jpeg", "16.jpg", "17.jpg", "18.jpg", "19.jpg", "20.jpg" }; std::vector<int> count_faces{ 1, 2, 6, 0, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 8, 2 }; std::string path_images{ "E:/GitCode/Face_Test/testdata/" }; if (images.size() != count_faces.size()) { fprintf(stderr, "their size that images and count_faces are mismatch\n"); return -1; } for (int i = 0; i < images.size(); i++) { dlib::array2d<unsigned char> img; dlib::load_image(img, path_images + images[i]); // Make the image bigger by a factor of two. This is useful since // the face detector looks for faces that are about 80 by 80 pixels // or larger. Therefore, if you want to find faces that are smaller // than that then you need to upsample the image as we do here by // calling pyramid_up(). So this will allow it to detect faces that // are at least 40 by 40 pixels in size. We could call pyramid_up() // again to find even smaller faces, but note that every time we // upsample the image we make the detector run slower since it must // process a larger image. pyramid_up(img); // Now tell the face detector to give us a list of bounding boxes // around all the faces it can find in the image. std::vector<dlib::rectangle> dets = detector(img); fprintf(stderr, "detect face count: %d, actual face count: %d\n", dets.size(), count_faces[i]); cv::Mat matSrc = cv::imread(path_images + images[i], 1); if (matSrc.empty()) { fprintf(stderr, "read image error: %s\n", images[i].c_str()); return -1; } for (auto faces : dets) { int x = faces.left() / 2; int y = faces.top() / 2; int w = faces.width() / 2; int h = faces.height() / 2; cv::rectangle(matSrc, cv::Rect(x, y, w, h), cv::Scalar(0, 255, 0), 2); } std::string save_result = path_images + "_" + images[i]; cv::imwrite(save_result, matSrc); } int width = 200; int height = 200; cv::Mat dst(height * 5, width * 4, CV_8UC3); for (int i = 0; i < images.size(); i++) { std::string input_image = path_images + "_" + images[i]; cv::Mat src = cv::imread(input_image, 1); if (src.empty()) { fprintf(stderr, "read image error: %s\n", images[i].c_str()); return -1; } cv::resize(src, src, cv::Size(width, height), 0, 0, 4); int x = (i * width) % (width * 4); int y = (i / 4) * height; cv::Mat part = dst(cv::Rect(x, y, width, height)); src.copyTo(part); } std::string output_image = path_images + "result.png"; cv::imwrite(output_image, dst); fprintf(stderr, "ok\n"); return 0; }
执行结果如下图:
人脸检测结果如下:
GitHub:https://github.com/fengbingchun/Face_Test
相关文章推荐
- Dlib库中实现正脸人脸检测的测试代码
- Dlib库中实现正脸人脸关键点(landmark)检测的测试代码
- Dlib库中实现正脸人脸关键点(landmark)检测的测试代码
- OpenFace库(Tadas Baltrusaitis)中基于HOG进行正脸人脸检测的测试代码
- 手把手教你用0行代码实现人脸检测
- Dlib人脸特征点检测性能测试(光照+各部分功能模块运行时间)
- Python 3 利用 Dlib 19.7 实现人脸检测和剪切
- Python_人脸检测 (dlib库检测与opencv检测效果对比 含代码)
- 50行Python代码实现人脸检测
- HAAR与DLib的实时人脸检测之实现与对比
- 50行Python代码实现人脸检测功能
- Dlib人脸检测及关键点描述的python实现
- OpenCV中人脸检测代码实现
- python下通过dlib和opencv实现人脸对齐(包括关键点检测和仿射变换)
- 人脸检测主要介绍代码实现
- 25行 Python 代码实现人脸检测——OpenCV 技术教程
- python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
- 代码C++, opencv实现人脸识别,人脸检测,人脸匹配,视频中的人脸检测,摄像头下的人脸检测等
- openCV实现多人脸检测,多眼部检测,完整代码和详细注释
- OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检测的测试代码