diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2f2b81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build-vs/ +cmake-build-*/ +build/ +.idea/ +*.user diff --git a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp index 55958aa..0426a4a 100644 --- a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp +++ b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.cpp @@ -1,1294 +1,586 @@ #include "ANSCustomFireNSmoke.h" -bool ANSCustomFS::IsDetectedAreaValid(const cv::Rect& area) const { - return area.width > MIN_ROI_SIZE && area.height > MIN_ROI_SIZE; -} -// Reject fire/smoke boxes that are too small relative to the frame. Defined as a -// fraction of frame area so the behavior is consistent across camera resolutions. -bool ANSCustomFS::IsObjectLargeEnough(const cv::Rect& box, const cv::Size& frameSize) const { - const double frameArea = static_cast(frameSize.width) * frameSize.height; - if (frameArea <= 0.0) return true; // can't judge — don't drop - const double boxArea = static_cast(std::max(0, box.width)) * std::max(0, box.height); - return (boxArea / frameArea) >= static_cast(_minObjectAreaRatio); -} -bool ANSCustomFS::IsFireOrSmoke(int classId, float confidence) const { - return (classId == 0) || // Fire - (classId == 2 && confidence >= SMOKE_CONFIDENCE_THRESHOLD); // Smoke -} -bool ANSCustomFS::ValidateMotionCorrelation(const std::vector& fireNSmokeRects) const { - return !fireNSmokeRects.empty() && fireNSmokeRects.size() < MAX_MOTION_TRACKING; -} -void ANSCustomFS::UpdatePositiveDetection() { - _isFireNSmokeDetected = true; - _retainDetectedArea = 0; - _isRealFireFrame = true; -} -void ANSCustomFS::ResetDetectedArea() { - _detectedArea = cv::Rect(0, 0, 0, 0); -} -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); - if (cachedSections.empty()) { - _currentPriority = 0; - return; - } - const int lowestPriority = getLowestPriorityRegion(); +#include +#include +#include +#include +#include - 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++; - } +using ansmind::RectF; +using namespace ansmind::firesmoke; + +namespace { + +std::string trim(std::string s) { + while (!s.empty() && (s.back() == '\r' || s.back() == '\n' || s.back() == ' ' || s.back() == '\t')) s.pop_back(); + size_t i = 0; + while (i < s.size() && (s[i] == ' ' || s[i] == '\t')) ++i; + return s.substr(i); } -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; - if (maxDim > 1280) return 480; - if (maxDim > 640) return 320; - return 224; -} -void ANSCustomFS::RefineDetectedArea(const cv::Mat& frame, const ANSCENTER::Object& detection) { - int cropSize = CalculateOptimalCropSize(frame); - - int xc = detection.box.x + detection.box.width / 2; - int yc = detection.box.y + detection.box.height / 2; - - int x1_new = std::clamp(xc - cropSize / 2, 0, frame.cols - cropSize); - int y1_new = std::clamp(yc - cropSize / 2, 0, frame.rows - cropSize); - - _detectedArea = cv::Rect( - x1_new, - y1_new, - std::min(cropSize, frame.cols - x1_new), - std::min(cropSize, frame.rows - y1_new) - ); -} -void ANSCustomFS::ResetDetectionState() { - ResetDetectedArea(); - _retainDetectedArea = 0; - _isFireNSmokeDetected = false; - _trackHistory.clear(); -} -// --- 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; - - auto it = _trackHistory.find(trackId); - if (it == _trackHistory.end()) { - TrackRecord record; - record.trackId = trackId; - record.classId = classId; - record.bboxHistory.push_back(bbox); - 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); - } - else { - auto& record = it->second; - record.bboxHistory.push_back(bbox); - 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 parallel deques - while (static_cast(record.bboxHistory.size()) > VOTE_WINDOW) { - record.bboxHistory.pop_front(); - 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 (high-conf path) - if (record.detectedCount >= VOTE_THRESHOLD) { - record.confirmed = true; - } - } +/// ANSLIB hands the class list back as one string; comma-separated in every +/// package seen so far, newline-separated in the .names file it came from. +std::vector splitLabels(const std::string& s) { + std::vector out; + const char sep = s.find(',') != std::string::npos ? ',' : '\n'; + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, sep)) { + item = trim(item); + if (!item.empty()) out.push_back(item); + } + return out; } -bool ANSCustomFS::IsTrackConfirmed(int trackId) const { - auto it = _trackHistory.find(trackId); - if (it == _trackHistory.end()) return false; - return it->second.detectedCount >= VOTE_THRESHOLD; +float toFloat(const std::string& v, float dflt) { + try { return v.empty() ? dflt : std::stof(v); } + catch (...) { return dflt; } } -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 toBool(const std::string& v, bool dflt) { + if (v.empty()) return dflt; + std::string s = v; + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return char(std::tolower(c)); }); + if (s == "true" || s == "1" || s == "yes" || s == "on") return true; + if (s == "false" || s == "0" || s == "no" || s == "off") return false; + return dflt; } -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; +/// The value in use: what the operator set, else the parameter's default. +std::string valueOf(const CustomParameter& p) { + return !p.Value.empty() ? p.Value : p.DefaultValue; } -bool ANSCustomFS::HasBboxMovement(int trackId) const { - auto it = _trackHistory.find(trackId); - if (it == _trackHistory.end()) return false; - - const auto& history = it->second.bboxHistory; - if (history.size() < 2) return false; - - const cv::Rect& earliest = history.front(); - const cv::Rect& latest = history.back(); - - // Calculate center positions - float cx1 = earliest.x + earliest.width / 2.0f; - float cy1 = earliest.y + earliest.height / 2.0f; - float cx2 = latest.x + latest.width / 2.0f; - float cy2 = latest.y + latest.height / 2.0f; - - // Average size for normalization - float avgWidth = (earliest.width + latest.width) / 2.0f; - float avgHeight = (earliest.height + latest.height) / 2.0f; - if (avgWidth < 1.0f || avgHeight < 1.0f) return false; - - // Position change relative to average size - float posChange = std::sqrt( - std::pow((cx2 - cx1) / avgWidth, 2) + - std::pow((cy2 - cy1) / avgHeight, 2) - ); - - // Size change relative to average area - float area1 = static_cast(earliest.area()); - float area2 = static_cast(latest.area()); - float avgArea = (area1 + area2) / 2.0f; - float sizeChange = (avgArea > 0) ? std::abs(area2 - area1) / avgArea : 0.0f; - - return (posChange > BBOX_CHANGE_THRESHOLD) || (sizeChange > BBOX_CHANGE_THRESHOLD); +void addParam(CustomParams& param, const char* name, const char* type, int decimals, + int minV, int maxV, const std::string& dflt) { + CustomParameter p; + p.Name = name; + p.DataType = type; + p.NoOfDecimals = decimals; + p.MaxValue = maxV; + p.MinValue = minV; + p.StartValue = dflt; + p.DefaultValue = dflt; + p.ListItems.clear(); + param.Parameters.push_back(p); } -void ANSCustomFS::AgeTracks(const std::unordered_set& detectedTrackIds) { - auto it = _trackHistory.begin(); - while (it != _trackHistory.end()) { - if (detectedTrackIds.find(it->first) == detectedTrackIds.end()) { - // Track was NOT detected this frame - it->second.totalFrames++; +} // namespace - // Remove stale tracks that haven't been seen recently (both paths empty) - if (it->second.totalFrames > VOTE_WINDOW && - it->second.detectedCount == 0 && - it->second.lowConfDetectedCount == 0) { - it = _trackHistory.erase(it); - continue; - } - - // 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(); - 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; - } +ANSCustomFS::ANSCustomFS() { + char* e = nullptr; + size_t n = 0; + if (_dupenv_s(&e, &n, "ANSCUSTOM_FS_LOG") == 0 && e) { + _fsLog = e[0] == '1'; + std::free(e); + } } -// --- End voting functions --- - -void ANSCustomFS::UpdateNoDetectionCondition() -{ - _isRealFireFrame = false; - _realFireCheck = 0; - - if (_isFireNSmokeDetected) { - _retainDetectedArea++; - if (_retainDetectedArea >= RETAINFRAMES) { - ResetDetectionState(); - } - } - else { - ResetDetectionState(); - } -} -std::vector ANSCustomFS::ConvertToCustomObjects(const std::vector& objects) { - std::vector results; - results.reserve(objects.size()); - - for (const auto& obj : objects) { - if (obj.confidence >= _detectionScoreThreshold) { - CustomObject customObj; - customObj.box = obj.box; - customObj.classId = obj.classId; - customObj.confidence = obj.confidence; - customObj.cameraId = obj.cameraId; - customObj.className = obj.className; - customObj.extraInfo = "Detection Score Threshold:" + - std::to_string(_detectionScoreThreshold); - results.push_back(customObj); - } - } - return results; +ANSCustomFS::~ANSCustomFS() { + Destroy(); } -void ANSCustomFS::GetModelParameters() { - if (!_readROIs) { - cv::Rect exRoi; - for (auto& roi : _params.ROI_Values) { - if (roi.Name.find("ExclusiveROIs") != std::string::npos) { - exRoi.x = roi.ROIPoints[0].x; - exRoi.y = roi.ROIPoints[0].y; - exRoi.width = roi.ROIPoints[1].x - roi.ROIPoints[0].x; - exRoi.height = roi.ROIPoints[2].y - roi.ROIPoints[0].y; - _exclusiveROIs.push_back(exRoi); - } - } - - // Get parameters - for (auto& param : _params.Parameters) { - if (param.Name.find("SmokeScore") != std::string::npos) { - _smokeDetetectionThreshold = std::stof(param.Value); - } - else if (param.Name.find("Sensitivity") != std::string::npos) { - _motionSpecificity = 1.0f - std::stof(param.Value); - _motionSpecificity = std::clamp(_motionSpecificity, 0.0f, 1.0f); - // Note: Motion detector threshold is set during LoadModelFromFolder. - // Runtime threshold update is not supported through ANSLIB API. - } - else if (param.Name.find("MinObjectSize") != std::string::npos) { - // UI value is a percentage of the frame area (e.g. 0.03 = 0.03%). - try { - const float pct = std::stof(param.Value); - _minObjectAreaRatio = std::clamp(pct / 100.0f, 0.0f, 1.0f); - } - catch (const std::exception&) { - _minObjectAreaRatio = DEFAULT_MIN_OBJECT_RATIO; - } - } - } - _readROIs = true; - } +// --------------------------------------------------------------------------- +// geometry +// --------------------------------------------------------------------------- +cv::Rect ANSCustomFS::pixelRect(const RectF& r, int W, int H) { + const int x0 = std::clamp(int(std::lround(r.x * W)), 0, W); + const int y0 = std::clamp(int(std::lround(r.y * H)), 0, H); + const int x1 = std::clamp(int(std::lround((r.x + r.w) * W)), 0, W); + const int y1 = std::clamp(int(std::lround((r.y + r.h) * H)), 0, H); + return cv::Rect(x0, y0, std::max(0, x1 - x0), std::max(0, y1 - y0)); } -std::vector ANSCustomFS::FindNewDetectedArea( - const cv::Mat& frame, - const std::string& camera_id, cv::Mat& draw) -{ - std::vector output; - - // Reset detection state - _isRealFireFrame = false; - _realFireCheck = 0; - - // Update priority region - UpdatePriorityRegion(frame); - _detectedArea = getRegionByPriority(_currentPriority); - -#ifdef FNS_DEBUG - cv::rectangle(draw, _detectedArea, cv::Scalar(0, 0, 255), 4); // Red -#endif - - if (!IsDetectedAreaValid(_detectedArea)) { - ResetDetectedArea(); - return output; - } - - // Run detection on new area - cv::Mat detectedROI = frame(_detectedArea); - std::vector detectedObjects; - _detector->RunInference(detectedROI, camera_id.c_str(), detectedObjects); - - if (detectedObjects.empty()) { - ResetDetectedArea(); - return output; - } - - // 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 (!IsFireOrSmoke(detectedObj.classId, detectedObj.confidence)) continue; - - // Size gate — ignore boxes too small relative to the frame. - if (!IsObjectLargeEnough(detectedObj.box, frame.size())) continue; - - 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); - 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(); - } - - return output; +RectF ANSCustomFS::normRect(const cv::Rect& r, int W, int H) { + RectF f; + f.x = float(r.x) / float(W); + f.y = float(r.y) / float(H); + f.w = float(r.width) / float(W); + f.h = float(r.height) / float(H); + return f.clamped(); } - -std::vector ANSCustomFS::FindMovementObjects(const cv::Mat& frame, const std::string& camera_id, cv::Mat& draw) -{ - if (_motionSpecificity < 1) { - // 1. Extract the detected area - cv::Mat activeROI = frame(_detectedArea); - - // 2. Detect movement in the detected area - std::vector movementObjects; - _motiondetector->RunInference(activeROI, camera_id.c_str(), movementObjects); - std::vector fireNSmokeRects; - float movementarea_threshold = 0; - int totalMovementObjects = static_cast(movementObjects.size()); - - if ((totalMovementObjects > 0) && (totalMovementObjects <= 10)) { - for (const auto& rect : movementObjects) { - ANSCENTER::Object mBbj; - mBbj.box = rect.box; - mBbj.box.x += _detectedArea.x; - mBbj.box.y += _detectedArea.y; - movementarea_threshold = calculateIoU(_detectedArea, mBbj.box); - if (movementarea_threshold < 0.5) { - fireNSmokeRects.push_back(mBbj); -#ifdef FNS_DEBUG - cv::rectangle(draw, mBbj.box, cv::Scalar(123, 122, 34), 2); -#endif - } - } - } - activeROI.release(); - return fireNSmokeRects; - } - else return std::vector{}; +float ANSCustomFS::cover(const RectF& a, const RectF& b) { + const float ix = std::max(0.f, std::min(a.x + a.w, b.x + b.w) - std::max(a.x, b.x)); + const float iy = std::max(0.f, std::min(a.y + a.h, b.y + b.h) - std::max(a.y, b.y)); + return a.area() > 0 ? ix * iy / a.area() : 0.f; } - -std::vector ANSCustomFS::FindExcludedObjects( - const cv::Mat& frame, - const std::string& camera_id, cv::Mat& draw) -{ - // Final check for filters - // Get activeROI with padding to include surrounding area - cv::Rect paddedROI = _detectedArea; - paddedROI.x -= 100; - paddedROI.y -= 100; - paddedROI.width += 200; - paddedROI.height += 200; - // Ensure paddedROI is within frame bounds - paddedROI.x = std::max(0, paddedROI.x); - paddedROI.y = std::max(0, paddedROI.y); - paddedROI.width = std::min(frame.cols - paddedROI.x, paddedROI.width); - paddedROI.height = std::min(frame.rows - paddedROI.y, paddedROI.height); - // Run filter inference on padded ROI - cv::Mat paddedFrame = frame(paddedROI); - std::vector filteredObjects; - _filter->RunInference(paddedFrame, camera_id.c_str(), filteredObjects); - - if (!filteredObjects.empty()) { - std::vector excludedObjects; - for (const auto& filteredObj : filteredObjects) { - if (EXCLUDED_FILTER_CLASSES.find(filteredObj.classId) == EXCLUDED_FILTER_CLASSES.end()) - { - ANSCENTER::Object exOBbj; - exOBbj.box.x = paddedROI.x + filteredObj.box.x; - exOBbj.box.y = paddedROI.y + filteredObj.box.y; - exOBbj.box.width = filteredObj.box.width; - exOBbj.box.height = filteredObj.box.height; - excludedObjects.push_back(exOBbj); -#ifdef FNS_DEBUG - cv::rectangle(draw, exOBbj.box, cv::Scalar(23, 25, 0), 2); - cv::putText(draw, filteredObj.className, cv::Point(exOBbj.box.x, exOBbj.box.y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 2); -#endif - } - } - } - paddedFrame.release(); - return filteredObjects; +void ANSCustomFS::nmsClassAgnostic(std::vector& dets, float iouThr) { + std::sort(dets.begin(), dets.end(), [](const Det& a, const Det& b) { return a.conf > b.conf; }); + std::vector kept; + for (const auto& d : dets) { + bool dup = false; + for (const auto& k : kept) + if (k.box.iou(d.box) > iouThr) { dup = true; break; } + if (!dup) kept.push_back(d); + } + dets.swap(kept); } -ANSCustomFS::ANSCustomFS() - : EXCLUDED_FILTER_CLASSES({ 13, 59, 60, 68, 69, 70 }) -{ - _smokeDetetectionThreshold = 0; -} -bool ANSCustomFS::OptimizeModel(bool fp16) -{ - if (!_detector || !_filter) return false; - int detectorResult = _detector->Optimize(fp16); - int filterResult = _filter->Optimize(fp16); - if ((detectorResult != 1) || (filterResult != 1)) return false; - return true; -} -bool ANSCustomFS::ConfigureParameters(CustomParams& param) { - param.ROI_Config.clear(); - // We do need to have exclusive ROIs for the model - CustomROIConfig exclusiveROI; - if (this->_params.ROI_Config.empty()) { - exclusiveROI.Name = "ExclusiveROIs"; - exclusiveROI.Polygon = false; - exclusiveROI.Rectangle = true; - exclusiveROI.Line = false; - exclusiveROI.MinItems = 0; - exclusiveROI.MaxItems = 20; - exclusiveROI.ROIMatch = "All Corners"; - param.ROI_Config.push_back(exclusiveROI); - } - else { - for (auto& roi : this->_params.ROI_Config) { - param.ROI_Config.push_back(roi); - } - } - - param.ROI_Options.clear(); - param.Parameters.clear(); - CustomParameter param1; - param1.Name = "SmokeScore"; - param1.DataType = "float"; - param1.NoOfDecimals = 2; - param1.MaxValue = 1; - param1.MinValue = 0; - param1.StartValue = std::to_string(SMOKE_CONFIDENCE_THRESHOLD); - param1.ListItems.clear(); - param1.DefaultValue = std::to_string(SMOKE_CONFIDENCE_THRESHOLD); - param.Parameters.push_back(param1); - - CustomParameter param2; - param2.Name = "Sensitivity"; - param2.DataType = "float"; - param2.NoOfDecimals = 2; - param2.MaxValue = 1; - param2.MinValue = 0; - param2.StartValue = std::to_string(MOTION_SENSITIVITY); - param2.ListItems.clear(); - param2.DefaultValue = std::to_string(MOTION_SENSITIVITY); - param.Parameters.push_back(param2); - - // Minimum detected fire/smoke size, as a percentage of the full frame area. - // Boxes below this are rejected — suppresses tiny false positives. - CustomParameter param3; - param3.Name = "MinObjectSize"; - param3.DataType = "float"; - param3.NoOfDecimals = 2; - param3.MaxValue = 10; // up to 10% of the frame - param3.MinValue = 0; - param3.StartValue = std::to_string(DEFAULT_MIN_OBJECT_RATIO * 100.0f); - param3.ListItems.clear(); - param3.DefaultValue = std::to_string(DEFAULT_MIN_OBJECT_RATIO * 100.0f); - param.Parameters.push_back(param3); - - param.ROI_Values.clear(); - for (auto& roi : this->_params.ROI_Values) { - CustomROIValue roiValue; - roiValue.Name = roi.Name; - roiValue.ROIPoints = roi.ROIPoints; - roiValue.ROIMatch = roi.ROIMatch; - roiValue.Option = roi.Option; - param.ROI_Values.push_back(roiValue); - } - return true; +// --------------------------------------------------------------------------- +// the models +// --------------------------------------------------------------------------- +bool ANSCustomFS::runDetector(const cv::Mat& bgr, const cv::Rect& roi, std::vector& out, + const std::string& camera) { + if (!_detector || roi.width < 2 || roi.height < 2) return false; + const bool whole = roi.x == 0 && roi.y == 0 && roi.width == bgr.cols && roi.height == bgr.rows; + // A ROI view is not continuous; ANSLIB wants a plain image. + cv::Mat sub = whole ? bgr : bgr(roi).clone(); + std::vector objs; + const int rc = _detector->RunInference(sub, camera.c_str(), objs); + (void)rc; // ANSLIB reports through its own log; an empty list is the answer either way + for (const auto& o : objs) { + Det d; + d.box = normRect(cv::Rect(o.box.x + roi.x, o.box.y + roi.y, o.box.width, o.box.height), bgr.cols, bgr.rows); + d.classId = o.classId; + d.conf = o.confidence; + if (d.box.w > 0 && d.box.h > 0) out.push_back(d); + } + return true; } -std::vector ANSCustomFS::RunInference(const cv::Mat& input) -{ - return RunInference(input, "CustomCam"); +bool ANSCustomFS::runFilter(const cv::Mat& bgr, const cv::Rect& roi, std::vector& out, + const std::string& camera) { + if (!_filter || roi.width < 2 || roi.height < 2) return false; + const bool whole = roi.x == 0 && roi.y == 0 && roi.width == bgr.cols && roi.height == bgr.rows; + cv::Mat sub = whole ? bgr : bgr(roi).clone(); + std::vector objs; + const int rc = _filter->RunInference(sub, camera.c_str(), objs); + (void)rc; + for (const auto& o : objs) { + Det d; + d.box = normRect(cv::Rect(o.box.x + roi.x, o.box.y + roi.y, o.box.width, o.box.height), bgr.cols, bgr.rows); + d.classId = o.classId; + d.conf = o.confidence; + if (d.box.w > 0 && d.box.h > 0) out.push_back(d); + } + return true; } -bool ANSCustomFS::Destroy() -{ - try { - _detector.reset(); - _filter.reset(); - _motiondetector.reset(); - return true; - } - catch (...) { - return false; - } + +Cues ANSCustomFS::cuesFor(const cv::Mat& bgr, const RectF& box) const { + const cv::Rect r = pixelRect(box, bgr.cols, bgr.rows); + if (r.width < 2 || r.height < 2) return Cues{}; + const uint8_t* p = bgr.ptr(r.y) + size_t(r.x) * 3; + return patchCues(p, r.width, r.height, int(bgr.step)); } + +// --------------------------------------------------------------------------- +// lifecycle +// --------------------------------------------------------------------------- bool ANSCustomFS::Initialize(const std::string& modelDirectory, float detectionScoreThreshold, std::string& labelMap) { - try { - _modelDirectory = modelDirectory; - _detectionScoreThreshold = detectionScoreThreshold; + std::lock_guard lock(_mutex); + try { + _modelDirectory = modelDirectory; + _detectionScoreThreshold = detectionScoreThreshold; - // Create model instances using factory pattern (ABI-safe) - _detector = ANSLIBPtr(ANSCENTER::ANSLIB::Create(), &ANSCENTER::ANSLIB::Destroy); - _filter = ANSLIBPtr(ANSCENTER::ANSLIB::Create(), &ANSCENTER::ANSLIB::Destroy); - _motiondetector = ANSLIBPtr(ANSCENTER::ANSLIB::Create(), &ANSCENTER::ANSLIB::Destroy); + std::cerr << "[ANSCustomFS] Initialize: model dir " << modelDirectory << std::endl; + _detector = ANSLIBPtr(ANSCENTER::ANSLIB::Create(), &ANSCENTER::ANSLIB::Destroy); + _filter = ANSLIBPtr(ANSCENTER::ANSLIB::Create(), &ANSCENTER::ANSLIB::Destroy); + if (!_detector || !_filter) { + std::cerr << "ANSCustomFS::Initialize: ANSLIB::Create failed." << std::endl; + return false; + } + // NVIDIA -> TensorRT, anything else -> OpenVINO; both plain detectors. + const int engineType = _detector->GetEngineType(); + std::cerr << "[ANSCustomFS] engine type " << engineType << " -> " << (engineType == 1 ? "TensorRT" : "OpenVINO") << std::endl; + const int modelType = engineType == 1 ? 31 : 30; + const int detectionType = 1; + // The engines are built or loaded HERE, whatever the caller set with + // SetLoadEngineOnCreate: a lazily loaded engine faulted inside + // ANSLIB's inference entry on the first frame (ANSLIB.cpp:160), and + // the ans-custom-host asks for load-on-create anyway. The first run + // on a GPU builds TensorRT engines for minutes; they cache beside the + // models. + const int loadEngineOnCreation = 1; + (void)_loadEngineOnCreate; + const int autoEngineDetection = 1; + const std::string licenseKey; - _detectorModelType = 31; // TENSORRT - _detectorDetectionType = 1; // DETECTION - _filterModelType = 31; // TENSORRT - _filterDetectionType = 1; // DETECTION - _motionModelType = 19; // Motion detection (generic CPU) - _motionDetectionType = 1; // DETECTION + // The raw pass must hear the detector's QUIET verdicts too: a small + // fire is 0.2 on the whole frame long before it is 0.5, and a fake_* + // at 0.2 is still a fake. The alarm threshold is the verifier's + // evidence, never this floor. + const float floor = _p.minConf; + const int detResult = _detector->LoadModelFromFolder( + licenseKey.c_str(), "detector", "detector.names", + floor, floor, 0.5f, + autoEngineDetection, modelType, detectionType, loadEngineOnCreation, + modelDirectory.c_str(), labelMap); + if (detResult != 1) { + std::cerr << "ANSCustomFS::Initialize: failed to load the detector from " << modelDirectory << std::endl; + return false; + } + std::cerr << "[ANSCustomFS] detector loaded: " << labelMap << std::endl; + _labels = splitLabels(labelMap); + _roles.clear(); + _fireClass = _smokeClass = -1; + _fireName.clear(); + _smokeName.clear(); + for (size_t i = 0; i < _labels.size(); ++i) { + const Role r = roleOf(_labels[i]); + _roles.push_back(r); + if (r == Role::Fire && _fireClass < 0) { _fireClass = int(i); _fireName = _labels[i]; } + if (r == Role::Smoke && _smokeClass < 0) { _smokeClass = int(i); _smokeName = _labels[i]; } + } + if (_fireClass < 0 && _smokeClass < 0) { + std::cerr << "ANSCustomFS::Initialize: the detector's classes name neither fire nor smoke: " + << labelMap << std::endl; + return false; + } - // Check the hardware type - engineType = _detector->GetEngineType(); - if (engineType == 1) { - // NVIDIA GPU: Use TensorRT - _detectorModelType = 31; // TENSORRT - _detectorDetectionType = 1; // DETECTION - _filterModelType = 31; // TENSORRT - _filterDetectionType = 1; // DETECTION - std::cout << "NVIDIA GPU detected. Using TensorRT" << std::endl; - } - else { - // CPU/Other: Use OpenVINO - _detectorModelType = 30; // OPENVINO - _detectorDetectionType = 1; // DETECTION - _filterModelType = 30; //Yolo v12 - _filterDetectionType = 1; // DETECTION - std::cout << "CPU detected. Using OpenVINO and ONNX" << std::endl; + // The look-alike filter (COCO) is optional: without it vehicles and + // screens are simply not explained away. + std::string filterLabels; + const int filterResult = _filter->LoadModelFromFolder( + licenseKey.c_str(), "filter", "filter.names", + 0.25f, 0.25f, 0.5f, + autoEngineDetection, modelType, detectionType, loadEngineOnCreation, + modelDirectory.c_str(), filterLabels); + if (filterResult != 1) { + std::cerr << "ANSCustomFS::Initialize: no look-alike filter model (filter.onnx) - continuing without it." << std::endl; + _filter.reset(); + filterLabels.clear(); + } + std::cerr << "[ANSCustomFS] filter " << (_filter ? "loaded: " + filterLabels.substr(0, 60) : "absent") << std::endl; + _filterLabels = splitLabels(filterLabels); + // Which filter classes explain a look-alike away: by NAME when the + // package ships filter.names, by COCO index when it does not. + const size_t nc = _filterLabels.empty() ? 80 : _filterLabels.size(); + _vehicle.assign(nc, 0); + _screen.assign(nc, 0); + if (_filterLabels.empty()) { + for (int i : {1, 2, 3, 5, 7}) _vehicle[size_t(i)] = 1; + for (int i : {9, 62, 63, 67}) _screen[size_t(i)] = 1; + } else { + for (size_t i = 0; i < _filterLabels.size(); ++i) { + const std::string& s = _filterLabels[i]; + if (s == "bicycle" || s == "car" || s == "motorcycle" || s == "bus" || s == "truck") + _vehicle[i] = 1; + if (s == "tv" || s == "laptop" || s == "cell phone" || s == "traffic light") + _screen[i] = 1; + } + } - } - - this->_hsvThreshold = 0.25; - this->_fire_colour.push_back(std::pair( - cv::Scalar(0, 0, 179), cv::Scalar(255, 255, 255)) - ); - this->_smoke_colour.push_back(std::pair( - cv::Scalar(0, 0, 51), cv::Scalar(180, 128, 204)) - ); - - if (this->_detectionScoreThreshold < 0.25f) this->_detectionScoreThreshold = 0.25f; - - // Initialize parameters - _params.ROI_Config.clear(); - _params.ROI_Options.clear(); - _params.Parameters.clear(); - _params.ROI_Values.clear(); - - _smokeDetetectionThreshold = SMOKE_CONFIDENCE_THRESHOLD; - -#ifdef FNS_DEBUG - this->_loadEngineOnCreate = true; -#endif - int loadEngineOnCreation = _loadEngineOnCreate ? 1 : 0; - int autoEngineDetection = 1; - std::string licenseKey = ""; - std::string motionDetectorLabels; - - // 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(), - "detector", "detector.names", - detScoreThreshold, detConfThreshold, detNMSThreshold, - autoEngineDetection, - _detectorModelType, _detectorDetectionType, - loadEngineOnCreation, - modelDirectory.c_str(), - labelMap); - if (detResult != 1) { - std::cerr << "ANSCustomFS::Initialize: Failed to load detector model." << std::endl; - return false; - } - - // Enable ByteTrack tracker on the detector for persistent track IDs - int trackerResult = _detector->SetTracker(0 /*BYTETRACK*/, 1 /*enable*/); - if (trackerResult != 1) { - std::cerr << "ANSCustomFS::Initialize: Warning - Failed to enable ByteTrack tracker." << std::endl; - } - - // Load filter model (COCO general object detector for false positive filtering) - float filterScoreThreshold = 0.25f; - float filterConfThreshold = 0.5f; - float filterNMSThreshold = 0.5f; - int filterResult = _filter->LoadModelFromFolder( - licenseKey.c_str(), - "filter", "filter.names", - filterScoreThreshold, filterConfThreshold, filterNMSThreshold, - autoEngineDetection, - _filterModelType, _filterDetectionType, - loadEngineOnCreation, - modelDirectory.c_str(), - _filterLabels); - if (filterResult != 1) { - std::cerr << "ANSCustomFS::Initialize: Failed to load filter model." << std::endl; - return false; - } - - // Load motion detector model (optional — not used in Stage A tracker-based pipeline) - float motionScoreThreshold = MOTION_SENSITIVITY; - float motionConfThreshold = 0.5f; - float motionNMSThreshold = 0.5f; - int motionResult = _motiondetector->LoadModelFromFolder( - licenseKey.c_str(), - "motion", "motion.names", - motionScoreThreshold, motionConfThreshold, motionNMSThreshold, - autoEngineDetection, - _motionModelType, _motionDetectionType, - loadEngineOnCreation, - "", // Empty model directory - motion detector is algorithmic - motionDetectorLabels); - if (motionResult != 1) { - std::cerr << "ANSCustomFS::Initialize: Warning - Motion detector not loaded (not required for Stage A)." << std::endl; - // Non-fatal: motion detector is not used in the tracker-based Stage A pipeline - } - - return true; - } - catch (const std::exception& e) { - std::cerr << "ANSCustomFS::Initialize: Exception: " << e.what() << std::endl; - return false; - } - catch (...) { - std::cerr << "ANSCustomFS::Initialize: Unknown exception." << std::endl; - return false; - } + _verifier = Verifier(_p); + _paramSignature.clear(); + _lastResults.clear(); + _lastRunT = -1e9; + _clockStarted = false; + _frames = 0; + return true; + } + catch (const std::exception& e) { + std::cerr << "ANSCustomFS::Initialize: exception: " << e.what() << std::endl; + return false; + } + catch (...) { + std::cerr << "ANSCustomFS::Initialize: unknown exception." << std::endl; + return false; + } } -ANSCustomFS::~ANSCustomFS() + +bool ANSCustomFS::OptimizeModel(bool fp16) { - Destroy(); + std::lock_guard lock(_mutex); + if (!_detector) return false; + const int d = _detector->Optimize(fp16); + const int f = _filter ? _filter->Optimize(fp16) : 1; + return d == 1 && f == 1; } -cv::Rect ANSCustomFS::GenerateMinimumSquareBoundingBox(const std::vector& detectedObjects, int minSize) { - try { - int adMinSize = minSize - 20; - if (adMinSize < 0) adMinSize = 0; - if (detectedObjects.empty()) return cv::Rect(0, 0, minSize, minSize); - int minX = detectedObjects[0].box.x; - int minY = detectedObjects[0].box.y; - int maxX = detectedObjects[0].box.x + detectedObjects[0].box.width; - int maxY = detectedObjects[0].box.y + detectedObjects[0].box.height; - - for (const auto& rect : detectedObjects) { - minX = std::min(minX, rect.box.x); - minY = std::min(minY, rect.box.y); - maxX = std::max(maxX, rect.box.x + rect.box.width); - maxY = std::max(maxY, rect.box.y + rect.box.height); - } - - int width = maxX - minX; - int height = maxY - minY; - int squareSize = std::max({ width, height, adMinSize }); - int centerX = minX + width / 2; - int centerY = minY + height / 2; - int squareX = centerX - squareSize / 2; - int squareY = centerY - squareSize / 2; - - return cv::Rect(squareX - 10, squareY - 10, squareSize + 20, squareSize + 20); - } - catch (std::exception& e) { - return cv::Rect(0, 0, minSize, minSize); - } -} -bool ANSCustomFS::detectScreenFlicker(const std::vector& frames) { - if (frames.size() < 10) return false; - - std::vector intensities; - for (const auto& frame : frames) { - cv::Mat gray; - cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY); - intensities.push_back(cv::mean(gray)[0]); - } - - cv::Mat fftInput(static_cast(intensities.size()), 1, CV_64F, intensities.data()); - cv::Mat fftOutput; - cv::dft(fftInput, fftOutput, cv::DFT_REAL_OUTPUT); - - for (int i = 1; i < fftOutput.rows / 2; i++) { - double freq = std::abs(fftOutput.at(i, 0)); - if (freq > 50 && freq < 70) { - return true; - } - } - return false; -} -bool ANSCustomFS::detectReflection(const cv::Mat& frame) { - cv::Mat gray, edges; - cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY); - cv::Canny(gray, edges, 50, 150); - - int edgeCount = cv::countNonZero(edges); - return edgeCount > 5000; -} -bool ANSCustomFS::MajorityColourInFrame(cv::Mat frame, Range range, float area_threshold) +bool ANSCustomFS::Destroy() { - cv::Mat img_hsv; - try { - cv::Mat blur, fireMask; - cv::GaussianBlur(frame, blur, cv::Size(21, 21), 0); - cv::cvtColor(blur, img_hsv, cv::COLOR_BGR2HSV); - cv::inRange(img_hsv, range.first, range.second, fireMask); - - cv::erode(fireMask, fireMask, cv::Mat(), cv::Point(-1, -1), 2); - cv::dilate(fireMask, fireMask, cv::Mat(), cv::Point(-1, -1), 2); - return (cv::countNonZero(fireMask) / (frame.rows * frame.cols)) > area_threshold; - - } - catch (cv::Exception& e) { - return false; - } + std::lock_guard lock(_mutex); + try { + _detector.reset(); + _filter.reset(); + _lastResults.clear(); + return true; + } + catch (...) { + return false; + } } -bool ANSCustomFS::MajorityColourInFrame(cv::Mat frame, std::vector ranges, float area_threshold) + +// --------------------------------------------------------------------------- +// parameters +// --------------------------------------------------------------------------- +bool ANSCustomFS::ConfigureParameters(CustomParams& param) { - unsigned int current_area = 0; - cv::Mat img_hsv; - try { - cv::cvtColor(frame, img_hsv, cv::COLOR_BGR2HSV); - for (const Range& range : ranges) { - cv::inRange(img_hsv, range.first, range.second, frame); - current_area += cv::countNonZero(frame); - } - } - catch (cv::Exception& e) { - return false; - } - return (float)current_area / ((float)frame.rows * frame.cols) > area_threshold; + std::lock_guard lock(_mutex); + param.ROI_Config.clear(); + if (this->_params.ROI_Config.empty()) { + CustomROIConfig exclusiveROI; + exclusiveROI.Name = "ExclusiveROIs"; + exclusiveROI.Polygon = false; + exclusiveROI.Rectangle = true; + exclusiveROI.Line = false; + exclusiveROI.MinItems = 0; + exclusiveROI.MaxItems = 20; + exclusiveROI.ROIMatch = "All Corners"; + param.ROI_Config.push_back(exclusiveROI); + } else { + for (auto& roi : this->_params.ROI_Config) param.ROI_Config.push_back(roi); + } + param.ROI_Options.clear(); + param.Parameters.clear(); + + // One operator-facing knob for how much evidence an alarm needs (0 = the + // fewest false alarms, 1 = the earliest alarm; the physical gates never + // move), the wildfire place memory, which families to watch, and the + // cost levers. + addParam(param, "Sensitivity", "float", 2, 0, 1, "0.5"); + addParam(param, "SmokeSeconds", "float", 0, 0, 600, "0"); + addParam(param, "DetectFire", "Boolean", 0, 0, 1, "true"); + addParam(param, "DetectSmoke", "Boolean", 0, 0, 1, "true"); + addParam(param, "ZoomChecks", "float", 0, 0, 8, "2"); + addParam(param, "MaxFps", "float", 0, 1, 30, "6"); + addParam(param, "CandidateScore", "float", 2, 0, 1, "0.15"); + // Minimum candidate size as a PERCENTAGE of the frame area (0.03 = 0.03 %): + // resolution-independent, rejects the tiny boxes that were the dominant + // small false-positive source on low-resolution cameras. + addParam(param, "MinObjectSize", "float", 2, 0, 10, "0.03"); + + param.ROI_Values.clear(); + for (auto& roi : this->_params.ROI_Values) { + CustomROIValue roiValue; + roiValue.Name = roi.Name; + roiValue.ROIPoints = roi.ROIPoints; + roiValue.ROIMatch = roi.ROIMatch; + roiValue.Option = roi.Option; + roiValue.OriginalImageSize = roi.OriginalImageSize; + param.ROI_Values.push_back(roiValue); + } + return true; } -bool ANSCustomFS::DetectFireNSmokeColourInFrame(const cv::Mat& frame, const cv::Rect& bBox, - const std::vector& ranges, float area_threshold) + +void ANSCustomFS::applyParams() { - if (frame.empty()) { - return false; - } + // Cheap to recompute, so it runs every frame and only acts on a change: + // ANSVIS and ans-custom-host both push values through SetParameters + // without telling the plugin. + std::string sig; + for (const auto& p : _params.Parameters) sig += p.Name + "=" + valueOf(p) + ";"; + sig += "rois=" + std::to_string(_params.ROI_Values.size()); + for (const auto& r : _params.ROI_Values) + for (const auto& pt : r.ROIPoints) sig += "," + std::to_string(pt.x) + ":" + std::to_string(pt.y); + if (sig == _paramSignature) return; + _paramSignature = sig; - if (ranges.empty()) { - return false; - } + Params p; // the node's defaults + float sensitivity = 0.5f; + for (const auto& prm : _params.Parameters) { + const std::string v = valueOf(prm); + if (prm.Name == "Sensitivity") sensitivity = std::clamp(toFloat(v, 0.5f), 0.f, 1.f); + else if (prm.Name == "SmokeSeconds") p.smokePersistSeconds = std::clamp(toFloat(v, 0.f), 0.f, 600.f); + else if (prm.Name == "DetectFire") p.fire = toBool(v, true); + else if (prm.Name == "DetectSmoke") p.smoke = toBool(v, true); + else if (prm.Name == "ZoomChecks") _zoomBudget = std::clamp(int(std::lround(toFloat(v, 2.f))), 0, 8); + else if (prm.Name == "MaxFps") _maxFps = std::clamp(double(toFloat(v, 6.f)), 1.0, 30.0); + else if (prm.Name == "CandidateScore") p.minConf = std::clamp(toFloat(v, 0.15f), 0.05f, 0.9f); + else if (prm.Name == "MinObjectSize") _minObjectAreaRatio = std::clamp(toFloat(v, 0.03f) / 100.f, 0.f, 1.f); + } + // The same three settings the ANSMIND node offers, on a slider. + if (sensitivity > 0.66f) { // high + p.confirm = 1.4f; + p.minSeconds = 0.5f; + p.smokeMinSeconds = 2.0f; + } else if (sensitivity < 0.34f) { // low + p.confirm = 3.2f; + p.minSeconds = 2.0f; + p.smokeMinSeconds = 5.0f; + } // else balanced: the defaults + if (!p.fire && !p.smoke) p.fire = p.smoke = true; // both off is nobody's intent + _p = p; + _verifier.configure(_p); - if (area_threshold < 0.0f || area_threshold > 1.0f) { - return false; - } - - try { - constexpr int padding = 20; - const int x1 = std::max(bBox.x - padding, 0); - const int y1 = std::max(bBox.y - padding, 0); - const int x2 = std::min(bBox.x + bBox.width + padding, frame.cols); - const int y2 = std::min(bBox.y + bBox.height + padding, frame.rows); - - const int roiWidth = x2 - x1; - const int roiHeight = y2 - y1; - - if (roiWidth <= 0 || roiHeight <= 0) { - return false; - } - - cv::Mat roiFrame = frame(cv::Rect(x1, y1, roiWidth, roiHeight)); - - cv::Mat blur; - cv::GaussianBlur(roiFrame, blur, cv::Size(21, 21), 0); - - cv::Mat img_hsv; - cv::cvtColor(blur, img_hsv, cv::COLOR_BGR2HSV); - - cv::Mat combined_mask = cv::Mat::zeros(img_hsv.size(), CV_8U); - cv::Mat temp_mask; - - for (const Range& range : ranges) { - cv::inRange(img_hsv, range.first, range.second, temp_mask); - combined_mask |= temp_mask; - } - - static const cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5)); - cv::morphologyEx(combined_mask, combined_mask, cv::MORPH_CLOSE, kernel); - cv::morphologyEx(combined_mask, combined_mask, cv::MORPH_OPEN, kernel); - - const int matched_area = cv::countNonZero(combined_mask); - const int total_area = roiWidth * roiHeight; - const float matched_ratio = static_cast(matched_area) / static_cast(total_area); - - return matched_ratio > area_threshold; - - } - catch (const cv::Exception& e) { - return false; - } + _exclusiveROIs.clear(); + for (const auto& roi : _params.ROI_Values) { + if (roi.Name.find("ExclusiveROIs") == std::string::npos || roi.ROIPoints.empty()) continue; + int x0 = roi.ROIPoints[0].x, y0 = roi.ROIPoints[0].y, x1 = x0, y1 = y0; + for (const auto& pt : roi.ROIPoints) { + x0 = std::min(x0, pt.x); y0 = std::min(y0, pt.y); + x1 = std::max(x1, pt.x); y1 = std::max(y1, pt.y); + } + if (x1 > x0 && y1 > y0) _exclusiveROIs.emplace_back(x0, y0, x1 - x0, y1 - y0); + } } -bool ANSCustomFS::IsFireDetected(const cv::Mat image, const cv::Rect bBox) { - bool isFireColour = this->DetectFireNSmokeColourInFrame(image, bBox, this->_fire_colour, _hsvThreshold); - return isFireColour; -} -bool ANSCustomFS::IsSmokeDetected(const cv::Mat image, const cv::Rect bBox) { - bool isSmokeColour = this->DetectFireNSmokeColourInFrame(image, bBox, this->_smoke_colour, 0.9); - return isSmokeColour; -} -float ANSCustomFS::calculateIoU(const cv::Rect& box1, const cv::Rect& box2) { - cv::Rect intersection = box1 & box2; - int intersectionArea = intersection.area(); - if (intersectionArea <= 0) return 0.0f; - int unionArea = box1.area() + box2.area() - intersectionArea; - if (unionArea <= 0) return 0.0f; - return static_cast(intersectionArea) / unionArea; -} -bool ANSCustomFS::IsOverlapping(const ANSCENTER::Object& obj, const std::vector& objectList, float iouThreshold) +// --------------------------------------------------------------------------- +// a frame +// --------------------------------------------------------------------------- +std::vector ANSCustomFS::RunInference(const cv::Mat& input) { - if (objectList.empty()) return false; - for (const auto& otherObj : objectList) - { - float iou = calculateIoU(obj.box, otherObj.box); - if (iou > iouThreshold) - { - return true; - } - } - return false; -} -bool ANSCustomFS::IsOverlapping(const ANSCENTER::Object& obj, const std::vector& objectList, float iouThreshold) { - if (objectList.empty()) return false; - for (const auto& otherObj : objectList) - { - float iou = calculateIoU(obj.box, otherObj); - if (iou > iouThreshold) - { - return true; - } - } - return false; -} -bool ANSCustomFS::IsROIOverlapping(const cv::Rect& obj, const std::vector& objectList, float iouThreshold) { - if (objectList.empty()) return false; - - for (const auto& otherObj : objectList) - { - float iou = calculateIoU(obj, otherObj); - if (iou > iouThreshold) - { - return true; - } - } - return false; + return RunInference(input, "CustomCam"); } -// Functions for screen size division -double ANSCustomFS::calculateDistanceToCenter(const cv::Point& center, const cv::Rect& rect) { - cv::Point rectCenter(rect.x + rect.width / 2, rect.y + rect.height / 2); - return std::sqrt(std::pow(rectCenter.x - center.x, 2) + std::pow(rectCenter.y - center.y, 2)); -} -std::vector ANSCustomFS::divideImage(const cv::Mat& image) { - if (image.empty()) { - std::cerr << "Error: Empty image!" << std::endl; - return cachedSections; - } - cv::Size currentSize(image.cols, image.rows); - - if (currentSize == previousImageSize) { - return cachedSections; - } - - previousImageSize = currentSize; - cachedSections.clear(); - - int width = image.cols; - int height = image.rows; - int maxDimension = std::max(width, height); - int numSections = 10; - if (maxDimension <= 2560)numSections = 8; - if (maxDimension <= 1920)numSections = 6; - if (maxDimension <= 1280)numSections = 4; - if (maxDimension <= 960)numSections = 2; - if (maxDimension <= 480)numSections = 1; - - int gridRows = static_cast(std::sqrt(numSections)); - int gridCols = (numSections + gridRows - 1) / gridRows; - - int sectionWidth = width / gridCols; - int sectionHeight = height / gridRows; - - cv::Point imageCenter(width / 2, height / 2); - std::vector> distancePriorityList; - - for (int r = 0; r < gridRows; ++r) { - for (int c = 0; c < gridCols; ++c) { - int x = c * sectionWidth; - int y = r * sectionHeight; - int w = (c == gridCols - 1) ? width - x : sectionWidth; - int h = (r == gridRows - 1) ? height - y : sectionHeight; - - ImageSection section(cv::Rect(x, y, w, h)); - double distance = calculateDistanceToCenter(imageCenter, section.region); - distancePriorityList.emplace_back(distance, section); - } - } - - std::sort(distancePriorityList.begin(), distancePriorityList.end(), - [](const std::pair& a, const std::pair& b) { - if (std::abs(a.first - b.first) > 1e-5) { - return a.first < b.first; - } - return a.second.region.y == b.second.region.y - ? a.second.region.x < b.second.region.x - : a.second.region.y < b.second.region.y; - }); - - int priority = 1; - for (auto& entry : distancePriorityList) { - entry.second.priority = priority++; - cachedSections.push_back(entry.second); - } - - return cachedSections; -} -int ANSCustomFS::getHighestPriorityRegion() { - // 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() { - // 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) { - if (section.priority == priority) { - return section.region; - } - } - return cv::Rect(); -} - -std::vector ANSCustomFS::RunInference(const cv::Mat& input, const std::string& camera_id) { - std::lock_guard lock(_mutex); - - if (input.empty() || input.cols < MIN_IMAGE_SIZE || input.rows < MIN_IMAGE_SIZE) { - return {}; - } - - if (!_detector) { - return {}; - } - - try { - GetModelParameters(); - -#ifdef FNS_DEBUG - cv::Mat draw = input.clone(); -#else - cv::Mat draw; -#endif - - std::vector output; - - // A. Check if detected area is retained and valid - if (_detectedArea.width > MIN_ROI_SIZE && _detectedArea.height > MIN_ROI_SIZE) { -#ifdef FNS_DEBUG - cv::rectangle(draw, _detectedArea, cv::Scalar(0, 0, 255), 2); - for (const auto& roi : _exclusiveROIs) { - cv::rectangle(draw, roi, cv::Scalar(255, 0, 0), 2); - } -#endif - output = ProcessExistingDetectedArea(input, camera_id, draw); - } - else { - // B. Find new detected area - output = FindNewDetectedArea(input, camera_id, draw); - } - -#ifdef FNS_DEBUG - DisplayDebugFrame(draw); -#endif - - std::vector results = ConvertToCustomObjects(output); - - if (results.empty()) { - UpdateNoDetectionCondition(); - } - - return results; - - } - catch (const std::exception& e) { - return {}; - } -} - -// 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, - cv::Mat& draw) +std::vector ANSCustomFS::RunInference(const cv::Mat& input, const std::string& camera_id) { - std::vector output; + std::lock_guard lock(_mutex); + if (input.empty() || input.cols < 16 || input.rows < 16) return {}; + if (!_detector) return {}; - cv::Mat activeROI = frame(_detectedArea); + try { + applyParams(); - // Run detector on ROI — tracker assigns persistent trackIds - std::vector detectedObjects; - _detector->RunInference(activeROI, camera_id.c_str(), detectedObjects); + // The verifier reads BGR8 rows; anything else is converted once. + cv::Mat bgr; + if (input.type() == CV_8UC3) bgr = input; + else if (input.type() == CV_8UC1) cv::cvtColor(input, bgr, cv::COLOR_GRAY2BGR); + else if (input.type() == CV_8UC4) cv::cvtColor(input, bgr, cv::COLOR_BGRA2BGR); + else input.convertTo(bgr, CV_8UC3); + const int W = bgr.cols, H = bgr.rows; - if (detectedObjects.empty()) { - // Age all existing tracks (missed frame for all) - AgeTracks({}); - UpdateNoDetectionCondition(); - return output; - } + // Time: the replay's own clock when a harness set one, else real time. + double t; + if (_externalClock >= 0) { + t = _externalClock; + } else { + const auto now = std::chrono::steady_clock::now(); + if (!_clockStarted) { _t0 = now; _clockStarted = true; } + t = std::chrono::duration(now - _t0).count(); + } + // The detector is not run faster than MaxFps: evidence is per second + // of scene, not per frame, and a camera at 25 fps shows the verifier + // nothing a 6 fps sample does not. Between runs the last verdict holds. + if (t - _lastRunT < 1.0 / _maxFps - 1e-6) return _lastResults; + _lastRunT = t; + ++_frames; - // Precompute full-frame HSV once so we can sample V per bbox cheaply. - cv::Mat frameHsv; - cv::cvtColor(frame, frameHsv, cv::COLOR_BGR2HSV); + const Gray gray = smallGray(bgr.data, W, H, int(bgr.step)); + _verifier.beginFrame(gray, t, float(W) / float(std::max(1, H))); - // COCO filter is expensive — run it at most once per frame and reuse. - bool excludedComputed = false; - std::vector excludedObjects; + // 1. the whole frame, heard at a low floor, one box per object + // whatever the class: fire and fake_fire on the same flame are ONE + // candidate, and which of the two the model preferred is the + // evidence. + std::vector raw; + runDetector(bgr, cv::Rect(0, 0, W, H), raw, camera_id); + nmsClassAgnostic(raw, 0.5f); - // Collect detected track IDs this frame for aging - std::unordered_set detectedTrackIds; + std::vector cands; + for (const auto& d : raw) { + if (d.classId < 0 || size_t(d.classId) >= _roles.size()) continue; + Candidate c; + c.box = d.box.clamped(); + c.role = _roles[size_t(d.classId)]; + c.conf = d.conf; + if (c.role == Role::Other || c.box.w <= 0 || c.box.h <= 0) continue; + if (c.box.area() < _minObjectAreaRatio) continue; + if (!_exclusiveROIs.empty()) { + const cv::Rect px = pixelRect(c.box, W, H); + bool excluded = false; + for (const auto& ex : _exclusiveROIs) + if ((px & ex).area() > 0) { excluded = true; break; } + if (excluded) continue; + } + cands.push_back(c); + } - for (auto& detectedObj : detectedObjects) { - // Adjust coordinates to full frame - detectedObj.box.x += _detectedArea.x; - detectedObj.box.y += _detectedArea.y; - detectedObj.cameraId = camera_id; + std::vector alarms; + if (cands.empty()) { + _verifier.update(cands, alarms); + _lastResults.clear(); + return _lastResults; + } - // 1. Exclusive ROI check — skip if overlapping exclusion zones - if (IsROIOverlapping(detectedObj.box, _exclusiveROIs, INCLUSIVE_IOU_THRESHOLD)) { - continue; - } + // 2. look-alike objects on the whole frame - only now that there is a + // candidate to explain, which on a quiet camera is almost never. + std::vector objects; + if (_filter) runFilter(bgr, cv::Rect(0, 0, W, H), objects, camera_id); - // 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); + // 3. cues for every candidate; a native-resolution re-check for the + // few small fire candidates the plan picks. + for (auto& c : cands) { + RectF pb{ c.box.x - 0.1f * c.box.w, c.box.y - 0.1f * c.box.h, c.box.w * 1.2f, c.box.h * 1.2f }; + c.cues = cuesFor(bgr, pb.clamped()); + } + const double nativeMin = double(std::min(W, H)); + for (int i : _verifier.planZoom(cands, _zoomBudget)) { + Candidate& c = cands[size_t(i)]; + const double bw = c.box.w * W, bh = c.box.h * H; + const double side = std::min(std::max(3.0 * std::max(bw, bh), 320.0), nativeMin); + // Only a real MAGNIFICATION is evidence. A "zoom" that is most of + // the frame shows the model what it has already seen, minus the + // context. + if (side > 0.5 * nativeMin) continue; + RectF roi; + roi.w = float(side / W); + roi.h = float(side / H); + roi.x = std::clamp(c.box.x + c.box.w / 2 - roi.w / 2, 0.f, 1.f - roi.w); + roi.y = std::clamp(c.box.y + c.box.h / 2 - roi.h / 2, 0.f, 1.f - roi.h); + const cv::Rect px = pixelRect(roi, W, H); + std::vector z; + if (!runDetector(bgr, px, z, camera_id)) continue; + c.zoomValid = true; + const float ccx = c.box.x + c.box.w / 2, ccy = c.box.y + c.box.h / 2; + for (const auto& d : z) { + if (d.classId < 0 || size_t(d.classId) >= _roles.size()) continue; + const bool hit = d.box.iou(c.box) > 0.1f || + (ccx >= d.box.x && ccx <= d.box.x + d.box.w && + ccy >= d.box.y && ccy <= d.box.y + d.box.h); + if (!hit) continue; + const Role r = _roles[size_t(d.classId)]; + if (r == Role::Fire) c.zoomReal = std::max(c.zoomReal, d.conf); + if (r == Role::FakeFire) c.zoomFake = std::max(c.zoomFake, d.conf); + } + if (_filter) runFilter(bgr, px, objects, camera_id); + } + for (auto& c : cands) + for (const auto& o : objects) { + if (o.classId < 0 || size_t(o.classId) >= _vehicle.size()) continue; + if (cover(c.box, o.box) <= 0.6f) continue; + if (_vehicle[size_t(o.classId)]) c.onVehicle = true; + // A screen that fills the view is somebody TESTING with a + // replay, and they are owed their alarm; a small one is a TV. + if (_screen[size_t(o.classId)] && o.box.area() < 0.4f) c.onScreen = true; + } - if (!isValidFire && !isValidSmoke && !passesLowConfFire) { - continue; - } + // 4. judgement + _verifier.update(cands, alarms); + std::vector results; + results.reserve(alarms.size()); + for (const auto& a : alarms) { + const int cls = a.smoke ? _smokeClass : _fireClass; + if (cls < 0) continue; + CustomObject co; + co.classId = cls; + co.className = a.smoke ? _smokeName : _fireName; + // A confirmed track is a real positive; the caller's score filter + // must not drop it, so the reported confidence never sits under + // the threshold the model was opened with. + co.confidence = std::max(std::clamp(a.confidence, 0.f, 1.f), _detectionScoreThreshold); + co.box = pixelRect(a.box, W, H); + co.trackId = int(a.trackId & 0x7fffffff); + co.cameraId = camera_id; + char buf[160]; + std::snprintf(buf, sizeof buf, "fs_seconds=%.1f;fs_track=%llu;evidence=%.2f;held=%d", + a.seconds, static_cast(a.trackId), a.evidence, a.held ? 1 : 0); + co.extraInfo = buf; + results.push_back(std::move(co)); + } - // 2b. Minimum-size gate — reject boxes too small relative to the frame. - // Applied before voting so tiny noise never accumulates confirmation votes. - if (!IsObjectLargeEnough(detectedObj.box, frame.size())) { - continue; - } - - // 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, detectedObj.confidence, vMean); - -#ifdef FNS_DEBUG - // Draw detection with track info - cv::Scalar color = (detectedObj.classId == 0) ? cv::Scalar(0, 255, 255) : cv::Scalar(255, 0, 255); - cv::rectangle(draw, detectedObj.box, color, 2); - auto trackIt = _trackHistory.find(trackId); - if (trackIt != _trackHistory.end()) { - std::string label = "T" + std::to_string(trackId) + " H" + - std::to_string(trackIt->second.detectedCount) + "/" + - 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 - - // 5. Voting — which path (if any) confirmed this track? - const bool highConfirmed = IsTrackConfirmed(trackId); - const bool lowConfirmed = IsTrackLowConfirmed(trackId); - if (!highConfirmed && !lowConfirmed) { - 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; - } - - // 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); - if (_realFireCheck <= 0) { - _isRealFireFrame = false; - } - continue; - } - - // All checks passed — confirmed detection - AddConfirmedDetection(detectedObj, output); - _realFireCheck++; - } - - // Age out tracks not seen this frame - AgeTracks(detectedTrackIds); - - if (output.empty()) { - UpdateNoDetectionCondition(); - } - - return output; -} - -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; - _isRealFireFrame = true; + if (_fsLog) { + const Motion& m = _verifier.motion(); + for (const auto& d : _verifier.debug()) + std::fprintf(stderr, + "[fs] t=%.2f gm=(%.3f,%+.3f,%+.3f q%.2f) #%llu %s %s ev=%.2f obs=%d dyn=%.2f " + "flame=%.2f core=%.2f red=%.2f zoom=%.2f veh=%.2f scr=%.2f drift=%.2f blocked=%s\n", + t, m.s, m.tx, m.ty, m.q, static_cast(d.id), + d.smoke ? "S" : "F", d.confirmed ? "ALARM" : "-", d.evidence, d.obs, d.dyn, + d.flame, d.core, d.red, d.zoom, d.vehicle, d.screen, d.drift, d.blockedBy); + } + _lastResults = results; + return results; + } + catch (const std::exception& e) { + std::cerr << "ANSCustomFS::RunInference: exception: " << e.what() << std::endl; + return {}; + } + catch (...) { + return {}; + } } diff --git a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h index fa454d6..b44be0c 100644 --- a/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h +++ b/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h @@ -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 -#include -#include +#include "vendor/engine/fire_smoke.hpp" + +#include +#include +#include +#include +#include -#define RETAINFRAMES 80 -#define FILTERFRAMES 10 -//#define FNS_DEBUG class CUSTOM_API ANSCustomFS : public IANSCustomClass { - typedef std::pair 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 bboxHistory; // bounding box history within 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 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; + 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 _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 _frameHistory; - std::deque _frameQueue; - std::string _filterLabels; - int _realFireCheck{ 0 }; - bool _isRealFireFrame{ false }; - double _hsvThreshold; - std::vector _smoke_colour; - std::vector _fire_colour; + // ----- the package's classes ----- + std::vector _labels; ///< detector classes, in model order + std::vector _roles; ///< fire / fake_fire / smoke / fake_smoke per class + int _fireClass{ -1 }, _smokeClass{ -1 }; + std::string _fireName, _smokeName; + std::vector _filterLabels; + std::vector _vehicle, _screen; ///< which filter classes explain a look-alike away - // ----- Model config ----- - int _detectorModelType; - int _detectorDetectionType; - int _filterModelType; - int _filterDetectionType; - int _motionModelType; - int _motionDetectionType; + // ----- the verifier and its knobs ----- + ansmind::firesmoke::Verifier _verifier; + ansmind::firesmoke::Params _p; + int _zoomBudget{ 2 }; + double _maxFps{ 6.0 }; + float _minObjectAreaRatio{ 0.0003f }; ///< a candidate box under this fraction of the frame is noise ("MinObjectSize", %) + bool _fsLog{ false }; + std::vector _exclusiveROIs; ///< pixel rectangles no candidate may touch + std::string _paramSignature; ///< what the last applyParams() saw - // Add these constants at class level - int MIN_ROI_SIZE = 50; - float SMOKE_CONFIDENCE_THRESHOLD = 0.7f; - float DETECTION_CONFIDENCE_THRESHOLD = 0.5f; - float MAX_AREA_OVERLAP = 0.5f; - int MAX_MOTION_TRACKING = 10; - int FILTER_VERIFICATION_FRAMES = 10; - int MIN_IMAGE_SIZE = 10; - float INCLUSIVE_IOU_THRESHOLD = 0.001f; - float MOTION_SENSITIVITY = 0.5f; + // ----- time ----- + std::chrono::steady_clock::time_point _t0{}; + bool _clockStarted{ false }; + double _externalClock{ -1 }; ///< a replay's video time, when set + double _lastRunT{ -1e9 }; + std::vector _lastResults; + uint64_t _frames{ 0 }; - // Excluded filter class IDs - const std::unordered_set EXCLUDED_FILTER_CLASSES = - { - 13, // bench - 59, // bed - 60, // dining table - 68, // microwave - 69, // oven - 70 // toaster - }; - - cv::Size previousImageSize = cv::Size(0, 0); - std::vector cachedSections; - int _currentPriority{ 0 }; - bool _readROIs{ false }; - std::vector _exclusiveROIs; - float _smokeDetetectionThreshold{ 0 }; - float _motionSpecificity{ 0 }; - // Min fire/smoke box area as a fraction of the full frame; see DEFAULT_MIN_OBJECT_RATIO. - float _minObjectAreaRatio{ DEFAULT_MIN_OBJECT_RATIO }; - - // Tracker-based voting state - std::unordered_map _trackHistory; - - cv::Rect GenerateMinimumSquareBoundingBox(const std::vector& detectedObjects, int minSize = 640); - void UpdateNoDetectionCondition(); - bool detectScreenFlicker(const std::vector& frames); - bool detectReflection(const cv::Mat& frame); - bool MajorityColourInFrame(cv::Mat frame, Range range, float area_threshold); - bool MajorityColourInFrame(cv::Mat frame, std::vector ranges, float area_threshold); - bool DetectFireNSmokeColourInFrame(const cv::Mat& frame, const cv::Rect& bBox, const std::vector& 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& objectList, float iouThreshold = 0.5); - bool IsOverlapping(const ANSCENTER::Object& obj, const std::vector& objectList, float iouThreshold = 0.5); - - bool IsROIOverlapping(const cv::Rect& obj, const std::vector& 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& 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 ConvertToCustomObjects(const std::vector& objects); - void ResetDetectionState(); - void GetModelParameters(); - std::vector ProcessExistingDetectedArea(const cv::Mat& frame, const std::string& camera_id, cv::Mat& draw); - void AddConfirmedDetection(ANSCENTER::Object& detectedObj, std::vector& 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 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& 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); - - std::vector FindMovementObjects( - const cv::Mat& frame, - const std::string& camera_id, cv::Mat& draw); - - std::vector FindExcludedObjects( - const cv::Mat& frame, - const std::string& camera_id, cv::Mat& draw); + // ----- steps of a frame ----- + void applyParams(); + bool runDetector(const cv::Mat& bgr, const cv::Rect& roi, std::vector& out, const std::string& camera); + bool runFilter(const cv::Mat& bgr, const cv::Rect& roi, std::vector& out, const std::string& camera); + static void nmsClassAgnostic(std::vector& dets, float iouThr); + ansmind::firesmoke::Cues cuesFor(const cv::Mat& bgr, const RectF& box) const; + static cv::Rect pixelRect(const RectF& r, int W, int H); + static RectF normRect(const cv::Rect& r, int W, int H); + static float cover(const RectF& a, const RectF& b); public: bool Initialize(const std::string& modelDiretory, float detectionScoreThreshold, std::string& labelMap) override; @@ -179,6 +89,15 @@ public: std::vector RunInference(const cv::Mat& input) override; std::vector RunInference(const cv::Mat& input, const std::string& camera_id) 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 LastDebug() const { return _verifier.debug(); } + ANSCustomFS(); ~ANSCustomFS(); }; diff --git a/ANSCustomFireNSmokeDetection/README.md b/ANSCustomFireNSmokeDetection/README.md new file mode 100644 index 0000000..521ceb8 --- /dev/null +++ b/ANSCustomFireNSmokeDetection/README.md @@ -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