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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| #include "mediapipe/calculators/ims/head_pose_calculator.h" #include "mediapipe/framework/port/logging.h" #include <cmath>
namespace mediapipe {
using mediapipe::NormalizedLandmarkList; using mediapipe::NormalizedLandmark;
absl::Status HeadPoseCalculator::GetContract(CalculatorContract* cc) { cc->Inputs().Tag("LANDMARKS").Set<NormalizedLandmarkList>(); cc->Inputs().Tag("IMAGE_SIZE").Set<std::pair<int, int>>(); cc->Outputs().Tag("HEAD_POSE").Set<HeadPose>(); cc->Outputs().Tag("ROTATION_VECTOR").Set<cv::Mat>(); cc->Outputs().Tag("TRANSLATION_VECTOR").Set<cv::Mat>(); cc->Options<HeadPoseOptions>(); return absl::OkStatus(); }
absl::Status HeadPoseCalculator::Open(CalculatorContext* cc) { const auto& options = cc->Options<HeadPoseOptions>(); for (const auto& point : options.model_points()) { model_points_.emplace_back(point.x(), point.y(), point.z()); } if (model_points_.empty()) { model_points_ = { {0.0, 0.0, 0.0}, {0.0, -330.0, -65.0}, {-225.0, 170.0, -135.0}, {225.0, 170.0, -135.0}, {-150.0, -150.0, -125.0}, {150.0, -150.0, -125.0}, }; } for (int idx : options.landmark_indices()) { landmark_indices_.push_back(idx); } if (landmark_indices_.empty()) { landmark_indices_ = {1, 152, 33, 263, 61, 291}; } use_default_camera_matrix_ = options.use_default_camera_matrix(); dist_coeffs_ = cv::Mat::zeros(4, 1, CV_64F); LOG(INFO) << "HeadPoseCalculator initialized with " << model_points_.size() << " model points and " << landmark_indices_.size() << " landmarks"; return absl::OkStatus(); }
absl::Status HeadPoseCalculator::Process(CalculatorContext* cc) { if (cc->Inputs().Tag("LANDMARKS").IsEmpty()) { return absl::OkStatus(); } const auto& landmarks = cc->Inputs().Tag("LANDMARKS").Get<NormalizedLandmarkList>(); if (cc->Inputs().Tag("IMAGE_SIZE").IsEmpty()) { return absl::OkStatus(); } const auto& size = cc->Inputs().Tag("IMAGE_SIZE").Get<std::pair<int, int>>(); cv::Size image_size(size.first, size.second); if (landmarks.landmark_size() < *std::max_element(landmark_indices_.begin(), landmark_indices_.end())) { LOG(WARNING) << "Insufficient landmarks for pose estimation"; return absl::OkStatus(); } std::vector<cv::Point2d> image_points = ExtractImagePoints(landmarks, image_size); cv::Mat camera_matrix = BuildCameraMatrix(image_size); cv::Mat rotation_vector, translation_vector; bool success = cv::solvePnP( model_points_, image_points, camera_matrix, dist_coeffs_, rotation_vector, translation_vector, false, cv::SOLVEPNP_ITERATIVE ); if (!success) { LOG(WARNING) << "solvePnP failed"; return absl::OkStatus(); } HeadPose pose = RotationToEuler(rotation_vector, translation_vector); pose.timestamp = cc->InputTimestamp().Value(); float visibility_sum = 0.0f; for (int idx : landmark_indices_) { visibility_sum += landmarks.landmark(idx).visibility(); } pose.confidence = visibility_sum / landmark_indices_.size(); cc->Outputs().Tag("HEAD_POSE").AddPacket( MakePacket<HeadPose>(pose).At(cc->InputTimestamp())); cc->Outputs().Tag("ROTATION_VECTOR").AddPacket( MakePacket<cv::Mat>(rotation_vector.clone()).At(cc->InputTimestamp())); cc->Outputs().Tag("TRANSLATION_VECTOR").AddPacket( MakePacket<cv::Mat>(translation_vector.clone()).At(cc->InputTimestamp())); VLOG(1) << "HeadPose: pitch=" << pose.pitch << ", yaw=" << pose.yaw << ", roll=" << pose.roll; return absl::OkStatus(); }
std::vector<cv::Point2d> HeadPoseCalculator::ExtractImagePoints( const NormalizedLandmarkList& landmarks, const cv::Size& image_size) { std::vector<cv::Point2d> image_points; for (int idx : landmark_indices_) { const auto& lm = landmarks.landmark(idx); double x = lm.x() * image_size.width; double y = lm.y() * image_size.height; image_points.emplace_back(x, y); } return image_points; }
cv::Mat HeadPoseCalculator::BuildCameraMatrix(const cv::Size& image_size) { double focal_length = image_size.width; double cx = image_size.width / 2.0; double cy = image_size.height / 2.0; cv::Mat camera_matrix = (cv::Mat_<double>(3, 3) << focal_length, 0, cx, 0, focal_length, cy, 0, 0, 1 ); return camera_matrix; }
HeadPose HeadPoseCalculator::RotationToEuler( const cv::Mat& rotation_vector, const cv::Mat& translation_vector) { HeadPose pose; cv::Mat rotation_matrix; cv::Rodrigues(rotation_vector, rotation_matrix); pose.pitch = std::atan2(rotation_matrix.at<double>(2, 1), rotation_matrix.at<double>(2, 2)) * 180.0 / CV_PI; pose.yaw = std::atan2(-rotation_matrix.at<double>(2, 0), std::sqrt(rotation_matrix.at<double>(1, 0) * rotation_matrix.at<double>(1, 0) + rotation_matrix.at<double>(0, 0) * rotation_matrix.at<double>(0, 0))) * 180.0 / CV_PI; pose.roll = std::atan2(rotation_matrix.at<double>(1, 0), rotation_matrix.at<double>(0, 0)) * 180.0 / CV_PI; pose.tx = translation_vector.at<double>(0); pose.ty = translation_vector.at<double>(1); pose.tz = translation_vector.at<double>(2); return pose; }
REGISTER_CALCULATOR(HeadPoseCalculator);
}
|