Opencv 如何从ML套件人脸标志点估计人脸姿势
我正在使用iOS上的Firebase ML工具包检测人脸。虽然它提供了Euler Y和Z角度,但不提供Euler X角度(俯仰)。因此,我想尝试使用OpenCV solvePnp计算音高,如下所述: 这是我的目标C函数:Opencv 如何从ML套件人脸标志点估计人脸姿势,opencv,computer-vision,face-detection,firebase-mlkit,Opencv,Computer Vision,Face Detection,Firebase Mlkit,我正在使用iOS上的Firebase ML工具包检测人脸。虽然它提供了Euler Y和Z角度,但不提供Euler X角度(俯仰)。因此,我想尝试使用OpenCV solvePnp计算音高,如下所述: 这是我的目标C函数: +(void) estimatePose:(FIRVisionFace *)face imgSize:(CGSize)imgSize { // Contour legend: https://firebase.google.com/docs/ml-kit/image
+(void) estimatePose:(FIRVisionFace *)face imgSize:(CGSize)imgSize {
// Contour legend: https://firebase.google.com/docs/ml-kit/images/examples/face_contours.svg
FIRVisionFaceContour* faceOval = [face contourOfType:FIRFaceContourTypeFace];
FIRVisionFaceContour* leftEyeContour = [face contourOfType:FIRFaceContourTypeLeftEye];
FIRVisionFaceContour* rightEyeContour = [face contourOfType:FIRFaceContourTypeRightEye];
FIRVisionFaceContour* noseBridge = [face contourOfType:FIRFaceContourTypeNoseBridge];
FIRVisionFaceContour* upperLipTop = [face contourOfType:FIRFaceContourTypeUpperLipTop];
FIRVisionPoint* chin = faceOval.points[18];
FIRVisionPoint* leftEyeLeftCorner = leftEyeContour.points[0];
FIRVisionPoint* rightEyeRightCorner = rightEyeContour.points[8];
FIRVisionPoint* noseTip = noseBridge.points[1];
FIRVisionPoint* leftMouthCorner = upperLipTop.points[0];
FIRVisionPoint* rightMouthCorner = upperLipTop.points[10];
// 2D/3D model points using https://www.learnopencv.com/head-pose-estimation-using-opencv-and-dlib/#code
image_points.push_back( cv::Point2d(noseTip.x.doubleValue, noseTip.y.doubleValue) ); // Nose tip
image_points.push_back( cv::Point2d(chin.x.doubleValue, chin.y.doubleValue) ); // Chin
image_points.push_back( cv::Point2d(leftEyeLeftCorner.x.doubleValue, leftEyeLeftCorner.y.doubleValue) ); // Left eye left corner
image_points.push_back( cv::Point2d(rightEyeRightCorner.x.doubleValue, rightEyeRightCorner.y.doubleValue) ); // Right eye right corner
image_points.push_back( cv::Point2d(leftMouthCorner.x.doubleValue, leftMouthCorner.y.doubleValue) ); // Left Mouth corner
image_points.push_back( cv::Point2d(rightMouthCorner.x.doubleValue, rightMouthCorner.y.doubleValue) ); // Right mouth corner
model_points.push_back(cv::Point3d(0.0f, 0.0f, 0.0f)); // Nose tip
model_points.push_back(cv::Point3d(0.0f, -330.0f, -65.0f)); // Chin
model_points.push_back(cv::Point3d(-225.0f, 170.0f, -135.0f)); // Left eye left corner
model_points.push_back(cv::Point3d(225.0f, 170.0f, -135.0f)); // Right eye right corner
model_points.push_back(cv::Point3d(-150.0f, -150.0f, -125.0f)); // Left Mouth corner
model_points.push_back(cv::Point3d(150.0f, -150.0f, -125.0f)); // Right mouth corner
double focal_length = imgSize.width; // Approximate focal length.
cv::Point2d center = cv::Point2d(imgSize.width / 2, imgSize.height / 2);
cv::Mat camera_matrix = (cv::Mat_<double>(3,3) << focal_length, 0, center.x, 0 , focal_length, center.y, 0, 0, 1);
cv::Mat dist_coeffs = cv::Mat::zeros(4,1,cv::DataType<double>::type); // Assuming no lens distortion
// Output rotation and translation
cv::Mat rotation_vector; // Rotation in axis-angle form
cv::Mat translation_vector;
// Solve for pose
cv::solvePnP(model_points, image_points, camera_matrix, dist_coeffs, rotation_vector, translation_vector);
NSLog(@"Rotation Vector %f %f %f", rotation_vector.at<float>(0), rotation_vector.at<float>(1), rotation_vector.at<float>(2));
}
我做错了什么?问题在于日志语句。它需要打印双精度而不是浮动:
NSLog(@"Rotation Vector %f %f %f", rotation_vector.at<double>(0), rotation_vector.at<double>(1), rotation_vector.at<double>(2));
NSLog(@“旋转向量%f%f%f”,旋转向量在(0),旋转向量在(1),旋转向量在(2));
此外,旋转向量不是欧拉角。它需要转换为:
问题在于日志语句。它需要打印双精度而不是浮动:
NSLog(@"Rotation Vector %f %f %f", rotation_vector.at<double>(0), rotation_vector.at<double>(1), rotation_vector.at<double>(2));
NSLog(@“旋转向量%f%f%f”,旋转向量在(0),旋转向量在(1),旋转向量在(2));
此外,旋转向量不是欧拉角。它需要转换为:
嗨,Jacob——我对将EulerX检测添加到ML套件Vision的颤振库很感兴趣。您的解决方案是如何工作的?嗨,Jacob——我对将EulerX检测添加到用于ML Kit Vision的颤振库很感兴趣。你的解决方案怎么样?