Files
ANSCustomModels/ANSCustomFireNSmokeDetection/ANSCustomFireNSmoke.h
T

185 lines
9.3 KiB
C++
Raw Normal View History

2026-03-30 21:43:47 +11:00
#include "ANSLIB.h"
#include <deque>
#include <unordered_set>
2026-04-05 14:30:43 +10:00
#include <unordered_map>
2026-03-30 21:43:47 +11:00
#define RETAINFRAMES 80
#define FILTERFRAMES 10
2026-04-06 09:29:08 +10:00
//#define FNS_DEBUG
2026-03-30 21:43:47 +11:00
class CUSTOM_API ANSCustomFS : public IANSCustomClass
{
typedef std::pair<cv::Scalar, cv::Scalar> Range;
struct ImageSection {
cv::Rect region;
int priority;
ImageSection(const cv::Rect& r) : region(r), priority(0) {}
};
2026-04-05 14:30:43 +10:00
// Track record for voting-based detection confirmation
struct TrackRecord {
int trackId{ 0 };
int classId{ 0 }; // fire=0, smoke=2
std::deque<cv::Rect> bboxHistory; // bounding box history within window
2026-05-28 16:03:16 +10:00
std::deque<int> highConfHits; // parallel: 1 if >=HIGH_CONF_THRESHOLD, else 0
std::deque<double> vMeanHistory; // parallel: mean HSV V inside bbox (for flicker)
int detectedCount{ 0 }; // high-conf hits in sliding window
int lowConfDetectedCount{ 0 }; // total hits (any conf >= LOW_CONF_FIRE_THRESHOLD)
2026-04-05 14:30:43 +10:00
int totalFrames{ 0 }; // total frames since track appeared
2026-05-28 16:03:16 +10:00
bool confirmed{ false }; // passed high-conf voting threshold
2026-04-05 14:30:43 +10:00
};
2026-05-28 16:03:16 +10:00
// 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
2026-04-05 14:30:43 +10:00
static constexpr float BBOX_CHANGE_THRESHOLD = 0.05f;
2026-05-28 16:03:16 +10:00
// 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;
2026-06-18 05:57:14 +10:00
// 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
2026-04-05 14:30:43 +10:00
2026-03-30 21:43:47 +11:00
private:
using ANSLIBPtr = std::unique_ptr<ANSCENTER::ANSLIB, decltype(&ANSCENTER::ANSLIB::Destroy)>;
2026-04-06 07:11:04 +10:00
int engineType{ 0 };
2026-03-30 21:43:47 +11:00
ANSLIBPtr _motiondetector{ nullptr, &ANSCENTER::ANSLIB::Destroy };
ANSLIBPtr _detector{ nullptr, &ANSCENTER::ANSLIB::Destroy };
ANSLIBPtr _filter{ nullptr, &ANSCENTER::ANSLIB::Destroy };
std::recursive_mutex _mutex;
cv::Rect _detectedArea;
cv::Rect _previousDetectedArea;
int _retainDetectedArea{ 0 };
bool _isFireNSmokeDetected{ false };
float _detectionScoreThreshold{ 0.5 };
std::vector<cv::Mat> _frameHistory;
std::deque <cv::Mat> _frameQueue;
std::string _filterLabels;
int _realFireCheck{ 0 };
bool _isRealFireFrame{ false };
double _hsvThreshold;
std::vector<Range> _smoke_colour;
std::vector<Range> _fire_colour;
// ----- Model config -----
int _detectorModelType;
int _detectorDetectionType;
int _filterModelType;
int _filterDetectionType;
int _motionModelType;
int _motionDetectionType;
// 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;
// Excluded filter class IDs
const std::unordered_set<int> 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<ImageSection> cachedSections;
int _currentPriority{ 0 };
bool _readROIs{ false };
std::vector<cv::Rect> _exclusiveROIs;
float _smokeDetetectionThreshold{ 0 };
float _motionSpecificity{ 0 };
2026-06-18 05:57:14 +10:00
// Min fire/smoke box area as a fraction of the full frame; see DEFAULT_MIN_OBJECT_RATIO.
float _minObjectAreaRatio{ DEFAULT_MIN_OBJECT_RATIO };
2026-03-30 21:43:47 +11:00
2026-04-05 14:30:43 +10:00
// Tracker-based voting state
std::unordered_map<int, TrackRecord> _trackHistory;
2026-03-30 21:43:47 +11:00
cv::Rect GenerateMinimumSquareBoundingBox(const std::vector<ANSCENTER::Object>& detectedObjects, int minSize = 640);
void UpdateNoDetectionCondition();
bool detectScreenFlicker(const std::vector<cv::Mat>& frames);
bool detectReflection(const cv::Mat& frame);
bool MajorityColourInFrame(cv::Mat frame, Range range, float area_threshold);
bool MajorityColourInFrame(cv::Mat frame, std::vector<Range> ranges, float area_threshold);
bool DetectFireNSmokeColourInFrame(const cv::Mat& frame, const cv::Rect& bBox, const std::vector<Range>& ranges, float area_threshold);
bool IsFireDetected(const cv::Mat image, const cv::Rect bBox);
bool IsSmokeDetected(const cv::Mat image, const cv::Rect bBox);
float calculateIoU(const cv::Rect& box1, const cv::Rect& box2);
bool IsOverlapping(const ANSCENTER::Object& obj, const std::vector<ANSCENTER::Object>& objectList, float iouThreshold = 0.5);
bool IsOverlapping(const ANSCENTER::Object& obj, const std::vector<cv::Rect>& objectList, float iouThreshold = 0.5);
bool IsROIOverlapping(const cv::Rect& obj, const std::vector<cv::Rect>& objectList, float iouThreshold = 0.5);
bool IsDetectedAreaValid(const cv::Rect& area) const;
2026-06-18 05:57:14 +10:00
bool IsObjectLargeEnough(const cv::Rect& box, const cv::Size& frameSize) const;
2026-03-30 21:43:47 +11:00
bool IsFireOrSmoke(int classId, float confidence) const;
bool ValidateMotionCorrelation(const std::vector<cv::Rect>& fireNSmokeRects) const;
void UpdatePositiveDetection();
void ResetDetectedArea();
void UpdatePriorityRegion(const cv::Mat& frame);
int CalculateOptimalCropSize(const cv::Mat& frame) const;
void RefineDetectedArea(const cv::Mat& frame, const ANSCENTER::Object& detection);
std::vector<CustomObject> ConvertToCustomObjects(const std::vector<ANSCENTER::Object>& objects);
void ResetDetectionState();
void GetModelParameters();
std::vector<ANSCENTER::Object> ProcessExistingDetectedArea(const cv::Mat& frame, const std::string& camera_id, cv::Mat& draw);
void AddConfirmedDetection(ANSCENTER::Object& detectedObj, std::vector<ANSCENTER::Object>& output);
#ifdef FNS_DEBUG
void DisplayDebugFrame(cv::Mat& draw) {
cv::resize(draw, draw, cv::Size(1920, (1920 * draw.rows) / draw.cols));
cv::imshow("Combined Detected Areas", draw);
cv::waitKey(1);
}
#endif
// Function to separate screen size
double calculateDistanceToCenter(const cv::Point& center, const cv::Rect& rect);
std::vector<ImageSection> divideImage(const cv::Mat& image);
int getHighestPriorityRegion();
int getLowestPriorityRegion();
cv::Rect getRegionByPriority(int priority);
2026-05-28 16:03:16 +10:00
// 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)
2026-04-05 14:30:43 +10:00
bool HasBboxMovement(int trackId) const;
2026-05-28 16:03:16 +10:00
bool HasFireFlicker(int trackId) const; // V-channel temporal std-dev gate
2026-04-05 14:30:43 +10:00
void AgeTracks(const std::unordered_set<int>& detectedTrackIds);
2026-05-28 16:03:16 +10:00
// Warm-color priority scan: re-rank cached sections by HSV warm-pixel count
void RankCachedSectionsByWarmColor(const cv::Mat& frame);
2026-03-30 21:43:47 +11:00
std::vector<ANSCENTER::Object> FindNewDetectedArea(
const cv::Mat& frame,
const std::string& camera_id, cv::Mat& draw);
std::vector<ANSCENTER::Object> FindMovementObjects(
const cv::Mat& frame,
const std::string& camera_id, cv::Mat& draw);
std::vector<ANSCENTER::Object> FindExcludedObjects(
const cv::Mat& frame,
const std::string& camera_id, cv::Mat& draw);
public:
bool Initialize(const std::string& modelDiretory, float detectionScoreThreshold, std::string& labelMap) override;
bool OptimizeModel(bool fp16) override;
bool ConfigureParameters(CustomParams& param) override;
std::vector<CustomObject> RunInference(const cv::Mat& input) override;
std::vector<CustomObject> RunInference(const cv::Mat& input, const std::string& camera_id) override;
bool Destroy() override;
ANSCustomFS();
~ANSCustomFS();
};