diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index d622bd8..96a2bd0 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -50,7 +50,17 @@
"Bash(tr ';' '\\\\n')",
"Bash(ls -la \"C:/ProgramData/ANSCENTER/DLHUB_Runtime_Engine/\"*.dll)",
"Bash(cp \"C:/ProgramData/ANSCENTER/Shared/ANSLicensingSystem.dll\" \"C:/ProgramData/ANSCENTER/DLHUB_Runtime_Engine/ANSLicensingSystem.dll\")",
- "Bash(cp \"C:/ProgramData/ANSCENTER/Shared/anslicensing.dll\" \"C:/ProgramData/ANSCENTER/DLHUB_Runtime_Engine/anslicensing.dll\")"
+ "Bash(cp \"C:/ProgramData/ANSCENTER/Shared/anslicensing.dll\" \"C:/ProgramData/ANSCENTER/DLHUB_Runtime_Engine/anslicensing.dll\")",
+ "Bash(cmake --build cmake-build-release --target ANSCustomFireNSmokeDetection)",
+ "Bash(cmd.exe /c \"call \\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" >nul 2>&1 && cmake --build cmake-build-release --target ANSCustomFireNSmokeDetection\")",
+ "Bash(cmd.exe //c \"call \\\\\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\\\\\" >nul && cmake --build cmake-build-release --target ANSCustomFireNSmokeDetection 2>&1\")",
+ "Bash(cmd.exe /c C:/Projects/CLionProjects/ANSCustomModels/_build_fns.bat)",
+ "Bash(cmd.exe /c 'C:\\\\Projects\\\\CLionProjects\\\\ANSCustomModels\\\\_build_fns.bat')",
+ "Bash(/c/Projects/CLionProjects/ANSCustomModels/_build_fns.bat)",
+ "PowerShell($dll = \"C:\\\\ProgramData\\\\ANSCENTER\\\\Shared\\\\ANSLibsLoader.dll\"; if \\(Test-Path $dll\\) { Get-Item $dll | Select-Object Name, Length, LastWriteTime, VersionInfo | Format-List } else { \"DLL not found at $dll\" })",
+ "PowerShell($bytes = [System.IO.File]::ReadAllBytes\\(\"C:\\\\ProgramData\\\\ANSCENTER\\\\Shared\\\\ANSLibsLoader.dll\"\\); $text = [System.Text.Encoding]::ASCII.GetString\\($bytes\\); if \\($text -match \"ANSCENTER_DebugLog\"\\) { \"FOUND: ANSCENTER_DebugLog symbol present in DLL\" } else { \"MISSING: ANSCENTER_DebugLog NOT present in DLL\" }; if \\($text -match \"ANSCENTER_IsDebugViewEnabled\"\\) { \"FOUND: ANSCENTER_IsDebugViewEnabled symbol present\" } else { \"MISSING: ANSCENTER_IsDebugViewEnabled NOT present\" })",
+ "PowerShell($bytes = [System.IO.File]::ReadAllBytes\\(\"C:\\\\ProgramData\\\\ANSCENTER\\\\Shared\\\\ANSLIB.dll\"\\); $text = [System.Text.Encoding]::ASCII.GetString\\($bytes\\); $matches = [regex]::Matches\\($text, \"ANSCENTER_[A-Za-z]+\"\\); $matches | ForEach-Object { $_.Value } | Sort-Object -Unique)",
+ "Bash(cmake --build C:/Projects/CLionProjects/ANSCustomModels/cmake-build-release --target FireNSmokeDetection_Demo --config Release)"
]
}
}
diff --git a/.idea/editor.xml b/.idea/editor.xml
index fef3201..53a15f5 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -139,6 +139,7 @@
+
diff --git a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp
index 6c6f661..821d80c 100644
--- a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp
+++ b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp
@@ -20,15 +20,71 @@ void ANSCustomFS::ResetDetectedArea() {
}
void ANSCustomFS::UpdatePriorityRegion(const cv::Mat& frame)
{
+ // divideImage caches geometry by frame size — it's cheap when the size is stable.
std::vector sections = divideImage(frame);
- int lowestPriority = getLowestPriorityRegion();
- if (_currentPriority > lowestPriority || _currentPriority == 0) {
+ if (cachedSections.empty()) {
+ _currentPriority = 0;
+ return;
+ }
+ const int lowestPriority = getLowestPriorityRegion();
+
+ if (_currentPriority == 0 || _currentPriority >= lowestPriority) {
+ // Start of a new scan cycle — re-rank sections by warm-HSV pixel count.
+ // This steers the first-pass scan straight to the likely fire regions
+ // (reduces "late detection" compared to the old distance-to-center order).
+ RankCachedSectionsByWarmColor(frame);
_currentPriority = getHighestPriorityRegion();
}
else {
_currentPriority++;
}
}
+
+void ANSCustomFS::RankCachedSectionsByWarmColor(const cv::Mat& frame)
+{
+ if (cachedSections.empty() || frame.empty()) return;
+
+ try {
+ cv::Mat hsv;
+ cv::cvtColor(frame, hsv, cv::COLOR_BGR2HSV);
+
+ // Warm color = red / orange / yellow hues with enough saturation and brightness.
+ // OpenCV H is 0..179, so we cover the red wrap with two ranges.
+ cv::Mat mask1, mask2, warmMask;
+ cv::inRange(hsv,
+ cv::Scalar(0, 100, 150), cv::Scalar(30, 255, 255), mask1);
+ cv::inRange(hsv,
+ cv::Scalar(160, 100, 150), cv::Scalar(180, 255, 255), mask2);
+ warmMask = mask1 | mask2;
+
+ const cv::Rect frameRect(0, 0, frame.cols, frame.rows);
+ std::vector> scored; // (warmCount, sectionIndex)
+ scored.reserve(cachedSections.size());
+
+ for (size_t i = 0; i < cachedSections.size(); ++i) {
+ const cv::Rect safe = cachedSections[i].region & frameRect;
+ int count = 0;
+ if (safe.area() > 0) {
+ count = cv::countNonZero(warmMask(safe));
+ }
+ scored.emplace_back(count, static_cast(i));
+ }
+
+ // Sort descending by warm count; tie-break by distance-to-center (original order).
+ std::stable_sort(scored.begin(), scored.end(),
+ [](const std::pair& a, const std::pair& b) {
+ return a.first > b.first;
+ });
+
+ // Rewrite priorities 1..N into cachedSections so getRegionByPriority picks warmest first.
+ for (size_t rank = 0; rank < scored.size(); ++rank) {
+ cachedSections[scored[rank].second].priority = static_cast(rank + 1);
+ }
+ }
+ catch (const cv::Exception&) {
+ // On failure, leave existing priorities untouched (fallback to prior order).
+ }
+}
int ANSCustomFS::CalculateOptimalCropSize(const cv::Mat& frame) const {
int maxDim = std::max(frame.cols, frame.rows);
if (maxDim > 1920) return 640;
@@ -58,16 +114,22 @@ void ANSCustomFS::ResetDetectionState() {
_isFireNSmokeDetected = false;
_trackHistory.clear();
}
-// --- Tracker-based voting functions ---
+// --- Tracker-based voting functions (dual-path: high-conf + low-conf) ---
+
+void ANSCustomFS::UpdateTrackHistory(int trackId, int classId, const cv::Rect& bbox,
+ float confidence, double vMean) {
+ const int isHighConf = (confidence >= HIGH_CONF_THRESHOLD) ? 1 : 0;
-void ANSCustomFS::UpdateTrackHistory(int trackId, int classId, const cv::Rect& bbox) {
auto it = _trackHistory.find(trackId);
if (it == _trackHistory.end()) {
TrackRecord record;
record.trackId = trackId;
record.classId = classId;
record.bboxHistory.push_back(bbox);
- record.detectedCount = 1;
+ record.highConfHits.push_back(isHighConf);
+ record.vMeanHistory.push_back(vMean);
+ record.detectedCount = isHighConf;
+ record.lowConfDetectedCount = 1;
record.totalFrames = 1;
record.confirmed = false;
_trackHistory[trackId] = std::move(record);
@@ -75,16 +137,23 @@ void ANSCustomFS::UpdateTrackHistory(int trackId, int classId, const cv::Rect& b
else {
auto& record = it->second;
record.bboxHistory.push_back(bbox);
- record.detectedCount++;
+ record.highConfHits.push_back(isHighConf);
+ record.vMeanHistory.push_back(vMean);
+ if (isHighConf) record.detectedCount++;
+ record.lowConfDetectedCount++;
record.totalFrames++;
- // Slide the window: keep only VOTE_WINDOW entries in bbox history
+ // Slide the window: keep only VOTE_WINDOW entries in parallel deques
while (static_cast(record.bboxHistory.size()) > VOTE_WINDOW) {
record.bboxHistory.pop_front();
- record.detectedCount = std::max(0, record.detectedCount - 1);
+ const int oldHit = record.highConfHits.front();
+ record.highConfHits.pop_front();
+ record.vMeanHistory.pop_front();
+ if (oldHit == 1) record.detectedCount = std::max(0, record.detectedCount - 1);
+ record.lowConfDetectedCount = std::max(0, record.lowConfDetectedCount - 1);
}
- // Update confirmed status
+ // Update confirmed status (high-conf path)
if (record.detectedCount >= VOTE_THRESHOLD) {
record.confirmed = true;
}
@@ -97,6 +166,39 @@ bool ANSCustomFS::IsTrackConfirmed(int trackId) const {
return it->second.detectedCount >= VOTE_THRESHOLD;
}
+bool ANSCustomFS::IsTrackLowConfirmed(int trackId) const {
+ auto it = _trackHistory.find(trackId);
+ if (it == _trackHistory.end()) return false;
+ // Low-conf path requires more total hits than the high-conf window would need
+ // (since we accept weaker detections, we compensate with more votes)
+ return it->second.lowConfDetectedCount >= LOW_VOTE_THRESHOLD;
+}
+
+bool ANSCustomFS::HasFireFlicker(int trackId) const {
+ auto it = _trackHistory.find(trackId);
+ if (it == _trackHistory.end()) return false;
+
+ const auto& hist = it->second.vMeanHistory;
+ if (static_cast(hist.size()) < FLICKER_MIN_SAMPLES) return false;
+
+ // Compute mean
+ double sum = 0.0;
+ for (double v : hist) sum += v;
+ const double mean = sum / static_cast(hist.size());
+
+ // Compute std-dev
+ double sqSum = 0.0;
+ for (double v : hist) {
+ const double d = v - mean;
+ sqSum += d * d;
+ }
+ const double stddev = std::sqrt(sqSum / static_cast(hist.size()));
+
+ // Real fire flickers at ~3–10 Hz, driving measurable luma variance.
+ // Sunlight reflections, static bright surfaces, and highlights do not.
+ return stddev > FLICKER_V_STD_THRESHOLD;
+}
+
bool ANSCustomFS::HasBboxMovement(int trackId) const {
auto it = _trackHistory.find(trackId);
if (it == _trackHistory.end()) return false;
@@ -140,17 +242,26 @@ void ANSCustomFS::AgeTracks(const std::unordered_set& detectedTrackIds) {
// Track was NOT detected this frame
it->second.totalFrames++;
- // Remove stale tracks that haven't been seen recently
+ // Remove stale tracks that haven't been seen recently (both paths empty)
if (it->second.totalFrames > VOTE_WINDOW &&
- it->second.detectedCount == 0) {
+ it->second.detectedCount == 0 &&
+ it->second.lowConfDetectedCount == 0) {
it = _trackHistory.erase(it);
continue;
}
- // Age out the sliding window (add a "miss" frame)
+ // Age out the sliding window (add a "miss" frame) — drop oldest from all parallel deques
if (static_cast(it->second.bboxHistory.size()) >= VOTE_WINDOW) {
it->second.bboxHistory.pop_front();
- it->second.detectedCount = std::max(0, it->second.detectedCount - 1);
+ if (!it->second.highConfHits.empty()) {
+ const int oldHit = it->second.highConfHits.front();
+ it->second.highConfHits.pop_front();
+ if (oldHit == 1) it->second.detectedCount = std::max(0, it->second.detectedCount - 1);
+ }
+ if (!it->second.vMeanHistory.empty()) {
+ it->second.vMeanHistory.pop_front();
+ }
+ it->second.lowConfDetectedCount = std::max(0, it->second.lowConfDetectedCount - 1);
}
}
++it;
@@ -256,28 +367,55 @@ std::vector ANSCustomFS::FindNewDetectedArea(
return output;
}
- // Find best detection and refine area
+ // Find best detection and refine area.
+ // Two-tier candidacy:
+ // 1) High-conf (>= DETECTION_CONFIDENCE_THRESHOLD) — latches ROI and emits immediately.
+ // 2) Low-conf fire with fire-color (>= LOW_CONF_FIRE_THRESHOLD) — latches ROI only,
+ // so ProcessExistingDetectedArea can run voting next frame without false positives
+ // from initial single-frame emission.
float maxScore = 0.0f;
ANSCENTER::Object* bestDetection = nullptr;
+ bool bestIsHighConf = false;
for (auto& detectedObj : detectedObjects) {
detectedObj.box.x += _detectedArea.x;
detectedObj.box.y += _detectedArea.y;
detectedObj.cameraId = camera_id;
- if (detectedObj.confidence > DETECTION_CONFIDENCE_THRESHOLD &&
- IsFireOrSmoke(detectedObj.classId, detectedObj.confidence) &&
- detectedObj.confidence > maxScore) {
+ if (!IsFireOrSmoke(detectedObj.classId, detectedObj.confidence)) continue;
- maxScore = detectedObj.confidence;
- bestDetection = &detectedObj;
+ const bool isHighConf = (detectedObj.confidence > DETECTION_CONFIDENCE_THRESHOLD);
+ const bool isLowConfFire =
+ (detectedObj.classId == 0) && (detectedObj.confidence >= LOW_CONF_FIRE_THRESHOLD);
+
+ if (isHighConf) {
+ // Prefer high-conf; always overrides a low-conf candidate
+ if (!bestIsHighConf || detectedObj.confidence > maxScore) {
+ maxScore = detectedObj.confidence;
+ bestDetection = &detectedObj;
+ bestIsHighConf = true;
+ }
+ }
+ else if (isLowConfFire && !bestIsHighConf) {
+ // Gate low-conf candidates on fire-color presence in the bbox
+ if (!IsFireDetected(frame, detectedObj.box)) continue;
+ if (detectedObj.confidence > maxScore) {
+ maxScore = detectedObj.confidence;
+ bestDetection = &detectedObj;
+ // bestIsHighConf stays false
+ }
}
}
if (bestDetection != nullptr) {
RefineDetectedArea(frame, *bestDetection);
- output.push_back(*bestDetection);
- UpdatePositiveDetection();
+ if (bestIsHighConf) {
+ // Fast path: emit the first-frame detection as before
+ output.push_back(*bestDetection);
+ UpdatePositiveDetection();
+ }
+ // Low-conf path: commit ROI only. Voting in ProcessExistingDetectedArea
+ // will decide over subsequent frames whether to emit.
}
else {
ResetDetectedArea();
@@ -514,9 +652,12 @@ bool ANSCustomFS::Initialize(const std::string& modelDirectory, float detectionS
std::string licenseKey = "";
std::string motionDetectorLabels;
- // Load detector model (fire/smoke detector)
- float detScoreThreshold = _detectionScoreThreshold;
- float detConfThreshold = 0.5f;
+ // Load detector model (fire/smoke detector).
+ // Floor thresholds at LOW_CONF_FIRE_THRESHOLD so the detector emits low-confidence
+ // detections that the dual-path voting in ProcessExistingDetectedArea can evaluate.
+ // The user-facing _detectionScoreThreshold is still enforced in code.
+ float detScoreThreshold = LOW_CONF_FIRE_THRESHOLD;
+ float detConfThreshold = LOW_CONF_FIRE_THRESHOLD;
float detNMSThreshold = 0.5f;
int detResult = _detector->LoadModelFromFolder(
licenseKey.c_str(),
@@ -621,53 +762,6 @@ cv::Rect ANSCustomFS::GenerateMinimumSquareBoundingBox(const std::vector& frameQueue) {
- return false;
- if (frameQueue.size() < 10) return false;
- double totalFlow = 0.0;
- int count = 0;
-
- for (size_t i = 2; i < frameQueue.size(); i += 3) {
- cv::Mat prevGray, currGray;
- cv::resize(frameQueue[i - 2], prevGray, cv::Size(), 0.5, 0.5);
- cv::resize(frameQueue[i], currGray, cv::Size(), 0.5, 0.5);
- cv::cvtColor(prevGray, prevGray, cv::COLOR_BGR2GRAY);
- cv::cvtColor(currGray, currGray, cv::COLOR_BGR2GRAY);
-
- std::vector prevCorners, currCorners;
- cv::goodFeaturesToTrack(prevGray, prevCorners, 100, 0.01, 10);
-
- if (prevCorners.empty()) continue;
-
- std::vector status;
- std::vector err;
- cv::calcOpticalFlowPyrLK(prevGray, currGray, prevCorners, currCorners, status, err);
-
- double avgMotion = 0.0;
- int validPoints = 0;
-
- for (size_t j = 0; j < prevCorners.size(); j++) {
- if (status[j]) {
- double dx = currCorners[j].x - prevCorners[j].x;
- double dy = currCorners[j].y - prevCorners[j].y;
- avgMotion += std::sqrt(dx * dx + dy * dy);
- validPoints++;
- }
- }
-
- if (validPoints > 0) avgMotion /= validPoints;
- totalFlow += avgMotion;
- count++;
- }
-
- double avgMotion = (count > 0) ? totalFlow / count : 0;
-
- if (avgMotion < 0.5) {
- return true;
- }
-
- return false;
-}
bool ANSCustomFS::detectScreenFlicker(const std::vector& frames) {
if (frames.size() < 10) return false;
@@ -918,16 +1012,15 @@ std::vector ANSCustomFS::divideImage(const cv::Mat& i
return cachedSections;
}
int ANSCustomFS::getHighestPriorityRegion() {
- if (!cachedSections.empty()) {
- return cachedSections.front().priority;
- }
- return 0;
+ // Priorities are always assigned as 1..N (N = number of sections), so the highest
+ // (most important) priority is 1 whenever we have any sections. Returning a literal
+ // keeps this correct when RankCachedSectionsByWarmColor rewrites priorities without
+ // reordering cachedSections.
+ return cachedSections.empty() ? 0 : 1;
}
int ANSCustomFS::getLowestPriorityRegion() {
- if (!cachedSections.empty()) {
- return cachedSections.back().priority;
- }
- return 0;
+ // Lowest priority (least important) is N, the total section count.
+ return static_cast(cachedSections.size());
}
cv::Rect ANSCustomFS::getRegionByPriority(int priority) {
for (const auto& section : cachedSections) {
@@ -993,7 +1086,14 @@ std::vector ANSCustomFS::RunInference(const cv::Mat& input, const
}
}
-// Stage A: Process existing detected area with tracker-based voting
+// Stage A: Process existing detected area with dual-path tracker-based voting.
+//
+// Dual-path design addresses the "small early fire" failure mode:
+// HIGH-conf fast path: conf >= 0.5, 8-of-15 frames, gated by (movement OR flicker).
+// LOW-conf slow path: conf >= 0.25, 6-of-15 frames, fire-class only,
+// gated by (flicker AND fire-color present).
+// The flicker gate uses temporal std-dev of HSV V inside the tracked bbox —
+// real fire oscillates, sunlight reflections do not.
std::vector ANSCustomFS::ProcessExistingDetectedArea(
const cv::Mat& frame,
const std::string& camera_id,
@@ -1014,6 +1114,14 @@ std::vector ANSCustomFS::ProcessExistingDetectedArea(
return output;
}
+ // Precompute full-frame HSV once so we can sample V per bbox cheaply.
+ cv::Mat frameHsv;
+ cv::cvtColor(frame, frameHsv, cv::COLOR_BGR2HSV);
+
+ // COCO filter is expensive — run it at most once per frame and reuse.
+ bool excludedComputed = false;
+ std::vector excludedObjects;
+
// Collect detected track IDs this frame for aging
std::unordered_set detectedTrackIds;
@@ -1028,18 +1136,32 @@ std::vector ANSCustomFS::ProcessExistingDetectedArea(
continue;
}
- // 2. Confidence check — fire >= threshold, smoke >= smoke threshold
- const bool isValidFire = (detectedObj.classId == 0) && (detectedObj.confidence >= _detectionScoreThreshold);
- const bool isValidSmoke = (detectedObj.classId == 2) && (detectedObj.confidence >= _smokeDetetectionThreshold);
+ // 2. Confidence gate — dual-path entry:
+ // - fire (class 0) at >= LOW_CONF_FIRE_THRESHOLD enters the low-conf path
+ // - fire at >= _detectionScoreThreshold also enters the high-conf path
+ // - smoke (class 2) still requires SmokeScore threshold (high-conf only)
+ const bool passesLowConfFire =
+ (detectedObj.classId == 0) && (detectedObj.confidence >= LOW_CONF_FIRE_THRESHOLD);
+ const bool isValidFire =
+ (detectedObj.classId == 0) && (detectedObj.confidence >= _detectionScoreThreshold);
+ const bool isValidSmoke =
+ (detectedObj.classId == 2) && (detectedObj.confidence >= _smokeDetetectionThreshold);
- if (!isValidFire && !isValidSmoke) {
+ if (!isValidFire && !isValidSmoke && !passesLowConfFire) {
continue;
}
- // 3. Update track history with this detection
- int trackId = detectedObj.trackId;
+ // 3. Sample mean HSV V inside the bbox for the flicker deque
+ cv::Rect safeBox = detectedObj.box & cv::Rect(0, 0, frame.cols, frame.rows);
+ double vMean = 0.0;
+ if (safeBox.area() > 0) {
+ vMean = cv::mean(frameHsv(safeBox))[2];
+ }
+
+ // 4. Update track history with this detection
+ const int trackId = detectedObj.trackId;
detectedTrackIds.insert(trackId);
- UpdateTrackHistory(trackId, detectedObj.classId, detectedObj.box);
+ UpdateTrackHistory(trackId, detectedObj.classId, detectedObj.box, detectedObj.confidence, vMean);
#ifdef FNS_DEBUG
// Draw detection with track info
@@ -1047,27 +1169,51 @@ std::vector ANSCustomFS::ProcessExistingDetectedArea(
cv::rectangle(draw, detectedObj.box, color, 2);
auto trackIt = _trackHistory.find(trackId);
if (trackIt != _trackHistory.end()) {
- std::string label = "T" + std::to_string(trackId) + " " +
+ std::string label = "T" + std::to_string(trackId) + " H" +
std::to_string(trackIt->second.detectedCount) + "/" +
- std::to_string(VOTE_THRESHOLD);
+ std::to_string(VOTE_THRESHOLD) + " L" +
+ std::to_string(trackIt->second.lowConfDetectedCount) + "/" +
+ std::to_string(LOW_VOTE_THRESHOLD);
cv::putText(draw, label,
cv::Point(detectedObj.box.x, detectedObj.box.y - 10),
cv::FONT_HERSHEY_SIMPLEX, 0.5, color, 2);
}
#endif
- // 4. Voting check — require consistent detection across frames
- if (!IsTrackConfirmed(trackId)) {
+ // 5. Voting — which path (if any) confirmed this track?
+ const bool highConfirmed = IsTrackConfirmed(trackId);
+ const bool lowConfirmed = IsTrackLowConfirmed(trackId);
+ if (!highConfirmed && !lowConfirmed) {
continue;
}
- // 5. Movement check — verify bounding box is changing (not static false positive)
- if (!HasBboxMovement(trackId)) {
- continue;
+ // 6. Gate: movement / flicker / color based on path
+ const bool hasFlicker = HasFireFlicker(trackId);
+ const bool hasMovement = HasBboxMovement(trackId);
+
+ if (highConfirmed) {
+ // High-conf path: sunlight FP kill requires evidence of temporal variation
+ // — either spatial (bbox drift) OR luma flicker is enough.
+ // This replaces the old movement-only gate which rejected small stationary fires.
+ if (!hasMovement && !hasFlicker) {
+ continue;
+ }
+ }
+ else {
+ // Low-conf-only path: stricter gate to suppress noise
+ // * fire class only (smoke is too easily confused at low conf)
+ // * require flicker (not just drift)
+ // * require fire-color HSV in the bbox
+ if (detectedObj.classId != 0) continue;
+ if (!hasFlicker) continue;
+ if (!IsFireDetected(frame, detectedObj.box)) continue;
}
- // 6. COCO filter — exclude detections that overlap with known non-fire objects
- std::vector excludedObjects = FindExcludedObjects(frame, camera_id, draw);
+ // 7. COCO filter — exclude detections that overlap with known non-fire objects
+ if (!excludedComputed) {
+ excludedObjects = FindExcludedObjects(frame, camera_id, draw);
+ excludedComputed = true;
+ }
if (!excludedObjects.empty() && IsOverlapping(detectedObj, excludedObjects, 0)) {
// Detection overlaps with a known object — not fire/smoke
_realFireCheck = std::max(0, _realFireCheck - 1);
@@ -1093,6 +1239,13 @@ std::vector ANSCustomFS::ProcessExistingDetectedArea(
}
void ANSCustomFS::AddConfirmedDetection(ANSCENTER::Object& detectedObj, std::vector& output) {
+ // A detection that passed the dual voting + flicker + color gates is a real positive.
+ // ConvertToCustomObjects() downstream filters by _detectionScoreThreshold, which would
+ // otherwise drop low-conf fires even after they've been confirmed. Bump the confidence
+ // to the threshold so the confirmed detection reaches the caller.
+ if (detectedObj.confidence < _detectionScoreThreshold) {
+ detectedObj.confidence = _detectionScoreThreshold;
+ }
output.push_back(std::move(detectedObj));
_isFireNSmokeDetected = true;
_retainDetectedArea = 0;
diff --git a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h
index d301168..d9e7a55 100644
--- a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h
+++ b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h
@@ -20,15 +20,24 @@ class CUSTOM_API ANSCustomFS : public IANSCustomClass
int trackId{ 0 };
int classId{ 0 }; // fire=0, smoke=2
std::deque bboxHistory; // bounding box history within window
- int detectedCount{ 0 }; // frames detected in sliding window
+ std::deque highConfHits; // parallel: 1 if >=HIGH_CONF_THRESHOLD, else 0
+ std::deque 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 voting threshold
+ bool confirmed{ false }; // passed high-conf voting threshold
};
- // Voting mechanism constants
- static constexpr int VOTE_WINDOW = 15;
- static constexpr int VOTE_THRESHOLD = 8;
+ // 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;
private:
using ANSLIBPtr = std::unique_ptr;
@@ -96,7 +105,6 @@ private:
cv::Rect GenerateMinimumSquareBoundingBox(const std::vector& detectedObjects, int minSize = 640);
void UpdateNoDetectionCondition();
- bool detectStaticFire(std::deque& frameQueue);
bool detectScreenFlicker(const std::vector& frames);
bool detectReflection(const cv::Mat& frame);
bool MajorityColourInFrame(cv::Mat frame, Range range, float area_threshold);
@@ -136,11 +144,16 @@ private:
int getLowestPriorityRegion();
cv::Rect getRegionByPriority(int priority);
- // Tracker-based voting methods
- void UpdateTrackHistory(int trackId, int classId, const cv::Rect& bbox);
- bool IsTrackConfirmed(int trackId) const;
+ // 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& detectedTrackIds);
+
+ // Warm-color priority scan: re-rank cached sections by HSV warm-pixel count
+ void RankCachedSectionsByWarmColor(const cv::Mat& frame);
std::vector FindNewDetectedArea(
const cv::Mat& frame,
const std::string& camera_id, cv::Mat& draw);
diff --git a/tests/FireNSmokeDetection/main.cpp b/tests/FireNSmokeDetection/main.cpp
index 6228488..ca8cbd0 100644
--- a/tests/FireNSmokeDetection/main.cpp
+++ b/tests/FireNSmokeDetection/main.cpp
@@ -32,6 +32,7 @@ int FireNSmokeDetection() {
//std::string videoFilePath = "C:\\Programs\\FireTest\\HFire2.mp4"; // failed as camera is moving
//std::string videoFilePath = "C:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\E112 v4-2.mp4"; // failed white smoke low resolution cam
+ std::cout << "\n*** Start ***" << std::endl;
std::vector classes;
ANSCustomFS detector;
std::string labelMap;
@@ -123,5 +124,7 @@ int FireNSmokeDetection() {
}
int main() {
- return FireNSmokeDetection();
+ std::cout << "\n*** FireNSmokeDetection ***" << std::endl;
+ FireNSmokeDetection();
+ return 0;
}