如何从ML Kit脸部界标点估计脸部姿势

时间:2019-04-02 00:44:00

标签: opencv computer-vision face-detection firebase-mlkit

我在iOS上使用Firebase ML Kit来检测面部。尽管它提供了Euler Y和Z角,但没有提供Euler X角(螺距)。因此,我想尝试使用OpenCVsolvePnp计算音高,如下所述: https://www.learnopencv.com/head-pose-estimation-using-opencv-and-dlib/#code

这是我的Objective C函数:

ID  Name    ID  WorkDay     MissionCode
1   Brain   1   2019-02-01  2470
2   Amy     2   2019-02-01  7210

该函数末尾的print语句正在为大部分笔直的面部生成不切实际的值,例如:

+(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));
}

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

问题是日志语句。它需要打印双打而不是浮空:

NSLog(@"Rotation Vector %f %f %f", rotation_vector.at<double>(0), rotation_vector.at<double>(1), rotation_vector.at<double>(2));

此外,旋转矢量也不是欧拉角。需要进行转换:

http://answers.opencv.org/question/16796/computing-attituderoll-pitch-yaw-from-solvepnp/?answer=52913#post-id-52913