1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| #ifndef MEDIAPIPE_CALCULATORS_IMS_HEAD_POSE_CALCULATOR_H_ #define MEDIAPIPE_CALCULATORS_IMS_HEAD_POSE_CALCULATOR_H_
#include "mediapipe/framework/calculator_framework.h" #include "mediapipe/framework/formats/landmark.pb.h" #include <opencv2/opencv.hpp>
namespace mediapipe {
message HeadPose { float pitch = 1; float yaw = 2; float roll = 3; uint64 timestamp_ms = 4; }
class HeadPoseCalculator : public CalculatorBase { public: static absl::Status GetContract(CalculatorContract* cc) { cc->Inputs().Tag("LANDMARKS").Set<std::vector<NormalizedLandmarkList>>(); cc->Outputs().Tag("POSE").Set<HeadPose>(); cc->Options<HeadPoseOptions>(); return absl::OkStatus(); }
absl::Status Open(CalculatorContext* cc) override { model_points_ = { cv::Point3d(0.0, 0.0, 0.0), cv::Point3d(0.0, -330.0, -65.0), cv::Point3d(-225.0, 170.0, -135.0), cv::Point3d(225.0, 170.0, -135.0), cv::Point3d(-150.0, -150.0, -125.0), cv::Point3d(150.0, -150.0, -125.0) }; return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override { if (cc->Inputs().Tag("LANDMARKS").IsEmpty()) { return absl::OkStatus(); }
const auto& face_landmarks = cc->Inputs().Tag("LANDMARKS").Get<std::vector<NormalizedLandmarkList>>(); if (face_landmarks.empty()) { return absl::OkStatus(); }
const auto& landmarks = face_landmarks[0];
std::vector<cv::Point2d> image_points; image_points.push_back(cv::Point2d( landmarks.landmark(1).x(), landmarks.landmark(1).y())); image_points.push_back(cv::Point2d( landmarks.landmark(152).x(), landmarks.landmark(152).y())); image_points.push_back(cv::Point2d( landmarks.landmark(33).x(), landmarks.landmark(33).y())); image_points.push_back(cv::Point2d( landmarks.landmark(263).x(), landmarks.landmark(263).y())); image_points.push_back(cv::Point2d( landmarks.landmark(61).x(), landmarks.landmark(61).y())); image_points.push_back(cv::Point2d( landmarks.landmark(291).x(), landmarks.landmark(291).y()));
double focal_length = 640.0; cv::Point2d center(320.0, 240.0); 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_64F);
cv::Mat rotation_vector; cv::Mat translation_vector; bool success = cv::solvePnP( model_points_, image_points, camera_matrix, dist_coeffs, rotation_vector, translation_vector); if (!success) { LOG(WARNING) << "solvePnP failed"; return absl::OkStatus(); }
cv::Mat rotation_matrix; cv::Rodrigues(rotation_vector, rotation_matrix); double pitch, yaw, roll; RotationMatrixToEulerAngles(rotation_matrix, pitch, yaw, roll);
HeadPose pose; pose.set_pitch(static_cast<float>(pitch)); pose.set_yaw(static_cast<float>(yaw)); pose.set_roll(static_cast<float>(roll)); pose.set_timestamp_ms(cc->InputTimestamp().Value() / 1000);
cc->Outputs().Tag("POSE").AddPacket( MakePacket<HeadPose>(pose).At(cc->InputTimestamp()));
VLOG(1) << "Head pose: pitch=" << pitch << ", yaw=" << yaw << ", roll=" << roll;
return absl::OkStatus(); }
private: std::vector<cv::Point3d> model_points_; void RotationMatrixToEulerAngles(const cv::Mat& R, double& pitch, double& yaw, double& roll) { double sy = std::sqrt(R.at<double>(0, 0) * R.at<double>(0, 0) + R.at<double>(1, 0) * R.at<double>(1, 0)); bool singular = sy < 1e-6; if (!singular) { pitch = std::atan2(R.at<double>(2, 1), R.at<double>(2, 2)); yaw = std::atan2(-R.at<double>(2, 0), sy); roll = std::atan2(R.at<double>(1, 0), R.at<double>(0, 0)); } else { pitch = std::atan2(-R.at<double>(1, 2), R.at<double>(1, 1)); yaw = std::atan2(-R.at<double>(2, 0), sy); roll = 0; } pitch = pitch * 180.0 / CV_PI; yaw = yaw * 180.0 / CV_PI; roll = roll * 180.0 / CV_PI; } };
REGISTER_CALCULATOR(HeadPoseCalculator);
}
#endif
|