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
| #include "mediapipe/calculators/ims/gaze_estimation_calculator.h" #include "mediapipe/framework/port/logging.h"
namespace mediapipe {
using mediapipe::NormalizedLandmarkList; using mediapipe::NormalizedLandmark;
absl::Status GazeEstimationCalculator::GetContract(CalculatorContract* cc) { cc->Inputs().Tag("LANDMARKS").Set<NormalizedLandmarkList>(); cc->Outputs().Tag("GAZE_VECTOR").Set<GazeVector>(); cc->Outputs().Tag("LEFT_GAZE").Set<GazeVector>(); cc->Outputs().Tag("RIGHT_GAZE").Set<GazeVector>(); cc->Options<GazeEstimationOptions>(); return absl::OkStatus(); }
absl::Status GazeEstimationCalculator::Open(CalculatorContext* cc) { const auto& options = cc->Options<GazeEstimationOptions>(); left_iris_center_ = options.left_iris_center(); right_iris_center_ = options.right_iris_center(); left_eye_outer_ = options.left_eye_outer(); left_eye_inner_ = options.left_eye_inner(); right_eye_outer_ = options.right_eye_outer(); right_eye_inner_ = options.right_eye_inner(); LOG(INFO) << "GazeEstimationCalculator initialized"; return absl::OkStatus(); }
absl::Status GazeEstimationCalculator::Process(CalculatorContext* cc) { if (cc->Inputs().Tag("LANDMARKS").IsEmpty()) { return absl::OkStatus(); } const auto& landmarks = cc->Inputs().Tag("LANDMARKS").Get<NormalizedLandmarkList>(); if (landmarks.landmark_size() < 478) { LOG(WARNING) << "Insufficient landmarks for iris detection"; return absl::OkStatus(); } GazeVector left_gaze = CalculateEyeGaze( landmarks, left_iris_center_, left_eye_outer_, left_eye_inner_, 159, 145 ); GazeVector right_gaze = CalculateEyeGaze( landmarks, right_iris_center_, right_eye_outer_, right_eye_inner_, 386, 374 ); GazeVector avg_gaze; avg_gaze.x = (left_gaze.x + right_gaze.x) / 2.0f; avg_gaze.y = (left_gaze.y + right_gaze.y) / 2.0f; avg_gaze.confidence = (left_gaze.confidence + right_gaze.confidence) / 2.0f; avg_gaze.timestamp = cc->InputTimestamp().Value(); avg_gaze = SmoothGaze(avg_gaze); cc->Outputs().Tag("GAZE_VECTOR").AddPacket( MakePacket<GazeVector>(avg_gaze).At(cc->InputTimestamp())); cc->Outputs().Tag("LEFT_GAZE").AddPacket( MakePacket<GazeVector>(left_gaze).At(cc->InputTimestamp())); cc->Outputs().Tag("RIGHT_GAZE").AddPacket( MakePacket<GazeVector>(right_gaze).At(cc->InputTimestamp())); VLOG(1) << "Gaze: x=" << avg_gaze.x << ", y=" << avg_gaze.y; return absl::OkStatus(); }
GazeVector GazeEstimationCalculator::CalculateEyeGaze( const NormalizedLandmarkList& landmarks, int iris_center_idx, int eye_outer_idx, int eye_inner_idx, int eye_top_idx, int eye_bottom_idx) { GazeVector gaze; const auto& iris = landmarks.landmark(iris_center_idx); const auto& outer = landmarks.landmark(eye_outer_idx); const auto& inner = landmarks.landmark(eye_inner_idx); const auto& top = landmarks.landmark(eye_top_idx); const auto& bottom = landmarks.landmark(eye_bottom_idx); float eye_center_x = (outer.x() + inner.x()) / 2.0f; float eye_center_y = (top.y() + bottom.y()) / 2.0f; float eye_width = std::abs(inner.x() - outer.x()); float eye_height = std::abs(bottom.y() - top.y()); if (eye_width > 1e-6f && eye_height > 1e-6f) { gaze.x = (iris.x() - eye_center_x) / eye_width * 2.0f; gaze.y = (iris.y() - eye_center_y) / eye_height * 2.0f; } else { gaze.x = 0.0f; gaze.y = 0.0f; } gaze.confidence = (iris.visibility() + outer.visibility() + inner.visibility()) / 3.0f; return gaze; }
GazeVector GazeEstimationCalculator::SmoothGaze(const GazeVector& current) { gaze_history_.push_back(current); while (gaze_history_.size() > smooth_window_) { gaze_history_.pop_front(); } GazeVector smoothed; smoothed.x = 0.0f; smoothed.y = 0.0f; smoothed.confidence = 0.0f; for (const auto& g : gaze_history_) { smoothed.x += g.x; smoothed.y += g.y; smoothed.confidence += g.confidence; } smoothed.x /= gaze_history_.size(); smoothed.y /= gaze_history_.size(); smoothed.confidence /= gaze_history_.size(); smoothed.timestamp = current.timestamp; return smoothed; }
REGISTER_CALCULATOR(GazeEstimationCalculator);
}
|