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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| #ifndef MEDIAPIPE_CALCULATORS_OMS_SEATBELT_DETECTION_H_ #define MEDIAPIPE_CALCULATORS_OMS_SEATBELT_DETECTION_H_
#include "mediapipe/framework/calculator_framework.h" #include "mediapipe/framework/formats/landmark.pb.h"
namespace mediapipe {
message SeatbeltStatus { enum State { UNKNOWN = 0; FASTENED = 1; UNFASTENED = 2; UNDER_ARM = 3; BEHIND_BACK = 4; LOOSE = 5; } State state = 1; float confidence = 2; repeated bool seat_fastened = 3; uint64 timestamp_ms = 4; }
class SeatbeltDetectionCalculator : public CalculatorBase { public: static absl::Status GetContract(CalculatorContract* cc) { cc->Inputs().Tag("POSE_LANDMARKS").Set<std::vector<NormalizedLandmarkList>>(); cc->Inputs().Tag("SEATBELT_LANDMARKS").Set<std::vector<NormalizedLandmarkList>>(); cc->Inputs().Tag("IMAGE").Set<ImageFrame>(); cc->Outputs().Tag("SEATBELT_STATUS").Set<SeatbeltStatus>(); cc->Outputs().Tag("ALERT").Set<bool>(); cc->Options<SeatbeltDetectionOptions>(); return absl::OkStatus(); }
absl::Status Open(CalculatorContext* cc) override { const auto& options = cc->Options<SeatbeltDetectionOptions>(); shoulder_belt_threshold_ = options.shoulder_belt_threshold(); lap_belt_threshold_ = options.lap_belt_threshold(); loose_threshold_ = options.loose_threshold(); return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override { SeatbeltStatus status; status.set_timestamp_ms(cc->InputTimestamp().Value() / 1000); if (cc->Inputs().Tag("POSE_LANDMARKS").IsEmpty()) { status.set_state(SeatbeltStatus::UNKNOWN); cc->Outputs().Tag("SEATBELT_STATUS").AddPacket( MakePacket<SeatbeltStatus>(status).At(cc->InputTimestamp())); return absl::OkStatus(); } const auto& poses = cc->Inputs().Tag("POSE_LANDMARKS").Get<std::vector<NormalizedLandmarkList>>(); if (poses.empty()) { status.set_state(SeatbeltStatus::UNKNOWN); cc->Outputs().Tag("SEATBELT_STATUS").AddPacket( MakePacket<SeatbeltStatus>(status).At(cc->InputTimestamp())); return absl::OkStatus(); } const auto& pose = poses[0]; std::vector<Point3D> belt_points; if (!cc->Inputs().Tag("SEATBELT_LANDMARKS").IsEmpty()) { const auto& belts = cc->Inputs().Tag("SEATBELT_LANDMARKS").Get<std::vector<NormalizedLandmarkList>>(); if (!belts.empty()) { for (int i = 0; i < belts[0].landmark_size(); ++i) { Point3D p; p.x = belts[0].landmark(i).x(); p.y = belts[0].landmark(i).y(); p.z = belts[0].landmark(i).z(); belt_points.push_back(p); } } } Point3D left_shoulder = GetPoint(pose, 11); Point3D right_shoulder = GetPoint(pose, 12); Point3D left_hip = GetPoint(pose, 23); Point3D right_hip = GetPoint(pose, 24); Point3D chest = GetChestCenter(left_shoulder, right_shoulder); SeatbeltStatus::State detected_state = SeatbeltStatus::UNKNOWN; float confidence = 0.0f; if (belt_points.empty()) { detected_state = SeatbeltStatus::UNFASTENED; confidence = 0.8f; } else { bool shoulder_belt_ok = CheckShoulderBelt( belt_points, left_shoulder, right_shoulder, chest); bool lap_belt_ok = CheckLapBelt( belt_points, left_hip, right_hip); bool under_arm = CheckUnderArm(belt_points, left_shoulder, right_shoulder); bool behind_back = CheckBehindBack(belt_points, chest); bool loose = CheckLoose(belt_points, left_shoulder, right_shoulder); if (under_arm) { detected_state = SeatbeltStatus::UNDER_ARM; confidence = 0.7f; } else if (behind_back) { detected_state = SeatbeltStatus::BEHIND_BACK; confidence = 0.7f; } else if (loose) { detected_state = SeatbeltStatus::LOOSE; confidence = 0.6f; } else if (shoulder_belt_ok && lap_belt_ok) { detected_state = SeatbeltStatus::FASTENED; confidence = 0.9f; } else { detected_state = SeatbeltStatus::UNFASTENED; confidence = 0.6f; } } status.set_state(detected_state); status.set_confidence(confidence); bool alert = (detected_state == SeatbeltStatus::UNFASTENED || detected_state == SeatbeltStatus::UNDER_ARM || detected_state == SeatbeltStatus::BEHIND_BACK); cc->Outputs().Tag("SEATBELT_STATUS").AddPacket( MakePacket<SeatbeltStatus>(status).At(cc->InputTimestamp())); cc->Outputs().Tag("ALERT").AddPacket( MakePacket<bool>(alert).At(cc->InputTimestamp())); return absl::OkStatus(); }
private: float shoulder_belt_threshold_ = 0.2f; float lap_belt_threshold_ = 0.15f; float loose_threshold_ = 0.1f; Point3D GetPoint(const NormalizedLandmarkList& landmarks, int index) { Point3D p; if (index < landmarks.landmark_size()) { p.x = landmarks.landmark(index).x(); p.y = landmarks.landmark(index).y(); p.z = landmarks.landmark(index).z(); } return p; } Point3D GetChestCenter(const Point3D& left_shoulder, const Point3D& right_shoulder) { Point3D center; center.x = (left_shoulder.x + right_shoulder.x) / 2.0f; center.y = (left_shoulder.y + right_shoulder.y) / 2.0f; center.z = (left_shoulder.z + right_shoulder.z) / 2.0f; return center; } bool CheckShoulderBelt(const std::vector<Point3D>& belt_points, const Point3D& left_shoulder, const Point3D& right_shoulder, const Point3D& chest) { for (const auto& belt_point : belt_points) { float dist_to_chest = Distance2D(belt_point, chest); if (dist_to_chest < shoulder_belt_threshold_) { return true; } } return false; } bool CheckLapBelt(const std::vector<Point3D>& belt_points, const Point3D& left_hip, const Point3D& right_hip) { Point3D hip_center; hip_center.x = (left_hip.x + right_hip.x) / 2.0f; hip_center.y = (left_hip.y + right_hip.y) / 2.0f; for (const auto& belt_point : belt_points) { float dist_to_hip = Distance2D(belt_point, hip_center); if (dist_to_hip < lap_belt_threshold_) { return true; } } return false; } bool CheckUnderArm(const std::vector<Point3D>& belt_points, const Point3D& left_shoulder, const Point3D& right_shoulder) { for (const auto& belt_point : belt_points) { float dist_to_left = Distance2D(belt_point, left_shoulder); float dist_to_right = Distance2D(belt_point, right_shoulder); if (belt_point.y > left_shoulder.y + 0.1f && dist_to_left < 0.2f) { return true; } if (belt_point.y > right_shoulder.y + 0.1f && dist_to_right < 0.2f) { return true; } } return false; } bool CheckBehindBack(const std::vector<Point3D>& belt_points, const Point3D& chest) { for (const auto& belt_point : belt_points) { if (belt_point.z < chest.z - 0.05f) { return true; } } return false; } bool CheckLoose(const std::vector<Point3D>& belt_points, const Point3D& left_shoulder, const Point3D& right_shoulder) { for (const auto& belt_point : belt_points) { float dist_to_shoulder = std::min( Distance2D(belt_point, left_shoulder), Distance2D(belt_point, right_shoulder)); if (dist_to_shoulder > loose_threshold_) { return true; } } return false; } 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)); } };
REGISTER_CALCULATOR(SeatbeltDetectionCalculator);
}
#endif
|