#pragma once #include #include #include #include #include #include #include "frame/frame.h" #include "utils/simple_json.h" namespace rk3588 { struct RoiRect { float x = 0.0f; float y = 0.0f; float w = 1.0f; float h = 1.0f; }; struct AlarmRule { std::string name; std::set class_ids; std::set event_types; std::set region_ids; RoiRect roi; float min_score = 0.0f; float min_box_area_ratio = 0.0f; bool require_track_id = false; bool use_behavior_events = false; int min_duration_ms = 0; int min_hits = 1; int hit_window_ms = 0; int cooldown_ms = 5000; int per_track_cooldown_ms = 0; std::string schedule; // "HH:MM-HH:MM" format, empty = always active }; struct RuleMatchResult { bool matched = false; std::string rule_name; std::vector matched_detections; std::vector matched_behavior_events; }; class RuleEngine { public: bool Init(const SimpleJson& rules_config, const std::vector& labels); RuleMatchResult Evaluate(const std::shared_ptr& frame); void Reset(); private: bool IsInRoi(const Rect& bbox, const RoiRect& roi, int img_w, int img_h) const; bool IsInSchedule(const std::string& schedule) const; bool CheckDuration(const std::string& rule_name, bool currently_matched); bool CheckCooldown(const std::string& rule_name); void TriggerCooldown(const std::string& rule_name); bool CheckVote(const std::string& rule_name, int track_id, int min_hits, int hit_window_ms); bool CheckPerTrackCooldown(const std::string& rule_name, int track_id, int per_track_ms); void TriggerPerTrackCooldown(const std::string& rule_name, int track_id); bool PassQuality(const AlarmRule& rule, const Detection& det, double img_area) const; bool PassBehaviorQuality(const AlarmRule& rule, const BehaviorEventItem& event) const; bool MatchBehaviorEventType(const AlarmRule& rule, const BehaviorEventItem& event) const; static std::string MakeKey(const std::string& rule_name, int track_id); std::vector rules_; std::vector labels_; // Duration tracking: rule_name -> first match time std::map duration_start_; // Cooldown tracking: rule_name -> last trigger time std::map last_trigger_; // Per-track cooldown tracking: rule_name#track_id -> last trigger time std::map per_track_last_trigger_; // Vote history: rule_name#track_id -> timestamps within window std::map> vote_history_; }; } // namespace rk3588