vsc++姿态估计和人脸检测代码
时间: 2023-09-10 17:08:17 浏览: 169
VSC++环境傻瓜包Microsoft Visual C++ Build Tools.zip
以下是一个基于OpenCV和Dlib库的C++代码示例,用于姿态估计和人脸检测:
```c++
#include "opencv2/opencv.hpp"
#include "dlib/opencv.h"
#include "dlib/image_processing.h"
#include "dlib/image_processing/frontal_face_detector.h"
#include "dlib/image_processing/shape_predictor.h"
#include <iostream>
#include <fstream>
using namespace cv;
using namespace dlib;
using namespace std;
int main()
{
try
{
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor predictor;
deserialize("shape_predictor_68_face_landmarks.dat") >> predictor;
VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
namedWindow("Face Detection and Tracking", WINDOW_NORMAL);
setWindowProperty("Face Detection and Tracking", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
while (true)
{
Mat frame;
cap >> frame;
if (frame.empty())
{
cerr << "Unable to capture frame" << endl;
break;
}
cv_image<bgr_pixel> cimg(frame);
std::vector<rectangle> faces = detector(cimg);
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
{
full_object_detection shape = predictor(cimg, faces[i]);
shapes.push_back(shape);
}
for (unsigned long i = 0; i < faces.size(); ++i)
{
rectangle r = faces[i];
rectangle r_scaled(r.left() / 2, r.top() / 2, r.right() / 2, r.bottom() / 2);
rectangle frame_rect(0, 0, frame.cols, frame.rows);
if (frame_rect.contains(r_scaled.tl()) && frame_rect.contains(r_scaled.br()))
{
draw_rectangle(frame, r, cv::Scalar(0, 255, 0), 2);
full_object_detection shape = shapes[i];
for (unsigned long j = 0; j < shape.num_parts(); ++j)
{
circle(frame, cv::Point(shape.part(j).x(), shape.part(j).y()), 2, cv::Scalar(0, 0, 255), -1);
}
// 人脸姿态估计
std::vector<cv::Point3d> model_points;
// 3D模型点
model_points.push_back(cv::Point3d(0.0f, 0.0f, 0.0f)); // 鼻尖
model_points.push_back(cv::Point3d(0.0f, -330.0f, -65.0f)); // 下巴
model_points.push_back(cv::Point3d(-225.0f, 170.0f, -135.0f)); // 左眼内角
model_points.push_back(cv::Point3d(225.0f, 170.0f, -135.0f)); // 右眼内角
model_points.push_back(cv::Point3d(-150.0f, -150.0f, -125.0f)); // 左嘴角
model_points.push_back(cv::Point3d(150.0f, -150.0f, -125.0f)); // 右嘴角
std::vector<cv::Point2d> image_points;
// 2D图像点
for (unsigned long j = 0; j < shape.num_parts(); ++j)
{
image_points.push_back(cv::Point2d(shape.part(j).x(), shape.part(j).y()));
}
cv::Mat camera_matrix = (cv::Mat_<double>(3, 3) <<
1.0, 0, frame.cols / 2,
0, 1.0, frame.rows / 2,
0, 0, 1.0); // 内参矩阵
cv::Mat dist_coeffs = cv::Mat::zeros(4, 1, cv::DataType<double>::type); // 4个畸变系数:k1,k2,p1,p2
cv::Mat rotation_vector; // 旋转向量
cv::Mat translation_vector; // 平移向量
cv::solvePnP(model_points, image_points, camera_matrix, dist_coeffs, rotation_vector, translation_vector);
// 旋转向量转换为旋转矩阵
cv::Mat rotation_matrix;
cv::Rodrigues(rotation_vector, rotation_matrix);
// 投影矩阵
cv::Matx34d projection_matrix(
rotation_matrix.at<double>(0, 0), rotation_matrix.at<double>(0, 1), rotation_matrix.at<double>(0, 2), translation_vector.at<double>(0),
rotation_matrix.at<double>(1, 0), rotation_matrix.at<double>(1, 1), rotation_matrix.at<double>(1, 2), translation_vector.at<double>(1),
rotation_matrix.at<double>(2, 0), rotation_matrix.at<double>(2, 1), rotation_matrix.at<double>(2, 2), translation_vector.at<double>(2)
);
// 计算欧拉角
cv::Vec3d euler_angles;
cv::Matx33d rotation_matrix_ = rotation_matrix;
double sy = sqrt(rotation_matrix_(0, 0) * rotation_matrix_(0, 0) + rotation_matrix_(1, 0) * rotation_matrix_(1, 0));
bool singular = sy < 1e-6;
if (!singular)
{
euler_angles[0] = atan2(rotation_matrix_(2, 1), rotation_matrix_(2, 2));
euler_angles[1] = atan2(-rotation_matrix_(2, 0), sy);
euler_angles[2] = atan2(rotation_matrix_(1, 0), rotation_matrix_(0, 0));
}
else
{
euler_angles[0] = atan2(-rotation_matrix_(1, 2), rotation_matrix_(1, 1));
euler_angles[1] = atan2(-rotation_matrix_(2, 0), sy);
euler_angles[2] = 0;
}
// 显示欧拉角信息
stringstream ss;
ss << "Yaw: " << euler_angles[1] * 180 / CV_PI << " degrees" << endl;
ss << "Pitch: " << euler_angles[0] * 180 / CV_PI << " degrees" << endl;
ss << "Roll: " << euler_angles[2] * 180 / CV_PI << " degrees" << endl;
cv::putText(frame, ss.str(), cv::Point(10, frame.rows - 50), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 2);
}
}
imshow("Face Detection and Tracking", frame);
if (waitKey(1) == 27)
{
break;
}
}
destroyAllWindows();
}
catch (exception& e)
{
cerr << e.what() << endl;
}
return 0;
}
```
这段代码实现了从摄像头捕捉视频流,实时检测人脸并标出关键点,同时根据人脸关键点进行姿态估计,并在视频中显示欧拉角信息。请注意,由于姿态估计需要3D模型点,因此需要预先定义这些点的坐标。在这个示例中,我们将这些点硬编码为固定值。这段代码还需要使用OpenCV和Dlib库,因此请确保在编译代码之前安装这些库。
阅读全文