84 lines
2.8 KiB
C++
84 lines
2.8 KiB
C++
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <deque>
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#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<int> class_ids;
|
|
std::set<std::string> event_types;
|
|
std::set<std::string> 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<Detection> matched_detections;
|
|
std::vector<BehaviorEventItem> matched_behavior_events;
|
|
};
|
|
|
|
class RuleEngine {
|
|
public:
|
|
bool Init(const SimpleJson& rules_config, const std::vector<std::string>& labels);
|
|
RuleMatchResult Evaluate(const std::shared_ptr<Frame>& 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<AlarmRule> rules_;
|
|
std::vector<std::string> labels_;
|
|
|
|
// Duration tracking: rule_name -> first match time
|
|
std::map<std::string, std::chrono::steady_clock::time_point> duration_start_;
|
|
|
|
// Cooldown tracking: rule_name -> last trigger time
|
|
std::map<std::string, std::chrono::steady_clock::time_point> last_trigger_;
|
|
|
|
// Per-track cooldown tracking: rule_name#track_id -> last trigger time
|
|
std::map<std::string, std::chrono::steady_clock::time_point> per_track_last_trigger_;
|
|
|
|
// Vote history: rule_name#track_id -> timestamps within window
|
|
std::map<std::string, std::deque<std::chrono::steady_clock::time_point>> vote_history_;
|
|
};
|
|
|
|
} // namespace rk3588
|