OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检测的测试代码
2017-04-12 21:01
691 查看
Tadas Baltrusaitis的OpenFace是一个开源的面部行为分析工具,它的源码可以从 https://github.com/TadasBaltrusaitis/OpenFace 下载。OpenFace主要包括面部关键点检测(facial landmard detection)、头部姿势估计(head pose estimation)、面部动作单元识别(facial action unit recognition)、人眼视线方向估计(eye gaze estimation)。
编译Tadas Baltrusaitis的OpenFace需要依赖开源库boost、dlib、TBB、OpenCV。
以下是基于Haar Cascade Classifiers方法的人脸检测的测试代码:
#include "funset.hpp"
#include <vector>
#include <string>
#include <fstream>
#include <filesystem.hpp>
#include <filesystem/fstream.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <tbb/tbb.h>
#include <opencv2/opencv.hpp>
#include <LandmarkCoreIncludes.h>
#include <FaceAnalyser.h>
#include <GazeEstimation.h>
#define CONFIG_DIR "E:/GitCode/Face_Test/src/TadasBaltrusaitis_OpenFace/lib/local/LandmarkDetector/"
int test_FaceDetect_HaarCascade()
{
std::vector<std::string> arguments{ "", "-wild", "-fdir", "E:/GitCode/Face_Test/testdata/",
"-ofdir", "E:/GitCode/Face_Test/testdata/ret1/", "-oidir", "E:/GitCode/Face_Test/testdata/ret2/" };
std::vector<std::string> files, depth_files, output_images, output_landmark_locations, output_pose_locations;
std::vector<cv::Rect_<double> > bounding_boxes; // Bounding boxes for a face in each image (optional)
LandmarkDetector::get_image_input_output_params(files, depth_files, output_landmark_locations, output_pose_locations, output_images, bounding_boxes, arguments);
LandmarkDetector::FaceModelParameters det_parameters(arguments);
cv::CascadeClassifier classifier(det_parameters.face_detector_location);
for (auto file : files) {
cv::Mat grayscale_image = cv::imread(file, 0);
if (grayscale_image.empty()) {
fprintf(stderr, "Could not read the input image: %s\n", file.c_str());
return -1;
}
int pos = file.find_last_of("\\");
std::string image_name = file.substr(pos + 1);
std::vector<cv::Rect_<double> > face_detections; // Detect faces in an image
LandmarkDetector::DetectFaces(face_detections, grayscale_image, classifier);
std::string image_path = file.substr(0, pos);
std::string save_result = image_path + "/ret2/_" + image_name;
cv::Mat bgr = cv::imread(file, 1);
fprintf(stderr, "%s face count: %d\n", image_name.c_str(), face_detections.size());
for (int i = 0; i < face_detections.size(); ++i) {
cv::Rect_<double> rect{ face_detections[i] };
fprintf(stderr, " x: %.2f, y: %.2f, width: %.2f, height: %.2f\n",
rect.x, rect.y, rect.width, rect.height);
cv::rectangle(bgr, cv::Rect(rect.x, rect.y, rect.width, rect.height), cv::Scalar(0, 255, 0), 2);
}
cv::imwrite(save_result, bgr);
}
int width = 200;
int height = 200;
cv::Mat dst(height * 5, width * 4, CV_8UC3);
int pos = files[0].find_last_of("\\");
std::string image_path = files[0].substr(0, pos);
for (int i = 0; i < files.size(); i++) {
std::string image_name = files[i].substr(pos + 1);
std::string input_image = image_path + "/ret2/_" + image_name;
cv::Mat src = cv::imread(input_image, 1);
if (src.empty()) {
fprintf(stderr, "read image error: %s\n", input_image.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 = image_path + "/ret2/result.png";
cv::imwrite(output_image, dst);
return 0;
} 执行结果如下图:
人脸检测结果如下:
GitHub:https://github.com/fengbingchun/Face_Test
编译Tadas Baltrusaitis的OpenFace需要依赖开源库boost、dlib、TBB、OpenCV。
以下是基于Haar Cascade Classifiers方法的人脸检测的测试代码:
#include "funset.hpp"
#include <vector>
#include <string>
#include <fstream>
#include <filesystem.hpp>
#include <filesystem/fstream.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <tbb/tbb.h>
#include <opencv2/opencv.hpp>
#include <LandmarkCoreIncludes.h>
#include <FaceAnalyser.h>
#include <GazeEstimation.h>
#define CONFIG_DIR "E:/GitCode/Face_Test/src/TadasBaltrusaitis_OpenFace/lib/local/LandmarkDetector/"
int test_FaceDetect_HaarCascade()
{
std::vector<std::string> arguments{ "", "-wild", "-fdir", "E:/GitCode/Face_Test/testdata/",
"-ofdir", "E:/GitCode/Face_Test/testdata/ret1/", "-oidir", "E:/GitCode/Face_Test/testdata/ret2/" };
std::vector<std::string> files, depth_files, output_images, output_landmark_locations, output_pose_locations;
std::vector<cv::Rect_<double> > bounding_boxes; // Bounding boxes for a face in each image (optional)
LandmarkDetector::get_image_input_output_params(files, depth_files, output_landmark_locations, output_pose_locations, output_images, bounding_boxes, arguments);
LandmarkDetector::FaceModelParameters det_parameters(arguments);
cv::CascadeClassifier classifier(det_parameters.face_detector_location);
for (auto file : files) {
cv::Mat grayscale_image = cv::imread(file, 0);
if (grayscale_image.empty()) {
fprintf(stderr, "Could not read the input image: %s\n", file.c_str());
return -1;
}
int pos = file.find_last_of("\\");
std::string image_name = file.substr(pos + 1);
std::vector<cv::Rect_<double> > face_detections; // Detect faces in an image
LandmarkDetector::DetectFaces(face_detections, grayscale_image, classifier);
std::string image_path = file.substr(0, pos);
std::string save_result = image_path + "/ret2/_" + image_name;
cv::Mat bgr = cv::imread(file, 1);
fprintf(stderr, "%s face count: %d\n", image_name.c_str(), face_detections.size());
for (int i = 0; i < face_detections.size(); ++i) {
cv::Rect_<double> rect{ face_detections[i] };
fprintf(stderr, " x: %.2f, y: %.2f, width: %.2f, height: %.2f\n",
rect.x, rect.y, rect.width, rect.height);
cv::rectangle(bgr, cv::Rect(rect.x, rect.y, rect.width, rect.height), cv::Scalar(0, 255, 0), 2);
}
cv::imwrite(save_result, bgr);
}
int width = 200;
int height = 200;
cv::Mat dst(height * 5, width * 4, CV_8UC3);
int pos = files[0].find_last_of("\\");
std::string image_path = files[0].substr(0, pos);
for (int i = 0; i < files.size(); i++) {
std::string image_name = files[i].substr(pos + 1);
std::string input_image = image_path + "/ret2/_" + image_name;
cv::Mat src = cv::imread(input_image, 1);
if (src.empty()) {
fprintf(stderr, "read image error: %s\n", input_image.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 = image_path + "/ret2/result.png";
cv::imwrite(output_image, dst);
return 0;
} 执行结果如下图:
人脸检测结果如下:
GitHub:https://github.com/fengbingchun/Face_Test
相关文章推荐
- OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检測的測试代码
- OpenFace库(Tadas Baltrusaitis)中基于Haar Cascade Classifiers进行人脸检測的測试代码
- OpenFace库(Tadas Baltrusaitis)中基于HOG进行正脸人脸检测的测试代码
- 利用Adaboost和Haar特征进行人脸检测
- 基于PCA的人脸检测(Matlab版代码)
- opencv从摄像头读取视频并实时进行人脸检测代码
- 基于OpenCV的人脸检测代码
- 一种基于Haar小波变换的彩色图像人脸检测方法
- 用opencv自带的Haar分类器进行人脸检测(二)
- 人脸检测之Haar-like,Adaboost,级联(cascade)
- opencv 利用类haar小波特征的人脸、人眼等特征检测(带代码)
- AdaBoost中利用Haar特征进行人脸识别算法分析与总结2——级联分类器与检测过程
- AdaBoost中利用Haar特征进行人脸识别算法分析与总结2——级联分类器与检测过程 .
- 【转载】浅谈OpenCV人脸检测以及两个函数cvHaarDetectObjects和cvRunHaarClassifierCascade
- 用opencv的traincascade.exe训练行人的HAAR、LBP和HOG特征的xml文件,并对分类器进行加载和检测
- 基于Haar特征Adaboost人脸检测级联分类
- 对adaboost+haar特征进行人脸检测的原理的认识
- OpenCV学习笔记:基于Haar特征的人脸检测
- 基于OpenCV读取摄像头进行人脸检测和人脸识别
- 基于Gabor特征提取和人工智能神经网络的人脸检测matlab代码