Fix ALPR Batch and memory leak

This commit is contained in:
2026-04-15 09:23:05 +10:00
parent 7778f8c214
commit b05c49ad93
9 changed files with 686 additions and 83 deletions

View File

@@ -50,6 +50,7 @@ namespace ANSCENTER
};
// cameraId → (trackId → tracked plate)
std::unordered_map<std::string, std::unordered_map<int, TrackedPlateById>> trackedPlatesById;
int _pruneCounterById = 0; // counts checkPlateByTrackId calls for periodic pruning
public:
void Init(int framesToStore = MAX_ALPR_FRAME);
@@ -100,6 +101,28 @@ namespace ANSCENTER
[[nodiscard]] virtual bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult) = 0;
[[nodiscard]] virtual bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult,const std::string & cameraId) = 0;
[[nodiscard]] virtual std::vector<Object> RunInference(const cv::Mat& input, const std::string &cameraId) = 0;
/// Stateless batch inference for pipeline mode.
/// For each vehicle ROI in `vehicleBoxes` (in FRAME coordinates), crop
/// the vehicle, run LP detection and text recognition, and return
/// detected plates in FULL-FRAME coordinates.
///
/// Tracker, voting, spatial dedup, and per-camera accumulating state
/// are all bypassed — this is the fast-path for callers that already
/// have precise vehicle bboxes and want raw per-frame results with no
/// cross-frame memory. The implementation issues a single batched
/// `_lpDetector->RunInferencesBatch` call for detection and a single
/// batched recognizer call for OCR, so the ORT/TRT allocator sees
/// exactly one shape per frame regardless of how many vehicles the
/// caller passes.
///
/// Default implementation falls back to calling `RunInference` in a
/// loop per crop so older subclasses keep compiling.
[[nodiscard]] virtual std::vector<Object> RunInferencesBatch(
const cv::Mat& input,
const std::vector<cv::Rect>& vehicleBoxes,
const std::string& cameraId);
[[nodiscard]] std::string VectorDetectionToJsonString(const std::vector<Object>& dets);
void SetPlateFormats(const std::vector<std::string>& formats);
void SetPlateFormat(const std::string& format);
@@ -167,6 +190,17 @@ extern "C" ANSLPR_API int ANSALPR_RunInferenceComplete_LV(ANSCENTER::ANSALPR
extern "C" ANSLPR_API int ANSALPR_RunInferenceComplete_CPP(ANSCENTER::ANSALPR** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize,std::string& detectionResult, std::string& imageStr);
extern "C" ANSLPR_API int ANSALPR_RunInferencesComplete_LV(ANSCENTER::ANSALPR** Handle, cv::Mat** cvImage, const char* cameraId, int maxImageSize,const char* strBboxes, LStrHandle detectionResult);
// Dedicated pipeline-mode batch inference:
// - always runs with tracker OFF, voting OFF, spatial dedup OFF
// - issues ONE batched LP-detect call and ONE batched recognizer call
// across every vehicle in `strBboxes`, instead of looping per crop
// - returns plate bboxes in the caller's resized coordinate space (same
// semantics as ANSALPR_RunInferencesComplete_LV)
// Use this in LabVIEW whenever the caller already has vehicle bboxes and
// wants raw per-frame results. Fixes the cross-frame allocator churn that
// causes ANSALPR_OCR memory growth under the per-bbox loop path.
extern "C" ANSLPR_API int ANSALPR_RunInferencesBatch_LV(ANSCENTER::ANSALPR** Handle, cv::Mat** cvImage, const char* cameraId, int maxImageSize, const char* strBboxes, LStrHandle detectionResult);
// Get/Set format
extern "C" ANSLPR_API int ANSALPR_SetFormat(ANSCENTER::ANSALPR** Handle, const char* format);
extern "C" ANSLPR_API int ANSALPR_SetFormats(ANSCENTER::ANSALPR** Handle, const char* formats);// comma separated formats