ANSCustomFireNSmokeDetection: the ANSVIS custom model is judged by ANSMIND's Fire & Smoke verifier
The DLL used to find a detection in one section of the frame, latch a crop
round it and vote on tracked boxes inside that crop. Measured on the field
recordings that was itself the main false-alarm source (the detector needs
context: a floodlight is fake_fire on the whole frame and fire on a tight
crop of the same lamp), and a PTZ tour broke the latch. ANSMIND's native
vision.fire_smoke node replaced it with a whole-frame-first verifier whose
cues are box-relative and carried by global motion, fire-only native zoom
re-checks, a look-alike filter and a ladder of gates measured on real and
false recordings.
The DLL now feeds that verifier, vendored verbatim from ANSMINDCORE dba11ac
(vendor/engine/fire_smoke.hpp, pure C++): ANSLIB runs the package's
detector on the whole frame at a low floor with class-agnostic NMS, the COCO
filter when there is a candidate to explain, and the detector again on the
few native crops the verifier plans; only confirmed tracks come back, as
fire/smoke CustomObjects with fs_seconds/fs_track/evidence in extraInfo.
Parameters: Sensitivity (slider onto low/balanced/high), SmokeSeconds,
DetectFire/DetectSmoke, ZoomChecks, MaxFps (6: evidence is per second of
scene), CandidateScore, MinObjectSize (49a53c3's knob, kept: a candidate
under that fraction of the frame is noise), ExclusiveROIs. Engines load
inside Initialize: a lazily loaded one faulted in ANSLIB's inference entry
on the first frame.
FireNSmokeDetection_Run replays a recording on the video's clock (parity
harness with firesmoke-run). Measured, same package, TensorRT:
15.9.9.0_9.51_real_smoke 63 alarms, first at 3.0 s (node: 56, 3.0 s)
15.9.9.51.10.51_real_smoke 113 alarms, first at 3.0 s (node: 126, 3.0 s)
8 KA false_* clips 0 alarms (node: 0)
Unit tests updated for the new parameter schema; 9/9 pass. The headless
checker from 49a53c3 builds unchanged against the new class.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
build-vs/
|
||||||
|
cmake-build-*/
|
||||||
|
build/
|
||||||
|
.idea/
|
||||||
|
*.user
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,176 +1,86 @@
|
|||||||
|
// ANSCustomFireNSmoke.h — the ANSVIS Fire & Smoke custom model, judged by
|
||||||
|
// the same verifier the ANSMIND `vision.fire_smoke` node runs.
|
||||||
|
//
|
||||||
|
// The first generation of this DLL found a detection in one section of the
|
||||||
|
// frame, latched a crop round it and voted on tracked boxes inside that crop.
|
||||||
|
// Measured on the field recordings that was itself the main false-alarm
|
||||||
|
// source: the detector needs CONTEXT (a floodlight is `fake_fire` on the
|
||||||
|
// whole frame and `fire` on a tight crop of the same lamp), the active window
|
||||||
|
// lost the rest of the picture, and a PTZ tour broke the latch. ANSMIND's
|
||||||
|
// native node replaced that with a whole-frame-first verifier whose every cue
|
||||||
|
// is box-relative and carried by global motion (PTZ-safe), with native-
|
||||||
|
// resolution zoom re-checks for fire only, a look-alike filter, and a ladder
|
||||||
|
// of physical gates measured on real and false recordings (vendor/engine/
|
||||||
|
// fire_smoke.hpp, pure C++, no OpenCV).
|
||||||
|
//
|
||||||
|
// This DLL now feeds that verifier: ANSLIB runs the package's detector on
|
||||||
|
// the whole frame (low floor, class-agnostic NMS), the filter model when
|
||||||
|
// there is something to explain, and the detector again on the few native
|
||||||
|
// crops the verifier asks for; the verifier decides, and only CONFIRMED fire
|
||||||
|
// / smoke tracks come back as CustomObjects, every frame while they last.
|
||||||
|
// So ANSVIS (this DLL in its server) and ANSMIND (the node, or this DLL in
|
||||||
|
// ans-custom-host) judge a scene identically.
|
||||||
#include "ANSLIB.h"
|
#include "ANSLIB.h"
|
||||||
#include <deque>
|
#include "vendor/engine/fire_smoke.hpp"
|
||||||
#include <unordered_set>
|
|
||||||
#include <unordered_map>
|
#include <chrono>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#define RETAINFRAMES 80
|
|
||||||
#define FILTERFRAMES 10
|
|
||||||
//#define FNS_DEBUG
|
|
||||||
class CUSTOM_API ANSCustomFS : public IANSCustomClass
|
class CUSTOM_API ANSCustomFS : public IANSCustomClass
|
||||||
{
|
{
|
||||||
typedef std::pair<cv::Scalar, cv::Scalar> Range;
|
|
||||||
struct ImageSection {
|
|
||||||
cv::Rect region;
|
|
||||||
int priority;
|
|
||||||
ImageSection(const cv::Rect& r) : region(r), priority(0) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Track record for voting-based detection confirmation
|
|
||||||
struct TrackRecord {
|
|
||||||
int trackId{ 0 };
|
|
||||||
int classId{ 0 }; // fire=0, smoke=2
|
|
||||||
std::deque<cv::Rect> bboxHistory; // bounding box history within window
|
|
||||||
std::deque<int> highConfHits; // parallel: 1 if >=HIGH_CONF_THRESHOLD, else 0
|
|
||||||
std::deque<double> vMeanHistory; // parallel: mean HSV V inside bbox (for flicker)
|
|
||||||
int detectedCount{ 0 }; // high-conf hits in sliding window
|
|
||||||
int lowConfDetectedCount{ 0 }; // total hits (any conf >= LOW_CONF_FIRE_THRESHOLD)
|
|
||||||
int totalFrames{ 0 }; // total frames since track appeared
|
|
||||||
bool confirmed{ false }; // passed high-conf voting threshold
|
|
||||||
};
|
|
||||||
|
|
||||||
// Dual-path voting mechanism constants
|
|
||||||
static constexpr int VOTE_WINDOW = 15; // sliding window size (frames)
|
|
||||||
static constexpr int VOTE_THRESHOLD = 8; // high-conf fast path (>=0.5)
|
|
||||||
static constexpr int LOW_VOTE_THRESHOLD = 6; // low-conf slow path (>=0.25 + gates)
|
|
||||||
static constexpr float HIGH_CONF_THRESHOLD = 0.5f; // boundary between low/high conf
|
|
||||||
static constexpr float LOW_CONF_FIRE_THRESHOLD = 0.25f; // detector floor for small-fire path
|
|
||||||
static constexpr float BBOX_CHANGE_THRESHOLD = 0.05f;
|
|
||||||
// V-channel flicker gate: real fire oscillates in luma; sunlight does not
|
|
||||||
static constexpr double FLICKER_V_STD_THRESHOLD = 3.0;
|
|
||||||
static constexpr int FLICKER_MIN_SAMPLES = 6;
|
|
||||||
// Minimum fire/smoke box area as a fraction of the full frame (configurable via "MinObjectSize").
|
|
||||||
// Resolution-independent: rejects tiny boxes (the dominant small false-positive source).
|
|
||||||
static constexpr float DEFAULT_MIN_OBJECT_RATIO = 0.0003f; // 0.03% of frame area
|
|
||||||
|
|
||||||
private:
|
|
||||||
using ANSLIBPtr = std::unique_ptr<ANSCENTER::ANSLIB, decltype(&ANSCENTER::ANSLIB::Destroy)>;
|
using ANSLIBPtr = std::unique_ptr<ANSCENTER::ANSLIB, decltype(&ANSCENTER::ANSLIB::Destroy)>;
|
||||||
|
using Role = ansmind::firesmoke::Role;
|
||||||
|
using RectF = ansmind::RectF;
|
||||||
|
|
||||||
|
/// One detector verdict in normalised frame coordinates.
|
||||||
|
struct Det {
|
||||||
|
RectF box;
|
||||||
|
int classId = -1;
|
||||||
|
float conf = 0;
|
||||||
|
};
|
||||||
|
|
||||||
int engineType{ 0 };
|
|
||||||
ANSLIBPtr _motiondetector{ nullptr, &ANSCENTER::ANSLIB::Destroy };
|
|
||||||
ANSLIBPtr _detector{ nullptr, &ANSCENTER::ANSLIB::Destroy };
|
ANSLIBPtr _detector{ nullptr, &ANSCENTER::ANSLIB::Destroy };
|
||||||
ANSLIBPtr _filter{ nullptr, &ANSCENTER::ANSLIB::Destroy };
|
ANSLIBPtr _filter{ nullptr, &ANSCENTER::ANSLIB::Destroy };
|
||||||
|
|
||||||
std::recursive_mutex _mutex;
|
std::recursive_mutex _mutex;
|
||||||
cv::Rect _detectedArea;
|
|
||||||
cv::Rect _previousDetectedArea;
|
|
||||||
int _retainDetectedArea{ 0 };
|
|
||||||
bool _isFireNSmokeDetected{ false };
|
|
||||||
float _detectionScoreThreshold{ 0.5 };
|
|
||||||
std::vector<cv::Mat> _frameHistory;
|
|
||||||
std::deque <cv::Mat> _frameQueue;
|
|
||||||
std::string _filterLabels;
|
|
||||||
int _realFireCheck{ 0 };
|
|
||||||
bool _isRealFireFrame{ false };
|
|
||||||
double _hsvThreshold;
|
|
||||||
std::vector<Range> _smoke_colour;
|
|
||||||
std::vector<Range> _fire_colour;
|
|
||||||
|
|
||||||
// ----- Model config -----
|
// ----- the package's classes -----
|
||||||
int _detectorModelType;
|
std::vector<std::string> _labels; ///< detector classes, in model order
|
||||||
int _detectorDetectionType;
|
std::vector<Role> _roles; ///< fire / fake_fire / smoke / fake_smoke per class
|
||||||
int _filterModelType;
|
int _fireClass{ -1 }, _smokeClass{ -1 };
|
||||||
int _filterDetectionType;
|
std::string _fireName, _smokeName;
|
||||||
int _motionModelType;
|
std::vector<std::string> _filterLabels;
|
||||||
int _motionDetectionType;
|
std::vector<char> _vehicle, _screen; ///< which filter classes explain a look-alike away
|
||||||
|
|
||||||
// Add these constants at class level
|
// ----- the verifier and its knobs -----
|
||||||
int MIN_ROI_SIZE = 50;
|
ansmind::firesmoke::Verifier _verifier;
|
||||||
float SMOKE_CONFIDENCE_THRESHOLD = 0.7f;
|
ansmind::firesmoke::Params _p;
|
||||||
float DETECTION_CONFIDENCE_THRESHOLD = 0.5f;
|
int _zoomBudget{ 2 };
|
||||||
float MAX_AREA_OVERLAP = 0.5f;
|
double _maxFps{ 6.0 };
|
||||||
int MAX_MOTION_TRACKING = 10;
|
float _minObjectAreaRatio{ 0.0003f }; ///< a candidate box under this fraction of the frame is noise ("MinObjectSize", %)
|
||||||
int FILTER_VERIFICATION_FRAMES = 10;
|
bool _fsLog{ false };
|
||||||
int MIN_IMAGE_SIZE = 10;
|
std::vector<cv::Rect> _exclusiveROIs; ///< pixel rectangles no candidate may touch
|
||||||
float INCLUSIVE_IOU_THRESHOLD = 0.001f;
|
std::string _paramSignature; ///< what the last applyParams() saw
|
||||||
float MOTION_SENSITIVITY = 0.5f;
|
|
||||||
|
|
||||||
// Excluded filter class IDs
|
// ----- time -----
|
||||||
const std::unordered_set<int> EXCLUDED_FILTER_CLASSES =
|
std::chrono::steady_clock::time_point _t0{};
|
||||||
{
|
bool _clockStarted{ false };
|
||||||
13, // bench
|
double _externalClock{ -1 }; ///< a replay's video time, when set
|
||||||
59, // bed
|
double _lastRunT{ -1e9 };
|
||||||
60, // dining table
|
std::vector<CustomObject> _lastResults;
|
||||||
68, // microwave
|
uint64_t _frames{ 0 };
|
||||||
69, // oven
|
|
||||||
70 // toaster
|
|
||||||
};
|
|
||||||
|
|
||||||
cv::Size previousImageSize = cv::Size(0, 0);
|
// ----- steps of a frame -----
|
||||||
std::vector<ImageSection> cachedSections;
|
void applyParams();
|
||||||
int _currentPriority{ 0 };
|
bool runDetector(const cv::Mat& bgr, const cv::Rect& roi, std::vector<Det>& out, const std::string& camera);
|
||||||
bool _readROIs{ false };
|
bool runFilter(const cv::Mat& bgr, const cv::Rect& roi, std::vector<Det>& out, const std::string& camera);
|
||||||
std::vector<cv::Rect> _exclusiveROIs;
|
static void nmsClassAgnostic(std::vector<Det>& dets, float iouThr);
|
||||||
float _smokeDetetectionThreshold{ 0 };
|
ansmind::firesmoke::Cues cuesFor(const cv::Mat& bgr, const RectF& box) const;
|
||||||
float _motionSpecificity{ 0 };
|
static cv::Rect pixelRect(const RectF& r, int W, int H);
|
||||||
// Min fire/smoke box area as a fraction of the full frame; see DEFAULT_MIN_OBJECT_RATIO.
|
static RectF normRect(const cv::Rect& r, int W, int H);
|
||||||
float _minObjectAreaRatio{ DEFAULT_MIN_OBJECT_RATIO };
|
static float cover(const RectF& a, const RectF& b);
|
||||||
|
|
||||||
// Tracker-based voting state
|
|
||||||
std::unordered_map<int, TrackRecord> _trackHistory;
|
|
||||||
|
|
||||||
cv::Rect GenerateMinimumSquareBoundingBox(const std::vector<ANSCENTER::Object>& detectedObjects, int minSize = 640);
|
|
||||||
void UpdateNoDetectionCondition();
|
|
||||||
bool detectScreenFlicker(const std::vector<cv::Mat>& frames);
|
|
||||||
bool detectReflection(const cv::Mat& frame);
|
|
||||||
bool MajorityColourInFrame(cv::Mat frame, Range range, float area_threshold);
|
|
||||||
bool MajorityColourInFrame(cv::Mat frame, std::vector<Range> ranges, float area_threshold);
|
|
||||||
bool DetectFireNSmokeColourInFrame(const cv::Mat& frame, const cv::Rect& bBox, const std::vector<Range>& ranges, float area_threshold);
|
|
||||||
bool IsFireDetected(const cv::Mat image, const cv::Rect bBox);
|
|
||||||
bool IsSmokeDetected(const cv::Mat image, const cv::Rect bBox);
|
|
||||||
float calculateIoU(const cv::Rect& box1, const cv::Rect& box2);
|
|
||||||
bool IsOverlapping(const ANSCENTER::Object& obj, const std::vector<ANSCENTER::Object>& objectList, float iouThreshold = 0.5);
|
|
||||||
bool IsOverlapping(const ANSCENTER::Object& obj, const std::vector<cv::Rect>& objectList, float iouThreshold = 0.5);
|
|
||||||
|
|
||||||
bool IsROIOverlapping(const cv::Rect& obj, const std::vector<cv::Rect>& objectList, float iouThreshold = 0.5);
|
|
||||||
bool IsDetectedAreaValid(const cv::Rect& area) const;
|
|
||||||
bool IsObjectLargeEnough(const cv::Rect& box, const cv::Size& frameSize) const;
|
|
||||||
bool IsFireOrSmoke(int classId, float confidence) const;
|
|
||||||
bool ValidateMotionCorrelation(const std::vector<cv::Rect>& fireNSmokeRects) const;
|
|
||||||
void UpdatePositiveDetection();
|
|
||||||
void ResetDetectedArea();
|
|
||||||
void UpdatePriorityRegion(const cv::Mat& frame);
|
|
||||||
int CalculateOptimalCropSize(const cv::Mat& frame) const;
|
|
||||||
void RefineDetectedArea(const cv::Mat& frame, const ANSCENTER::Object& detection);
|
|
||||||
std::vector<CustomObject> ConvertToCustomObjects(const std::vector<ANSCENTER::Object>& objects);
|
|
||||||
void ResetDetectionState();
|
|
||||||
void GetModelParameters();
|
|
||||||
std::vector<ANSCENTER::Object> ProcessExistingDetectedArea(const cv::Mat& frame, const std::string& camera_id, cv::Mat& draw);
|
|
||||||
void AddConfirmedDetection(ANSCENTER::Object& detectedObj, std::vector<ANSCENTER::Object>& output);
|
|
||||||
#ifdef FNS_DEBUG
|
|
||||||
void DisplayDebugFrame(cv::Mat& draw) {
|
|
||||||
cv::resize(draw, draw, cv::Size(1920, (1920 * draw.rows) / draw.cols));
|
|
||||||
cv::imshow("Combined Detected Areas", draw);
|
|
||||||
cv::waitKey(1);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
// Function to separate screen size
|
|
||||||
double calculateDistanceToCenter(const cv::Point& center, const cv::Rect& rect);
|
|
||||||
std::vector<ImageSection> divideImage(const cv::Mat& image);
|
|
||||||
int getHighestPriorityRegion();
|
|
||||||
int getLowestPriorityRegion();
|
|
||||||
cv::Rect getRegionByPriority(int priority);
|
|
||||||
|
|
||||||
// Tracker-based voting methods (dual path: high-conf fast + low-conf slow)
|
|
||||||
void UpdateTrackHistory(int trackId, int classId, const cv::Rect& bbox, float confidence, double vMean);
|
|
||||||
bool IsTrackConfirmed(int trackId) const; // high-conf path
|
|
||||||
bool IsTrackLowConfirmed(int trackId) const; // low-conf path (fire only)
|
|
||||||
bool HasBboxMovement(int trackId) const;
|
|
||||||
bool HasFireFlicker(int trackId) const; // V-channel temporal std-dev gate
|
|
||||||
void AgeTracks(const std::unordered_set<int>& detectedTrackIds);
|
|
||||||
|
|
||||||
// Warm-color priority scan: re-rank cached sections by HSV warm-pixel count
|
|
||||||
void RankCachedSectionsByWarmColor(const cv::Mat& frame);
|
|
||||||
std::vector<ANSCENTER::Object> FindNewDetectedArea(
|
|
||||||
const cv::Mat& frame,
|
|
||||||
const std::string& camera_id, cv::Mat& draw);
|
|
||||||
|
|
||||||
std::vector<ANSCENTER::Object> FindMovementObjects(
|
|
||||||
const cv::Mat& frame,
|
|
||||||
const std::string& camera_id, cv::Mat& draw);
|
|
||||||
|
|
||||||
std::vector<ANSCENTER::Object> FindExcludedObjects(
|
|
||||||
const cv::Mat& frame,
|
|
||||||
const std::string& camera_id, cv::Mat& draw);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool Initialize(const std::string& modelDiretory, float detectionScoreThreshold, std::string& labelMap) override;
|
bool Initialize(const std::string& modelDiretory, float detectionScoreThreshold, std::string& labelMap) override;
|
||||||
@@ -179,6 +89,15 @@ public:
|
|||||||
std::vector<CustomObject> RunInference(const cv::Mat& input) override;
|
std::vector<CustomObject> RunInference(const cv::Mat& input) override;
|
||||||
std::vector<CustomObject> RunInference(const cv::Mat& input, const std::string& camera_id) override;
|
std::vector<CustomObject> RunInference(const cv::Mat& input, const std::string& camera_id) override;
|
||||||
bool Destroy() override;
|
bool Destroy() override;
|
||||||
|
|
||||||
|
/// Not part of the plugin contract. A replay harness hands the frame's
|
||||||
|
/// own time in seconds so the verifier's per-second evidence and gates
|
||||||
|
/// run on VIDEO time, not on how fast the machine decodes. Live callers
|
||||||
|
/// (ANSVIS, ans-custom-host) never call it and the steady clock is used.
|
||||||
|
void SetFrameClock(double seconds) { _externalClock = seconds; }
|
||||||
|
/// The verifier's per-track debug view after the last frame (replay harness).
|
||||||
|
std::vector<ansmind::firesmoke::TrackDebug> LastDebug() const { return _verifier.debug(); }
|
||||||
|
|
||||||
ANSCustomFS();
|
ANSCustomFS();
|
||||||
~ANSCustomFS();
|
~ANSCustomFS();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
# ANSCustomFireNSmokeDetection
|
||||||
|
|
||||||
|
The ANSVIS Fire & Smoke custom model (`ANSCustomFireNSmokeDetection.dll` inside
|
||||||
|
the `ANS_FireSmoke_v2.0` package), judged by the **same verifier the ANSMIND
|
||||||
|
`vision.fire_smoke` node runs**. ANSVIS loads the DLL into its server; ANSMIND
|
||||||
|
runs either its native node or this DLL through `ans-custom-host` (the Custom
|
||||||
|
Model node). Since 2026-11-01 all three judge a scene identically.
|
||||||
|
|
||||||
|
## Why it was rewritten
|
||||||
|
|
||||||
|
The first generation found a detection in one section of the frame, latched a
|
||||||
|
crop round it and voted on tracked boxes inside that crop. Measured on the
|
||||||
|
field recordings, that was itself the main false-alarm source:
|
||||||
|
|
||||||
|
* the detector needs **context** — a floodlight is `fake_fire` on the whole
|
||||||
|
frame and `fire` on a tight crop of the same lamp;
|
||||||
|
* the active window lost the rest of the picture, and a PTZ tour broke the
|
||||||
|
latch (the camera moved, the crop did not);
|
||||||
|
* the colour / flicker / movement gates were hand-tuned on a few clips and
|
||||||
|
did not survive the Tay Son PTZ: lit mist, statues, radar domes, a red flag.
|
||||||
|
|
||||||
|
ANSMIND's node replaced that with a whole-frame-first verifier whose every cue
|
||||||
|
is box-relative and carried by global motion (PTZ-safe), native-resolution
|
||||||
|
zoom re-checks for fire only, a look-alike filter, and a ladder of physical
|
||||||
|
gates measured on real and false recordings (`static`, `rigid`, `plain-red`,
|
||||||
|
`lamp`, `glare`, `travelling`, `night-brief`, `unlocalised`, `in-haze`,
|
||||||
|
`featureless`, `grain`, ...). That verifier is `vendor/engine/fire_smoke.hpp`,
|
||||||
|
pure C++ with no OpenCV, vendored verbatim from ANSMINDCORE (see
|
||||||
|
`vendor/README.txt` for the commit; re-copy to sync, never edit here).
|
||||||
|
|
||||||
|
## What the DLL does per frame
|
||||||
|
|
||||||
|
1. ANSLIB runs the package's `detector.onnx` (YOLO11m: fire / fake_fire /
|
||||||
|
smoke / fake_smoke) on the **whole frame** at a low floor
|
||||||
|
(`CandidateScore`, 0.15), then class-agnostic NMS: fire and fake_fire on the
|
||||||
|
same flame are one candidate, and which the model preferred is the evidence.
|
||||||
|
2. When there is a candidate, ANSLIB runs `filter.onnx` (COCO) on the frame;
|
||||||
|
a candidate inside a vehicle or a small screen is explained away by the
|
||||||
|
verifier.
|
||||||
|
3. Each candidate gets its box-relative cues (`patchCues`: flame hue, white
|
||||||
|
core, plain red, contrast, 24x24 grey patch for the change statistics).
|
||||||
|
4. For the few small **fire** candidates the verifier picks (`ZoomChecks`),
|
||||||
|
the detector runs again on a native crop (3x the box, at least 320 px,
|
||||||
|
never more than half the frame); the crop's verdict may only boost.
|
||||||
|
5. `Verifier::update` tracks, gates and decides. Only **confirmed** tracks
|
||||||
|
come back, as `CustomObject`s with the package's `fire` / `smoke` class,
|
||||||
|
the track id, and `extraInfo = fs_seconds=..;fs_track=..;evidence=..;held=..`,
|
||||||
|
every frame while they last.
|
||||||
|
|
||||||
|
The detector is not run faster than `MaxFps` (6): evidence is per second of
|
||||||
|
scene, and between runs the last verdict is returned unchanged.
|
||||||
|
|
||||||
|
## Parameters (`ConfigureParameters`)
|
||||||
|
|
||||||
|
| Name | Type | Default | Meaning |
|
||||||
|
|---|---|---|---|
|
||||||
|
| `Sensitivity` | float 0..1 | 0.5 | How much evidence an alarm needs: under 0.34 = the node's `low`, over 0.66 = `high`, else `balanced`. The physical gates never move. |
|
||||||
|
| `SmokeSeconds` | float 0..600 | 0 | Seconds smoke must have been seen **at the place** before it may alarm (wildfire / PTZ landscapes: 20). |
|
||||||
|
| `DetectFire` / `DetectSmoke` | Boolean | true | Which families to watch. |
|
||||||
|
| `ZoomChecks` | float 0..8 | 2 | Native-resolution re-checks per frame (fire only). |
|
||||||
|
| `MaxFps` | float 1..30 | 6 | Detector rate ceiling in frames of scene time per second. |
|
||||||
|
| `CandidateScore` | float 0..1 | 0.15 | Detector floor for a candidate; the alarm threshold is the verifier's evidence, never this. |
|
||||||
|
| `MinObjectSize` | float 0..10 | 0.03 | Minimum candidate box size as a percentage of the frame area; smaller boxes are noise. |
|
||||||
|
| `ExclusiveROIs` | rectangles | none | Candidates touching one are dropped. |
|
||||||
|
|
||||||
|
The threshold ANSVIS opens the model with is not an alarm threshold either;
|
||||||
|
a confirmed alarm is reported at least at that confidence so no downstream
|
||||||
|
score filter can drop it.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```
|
||||||
|
cmake -S . -B build-vs -G "Visual Studio 18 2026" -A x64 # from the repo root
|
||||||
|
cmake --build build-vs --config Release --target ANSCustomFireNSmokeDetection FireNSmokeDetection_Run FireNSmokeDetection_Tests
|
||||||
|
```
|
||||||
|
|
||||||
|
Links `ANSLIB` (`C:\ProgramData\ANSCENTER\Shared`) and `opencv_world4130`
|
||||||
|
(`C:\ANSLibs\opencv`). The DLL and the harnesses load the ANS Runtime Engine's
|
||||||
|
DLLs from `C:\ProgramData\ANSCENTER\Shared`, so put that folder on `PATH` to
|
||||||
|
run them.
|
||||||
|
|
||||||
|
## Replay harness (parity with ANSMIND's `firesmoke-run`)
|
||||||
|
|
||||||
|
```
|
||||||
|
set PATH=C:\ProgramData\ANSCENTER\Shared;%PATH%
|
||||||
|
build-vs\bin\Release\FireNSmokeDetection_Run.exe <model dir> <video> [--max-fps 6] [--sensitivity 0.5] [--smoke-seconds 0] [--start S] [--end S] [--log]
|
||||||
|
```
|
||||||
|
|
||||||
|
Feeds the recording on the **video's** clock (`SetFrameClock`, a harness-only
|
||||||
|
method outside the plugin contract) and prints each alarm once. The model
|
||||||
|
dir is the decrypted package folder (`detector.onnx`, `detector.names`,
|
||||||
|
`filter.onnx`, `filter.names`, `Categories/`); hand it a backslash path —
|
||||||
|
ANSCORE's engine-path code splits on `/` only. The first run on a GPU builds
|
||||||
|
TensorRT engines (minutes); they cache beside the models.
|
||||||
|
|
||||||
|
`ANSCUSTOM_FS_LOG=1` (or `--log`) prints the verifier's per-track cue line
|
||||||
|
per frame to stderr, the same line the ANSMIND node logs with `ANSMIND_FS_LOG`.
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Vendored verbatim from ANSMINDCORE aeaed99 (src/engine/fire_smoke.hpp, src/core/detection.hpp).
|
||||||
|
fire_smoke.hpp is the Fire & Smoke verifier the ANSMIND vision.fire_smoke node runs; this DLL runs the same code
|
||||||
|
so the ANSVIS custom model and the ANSMIND node judge a scene identically. Re-copy both files to sync; do not edit here.
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
#pragma once
|
||||||
|
// core/detection — the detection currency of the pipeline.
|
||||||
|
//
|
||||||
|
// Coordinates are NORMALIZED [0,1] of the source frame, always. ANSCORE mixed
|
||||||
|
// pixel- and crop-relative coordinates and the ALPRChecker false-merge bug
|
||||||
|
// (§6.3) was the price; one coordinate space ends that class of bug. Pixel
|
||||||
|
// mapping happens only at the edges (pre-processing in, overlay/evidence out).
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ansmind {
|
||||||
|
|
||||||
|
struct RectF {
|
||||||
|
float x = 0, y = 0, w = 0, h = 0; // top-left + size, normalized
|
||||||
|
|
||||||
|
float area() const { return w > 0 && h > 0 ? w * h : 0.f; }
|
||||||
|
float iou(const RectF& o) const {
|
||||||
|
const float x1 = x > o.x ? x : o.x;
|
||||||
|
const float y1 = y > o.y ? y : o.y;
|
||||||
|
const float x2 = (x + w) < (o.x + o.w) ? (x + w) : (o.x + o.w);
|
||||||
|
const float y2 = (y + h) < (o.y + o.h) ? (y + h) : (o.y + o.h);
|
||||||
|
if (x2 <= x1 || y2 <= y1) return 0.f;
|
||||||
|
const float inter = (x2 - x1) * (y2 - y1);
|
||||||
|
return inter / (area() + o.area() - inter);
|
||||||
|
}
|
||||||
|
RectF clamped() const {
|
||||||
|
RectF r = *this;
|
||||||
|
if (r.x < 0) { r.w += r.x; r.x = 0; }
|
||||||
|
if (r.y < 0) { r.h += r.y; r.y = 0; }
|
||||||
|
if (r.x + r.w > 1) r.w = 1 - r.x;
|
||||||
|
if (r.y + r.h > 1) r.h = 1 - r.y;
|
||||||
|
if (r.w < 0) r.w = 0;
|
||||||
|
if (r.h < 0) r.h = 0;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// A resolved attribute attached by the TrackResolver (§6.2) — identity,
|
||||||
|
// plate, helmet… Value "unknown" is first-class (the stranger case).
|
||||||
|
struct DetAttr {
|
||||||
|
std::string key;
|
||||||
|
std::string value;
|
||||||
|
float confidence = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Detection {
|
||||||
|
RectF box;
|
||||||
|
float confidence = 0;
|
||||||
|
int classId = -1;
|
||||||
|
std::string className;
|
||||||
|
std::string nodeId; // which graph node produced it
|
||||||
|
uint64_t trackId = 0; // 0 = untracked
|
||||||
|
int parentIndex = -1; // RegionNode: index of the parent detection
|
||||||
|
std::vector<DetAttr> attrs; // TrackResolver output; rules bind to these
|
||||||
|
// Landmarks in NORMALIZED frame coords (x0,y0,…x4,y4 for SCRFD's 5-point
|
||||||
|
// faces; empty when the detector has none). The embed node aligns on
|
||||||
|
// these — the ANSFR accuracy lever.
|
||||||
|
std::vector<float> kps;
|
||||||
|
// Per-keypoint confidence, parallel to kps (one per x,y pair; empty when
|
||||||
|
// the detector gives none — SCRFD's five landmarks carry no score). Pose
|
||||||
|
// models (YOLOv8-pose, RF-DETR keypoints) fill it: consumers hide a joint
|
||||||
|
// the model could not find rather than drawing it at a guessed spot.
|
||||||
|
std::vector<float> kpConf;
|
||||||
|
// Instance mask, CROPPED TO THE BOX at the seg head's proto resolution
|
||||||
|
// (0/255 bytes, row-major maskW×maskH; empty for plain detectors).
|
||||||
|
// Deliberately small — a proto crop is ≤160px per side — consumers
|
||||||
|
// upscale to the box when they need pixels.
|
||||||
|
std::vector<uint8_t> mask;
|
||||||
|
uint16_t maskW = 0, maskH = 0;
|
||||||
|
std::vector<float> embedding; // engine-internal transient (embed nodes)
|
||||||
|
};
|
||||||
|
|
||||||
|
using DetectionSet = std::vector<Detection>;
|
||||||
|
|
||||||
|
} // namespace ansmind
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -99,3 +99,18 @@ target_link_libraries(FireNSmokeDetection_HeadlessCheck PRIVATE
|
|||||||
if(MSVC)
|
if(MSVC)
|
||||||
target_compile_options(FireNSmokeDetection_HeadlessCheck PRIVATE /W3 /sdl /permissive-)
|
target_compile_options(FireNSmokeDetection_HeadlessCheck PRIVATE /W3 /sdl /permissive-)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 4. Headless replay harness on the video's clock (parity with ANSMIND's firesmoke-run)
|
||||||
|
# ============================================================================
|
||||||
|
add_executable(FireNSmokeDetection_Run
|
||||||
|
run_main.cpp
|
||||||
|
)
|
||||||
|
target_compile_features(FireNSmokeDetection_Run PRIVATE cxx_std_17)
|
||||||
|
target_compile_definitions(FireNSmokeDetection_Run PRIVATE ${COMMON_DEFS})
|
||||||
|
target_include_directories(FireNSmokeDetection_Run PRIVATE ${COMMON_INCLUDES})
|
||||||
|
target_link_directories(FireNSmokeDetection_Run PRIVATE ${ANSLIB_LIB_DIR} ${OPENCV_LIB_DIR})
|
||||||
|
target_link_libraries(FireNSmokeDetection_Run PRIVATE ANSLIB opencv_world4130 ANSCustomFireNSmokeDetection)
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_options(FireNSmokeDetection_Run PRIVATE /W3 /sdl /permissive-)
|
||||||
|
endif()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "TestCommon.h"
|
#include "TestCommon.h"
|
||||||
|
#include <algorithm>
|
||||||
#include "ANSCustomFireNSmoke.h"
|
#include "ANSCustomFireNSmoke.h"
|
||||||
|
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
@@ -48,26 +49,24 @@ TEST_F(FireNSmokeUnitTest, ConfigureParametersReturnsValidConfig) {
|
|||||||
EXPECT_EQ(params.ROI_Config[0].MinItems, 0);
|
EXPECT_EQ(params.ROI_Config[0].MinItems, 0);
|
||||||
EXPECT_EQ(params.ROI_Config[0].MaxItems, 20);
|
EXPECT_EQ(params.ROI_Config[0].MaxItems, 20);
|
||||||
|
|
||||||
// Should have SmokeScore and Sensitivity parameters
|
// The verifier's knobs: Sensitivity (0..1 slider onto the node's
|
||||||
ASSERT_GE(params.Parameters.size(), 2u);
|
// low/balanced/high), SmokeSeconds, DetectFire/DetectSmoke, ZoomChecks,
|
||||||
|
// MaxFps, CandidateScore.
|
||||||
bool hasSmokeScore = false;
|
std::vector<std::string> names;
|
||||||
bool hasSensitivity = false;
|
for (const auto& p : params.Parameters) names.push_back(p.Name);
|
||||||
|
for (const char* want : {"Sensitivity", "SmokeSeconds", "DetectFire", "DetectSmoke", "ZoomChecks", "MaxFps", "CandidateScore", "MinObjectSize"})
|
||||||
|
EXPECT_NE(std::find(names.begin(), names.end(), want), names.end()) << "Missing parameter " << want;
|
||||||
for (const auto& p : params.Parameters) {
|
for (const auto& p : params.Parameters) {
|
||||||
if (p.Name == "SmokeScore") {
|
if (p.Name == "Sensitivity") {
|
||||||
hasSmokeScore = true;
|
|
||||||
EXPECT_EQ(p.DataType, "float");
|
EXPECT_EQ(p.DataType, "float");
|
||||||
EXPECT_EQ(p.MaxValue, 1);
|
EXPECT_EQ(p.MaxValue, 1);
|
||||||
EXPECT_EQ(p.MinValue, 0);
|
EXPECT_EQ(p.MinValue, 0);
|
||||||
|
EXPECT_EQ(p.DefaultValue, "0.5");
|
||||||
}
|
}
|
||||||
if (p.Name == "Sensitivity") {
|
if (p.Name == "DetectFire" || p.Name == "DetectSmoke") EXPECT_EQ(p.DataType, "Boolean");
|
||||||
hasSensitivity = true;
|
if (p.Name == "MaxFps") EXPECT_EQ(p.DefaultValue, "6");
|
||||||
EXPECT_EQ(p.DataType, "float");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EXPECT_TRUE(hasSmokeScore) << "Missing SmokeScore parameter";
|
|
||||||
EXPECT_TRUE(hasSensitivity) << "Missing Sensitivity parameter";
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FireNSmokeUnitTest, DestroySucceeds) {
|
TEST_F(FireNSmokeUnitTest, DestroySucceeds) {
|
||||||
EXPECT_TRUE(detector.Destroy());
|
EXPECT_TRUE(detector.Destroy());
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
// FireNSmokeDetection_Run — replay a recording through the Fire & Smoke
|
||||||
|
// custom model the way ANSVIS and ans-custom-host would feed it live, on the
|
||||||
|
// VIDEO's clock, and print each alarm once as it is confirmed. Headless; this
|
||||||
|
// is the parity harness against ANSMIND's `firesmoke-run` (same verifier).
|
||||||
|
//
|
||||||
|
// FireNSmokeDetection_Run <model dir> <video> [--max-fps N] [--sensitivity F]
|
||||||
|
// [--smoke-seconds N] [--start S] [--end S] [--log]
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
|
||||||
|
#include "ANSCustomFireNSmoke.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc < 3) {
|
||||||
|
std::fprintf(stderr,
|
||||||
|
"usage: FireNSmokeDetection_Run <model dir> <video> [--max-fps N] [--sensitivity F]\n"
|
||||||
|
" [--smoke-seconds N] [--start S] [--end S] [--log]\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
const std::string modelDir = argv[1];
|
||||||
|
const std::string video = argv[2];
|
||||||
|
double maxFps = 6, startS = 0, endS = 1e9;
|
||||||
|
std::string sensitivity = "0.5", smokeSeconds = "0";
|
||||||
|
bool log = false;
|
||||||
|
for (int i = 3; i < argc; ++i) {
|
||||||
|
const std::string a = argv[i];
|
||||||
|
auto next = [&](const char* what) -> std::string {
|
||||||
|
if (i + 1 >= argc) { std::fprintf(stderr, "%s needs a value\n", what); std::exit(2); }
|
||||||
|
return argv[++i];
|
||||||
|
};
|
||||||
|
if (a == "--max-fps") maxFps = std::stod(next("--max-fps"));
|
||||||
|
else if (a == "--sensitivity") sensitivity = next("--sensitivity");
|
||||||
|
else if (a == "--smoke-seconds") smokeSeconds = next("--smoke-seconds");
|
||||||
|
else if (a == "--start") startS = std::stod(next("--start"));
|
||||||
|
else if (a == "--end") endS = std::stod(next("--end"));
|
||||||
|
else if (a == "--log") log = true;
|
||||||
|
else { std::fprintf(stderr, "unknown option %s\n", a.c_str()); return 2; }
|
||||||
|
}
|
||||||
|
if (log) _putenv_s("ANSCUSTOM_FS_LOG", "1");
|
||||||
|
|
||||||
|
ANSCustomFS model;
|
||||||
|
std::string labelMap;
|
||||||
|
const auto t0 = std::chrono::steady_clock::now();
|
||||||
|
if (!model.Initialize(modelDir, 0.5f, labelMap)) {
|
||||||
|
std::fprintf(stderr, "Initialize failed for %s\n", modelDir.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const double initMs = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
|
||||||
|
std::printf("model %s\nclasses %s\ninit %.0f ms\n", modelDir.c_str(), labelMap.c_str(), initMs);
|
||||||
|
|
||||||
|
// Parameters the way the host pushes them: schema from the plugin, values set.
|
||||||
|
CustomParams params;
|
||||||
|
model.ConfigureParameters(params);
|
||||||
|
for (auto& p : params.Parameters) {
|
||||||
|
if (p.Name == "MaxFps") p.Value = std::to_string(int(maxFps));
|
||||||
|
else if (p.Name == "Sensitivity") p.Value = sensitivity;
|
||||||
|
else if (p.Name == "SmokeSeconds") p.Value = smokeSeconds;
|
||||||
|
}
|
||||||
|
model.SetParameters(params);
|
||||||
|
std::printf("rate %.0f fps of video time, sensitivity %s, smoke_seconds %s\n\n", maxFps,
|
||||||
|
sensitivity.c_str(), smokeSeconds.c_str());
|
||||||
|
|
||||||
|
cv::VideoCapture cap(video);
|
||||||
|
if (!cap.isOpened()) {
|
||||||
|
std::fprintf(stderr, "cannot open %s\n", video.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const double fps = cap.get(cv::CAP_PROP_FPS) > 1 ? cap.get(cv::CAP_PROP_FPS) : 25.0;
|
||||||
|
long decoded = 0, processed = 0;
|
||||||
|
double lastT = 0, msSum = 0;
|
||||||
|
size_t alarms = 0;
|
||||||
|
std::map<int, double> firstSeen; // track -> first alarm time
|
||||||
|
std::map<int, double> lastSeen;
|
||||||
|
cv::Mat frame;
|
||||||
|
while (cap.read(frame)) {
|
||||||
|
const double t = double(decoded) / fps;
|
||||||
|
++decoded;
|
||||||
|
if (t < startS) continue;
|
||||||
|
if (t > endS) break;
|
||||||
|
model.SetFrameClock(t);
|
||||||
|
const auto a = std::chrono::steady_clock::now();
|
||||||
|
const std::vector<CustomObject> objs = model.RunInference(frame, "replay");
|
||||||
|
const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - a).count();
|
||||||
|
// The plugin holds its last verdict between detector runs; count a
|
||||||
|
// processed frame by the clock the plugin honours.
|
||||||
|
if (ms > 0.5) { ++processed; msSum += ms; }
|
||||||
|
for (const auto& o : objs) {
|
||||||
|
lastSeen[o.trackId] = t;
|
||||||
|
if (firstSeen.count(o.trackId)) continue;
|
||||||
|
firstSeen[o.trackId] = t;
|
||||||
|
++alarms;
|
||||||
|
std::printf("ALARM %-5s t=%7.2fs track %-4d conf=%.2f box=%d,%d %dx%d %s\n",
|
||||||
|
o.className.c_str(), t, o.trackId, o.confidence, o.box.x, o.box.y, o.box.width,
|
||||||
|
o.box.height, o.extraInfo.c_str());
|
||||||
|
std::fflush(stdout);
|
||||||
|
}
|
||||||
|
lastT = t;
|
||||||
|
}
|
||||||
|
size_t held = 0;
|
||||||
|
for (const auto& d : model.LastDebug())
|
||||||
|
if (!d.confirmed) ++held;
|
||||||
|
std::printf("\n%ld frames decoded, %ld processed over %.1fs of video - %zu alarm(s), %zu track(s) held back\n",
|
||||||
|
decoded, processed, lastT, alarms, held);
|
||||||
|
if (processed) std::printf("per processed frame: %.1f ms mean\n", msSum / double(processed));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user