safesight/edge/plugins/alarm/rule_engine.h
tian 29d9ef5d0d refactor: 项目结构梳理 - 设备端/管理端对称布局
仓库结构:
  edge/     设备端(原根目录设备端代码整体移入)
  control/  管理端(清理后)
  docs/     文档(PRD 移入 design/)
  README.md 根导航(新增)

清理:
- control/.brainstorm 临时草稿删除
- control 根级重复文档(API表/PRD_04)并入 docs/design/
- control/plan.md -> docs/implementation/control-plan.md
- control/safesightd-linux-arm64 二进制取消版本控制(.gitignore)
- edge/transform 模型转换产物归入 models/,onnx/pt 大源文件取消跟踪(.gitignore)
- Readme.md(PRD) -> docs/design/PRD_Product_v1.2.md(避开 README 大小写冲突)

更新:
- 根 README.md 导航、docs/README.md 文档索引
- deployment.md/检查表路径加 edge/ 前缀
- .gitignore 重写(edge/control 分区规则)
2026-08-02 11:39:54 +08:00

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