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
| #ifndef MEDIAPIPE_CALCULATORS_IMS_SPATIAL_ANALYZER_H_ #define MEDIAPIPE_CALCULATORS_IMS_SPATIAL_ANALYZER_H_
#include "mediapipe/framework/formats/landmark.pb.h" #include <cmath>
namespace mediapipe { namespace ims {
struct Point3D { float x, y, z; Point3D() : x(0), y(0), z(0) {} Point3D(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} static float Distance(const Point3D& a, const Point3D& b) { return std::sqrt(std::pow(a.x - b.x, 2) + std::pow(a.y - b.y, 2) + std::pow(a.z - b.z, 2)); } static float Distance2D(const Point3D& a, const Point3D& b) { return std::sqrt(std::pow(a.x - b.x, 2) + std::pow(a.y - b.y, 2)); } Point3D operator-(const Point3D& other) const { return Point3D(x - other.x, y - other.y, z - other.z); } Point3D operator+(const Point3D& other) const { return Point3D(x + other.x, y + other.y, z + other.z); } Point3D operator*(float scalar) const { return Point3D(x * scalar, y * scalar, z * scalar); } float Length() const { return std::sqrt(x * x + y * y + z * z); } Point3D Normalize() const { float len = Length(); if (len < 1e-6f) return Point3D(); return Point3D(x / len, y / len, z / len); } };
class SpatialAnalyzer { public: static Point3D GetPoint(const NormalizedLandmarkList& landmarks, int index) { if (index >= landmarks.landmark_size()) { return Point3D(); } const auto& lm = landmarks.landmark(index); return Point3D(lm.x(), lm.y(), lm.z()); } static bool IsHandNearEar(const Point3D& hand_wrist, const Point3D& hand_index_tip, const Point3D& left_ear, const Point3D& right_ear, float threshold = 0.15f) { Point3D hand_center = (hand_wrist + hand_index_tip) * 0.5f; float dist_left = Point3D::Distance2D(hand_center, left_ear); float dist_right = Point3D::Distance2D(hand_center, right_ear); float min_dist = std::min(dist_left, dist_right); return min_dist < threshold; } static bool IsHandNearMouth(const Point3D& hand_wrist, const Point3D& hand_index_tip, const Point3D& mouth_center, const Point3D& nose_tip, float threshold = 0.12f) { Point3D hand_center = (hand_wrist + hand_index_tip) * 0.5f; float dist_mouth = Point3D::Distance2D(hand_center, mouth_center); bool is_below_nose = hand_center.y > nose_tip.y; return dist_mouth < threshold && is_below_nose; } static bool IsBodyLeaningForward(const Point3D& nose, const Point3D& left_shoulder, const Point3D& right_shoulder, const Point3D& left_hip, const Point3D& right_hip, float threshold = 15.0f) { Point3D shoulder_center = (left_shoulder + right_shoulder) * 0.5f; Point3D hip_center = (left_hip + right_hip) * 0.5f; Point3D torso = shoulder_center - hip_center; float lean_angle = std::atan2(torso.z, torso.y) * 180.0f / M_PI; return std::abs(lean_angle) > threshold; } static bool AreHandsOffWheel(const Point3D& left_wrist, const Point3D& right_wrist, const Point3D& left_shoulder, const Point3D& right_shoulder, float min_height_ratio = 0.3f, float max_width_ratio = 0.8f) { float shoulder_width = Point3D::Distance2D(left_shoulder, right_shoulder); float left_wrist_height = left_shoulder.y - left_wrist.y; float right_wrist_height = right_shoulder.y - right_wrist.y; float wrist_distance = Point3D::Distance2D(left_wrist, right_wrist); bool hands_too_low = (left_wrist_height < shoulder_width * min_height_ratio) && (right_wrist_height < shoulder_width * min_height_ratio); bool hands_too_wide = wrist_distance > shoulder_width * max_width_ratio; return hands_too_low || hands_too_wide; } static bool IsPhoneCallGesture(const NormalizedLandmarkList& hand_landmarks) { if (hand_landmarks.landmark_size() < 21) { return false; } Point3D thumb_tip = GetPoint(hand_landmarks, THUMB_TIP); Point3D index_tip = GetPoint(hand_landmarks, INDEX_TIP); Point3D middle_tip = GetPoint(hand_landmarks, MIDDLE_TIP); Point3D ring_tip = GetPoint(hand_landmarks, RING_TIP); Point3D pinky_tip = GetPoint(hand_landmarks, PINKY_TIP); Point3D wrist = GetPoint(hand_landmarks, WRIST); Point3D index_mcp = GetPoint(hand_landmarks, 5); Point3D middle_mcp = GetPoint(hand_landmarks, 9); Point3D ring_mcp = GetPoint(hand_landmarks, 13); Point3D pinky_mcp = GetPoint(hand_landmarks, 17); bool index_extended = index_tip.y < index_mcp.y; bool middle_extended = middle_tip.y < middle_mcp.y; bool ring_extended = ring_tip.y < ring_mcp.y; bool pinky_extended = pinky_tip.y < pinky_mcp.y; bool is_phone_gesture = pinky_extended && !index_extended && !middle_extended && !ring_extended; return is_phone_gesture; } };
} }
#endif
|