Fix AMD ALPR_OCR and custom model

This commit is contained in:
2026-05-01 07:53:18 +10:00
parent e1f37ec767
commit 91efbb9a8f
25 changed files with 2841 additions and 429 deletions
+26
View File
@@ -7,6 +7,7 @@
#include <fstream>
#include <string>
#include <filesystem>
#include <atomic>
#include <opencv2/opencv.hpp>
#include "Utility.h"
#include "ANSLicense.h"
@@ -98,6 +99,31 @@ namespace ANSCENTER
bool _debugFlag{ false };
bool _licenseValid{ false };
bool _isInitialized{ false};
// Loading-in-progress flag — distinguishes the "LoadEngine() is still
// running" state from "LoadEngine() never called or failed". Set by
// ModelLoadingGuard at the top of LoadEngine() and cleared on exit
// (success, failure, or exception). RunInference/RunInferencesBatch
// consult this BEFORE _isInitialized so the early frames during the
// 3+ second model-load window don't flood the log with bogus
// "Model is not initialized" errors.
std::atomic<bool> _modelLoading{ false };
// RAII helper: sets _modelLoading=true on construction, false on
// destruction. Use at the top of LoadEngine() so the flag is always
// cleared on every exit path.
struct ModelLoadingGuard {
std::atomic<bool>& flag;
explicit ModelLoadingGuard(std::atomic<bool>& f) : flag(f) {
flag.store(true, std::memory_order_release);
}
~ModelLoadingGuard() {
flag.store(false, std::memory_order_release);
}
ModelLoadingGuard(const ModelLoadingGuard&) = delete;
ModelLoadingGuard& operator=(const ModelLoadingGuard&) = delete;
};
double _detectorThreshold{ 0.5 };
double _ocrThreshold{ 0.5 };
double _colorThreshold{ 0.0 };// If we use the colour information, this threshold should be greater than 0.0
+37 -20
View File
@@ -221,6 +221,11 @@ namespace ANSCENTER
bool ANSALPR_OCR::LoadEngine() {
std::lock_guard<std::recursive_mutex> lock(_mutex);
// RAII guard: set _modelLoading=true for the lifetime of this scope.
// RunInference / RunInferencesBatch consult this flag and silently
// defer (instead of logging "Model is not initialized") while we're
// loading the LP detector + OCR engine + colour classifier.
ModelLoadingGuard mlg(_modelLoading);
try {
WriteEventLog("ANSALPR_OCR::LoadEngine: Step 1 - Starting engine load");
this->_logger.LogInfo("ANSALPR_OCR::LoadEngine", "Step 1: Starting engine load", __FILE__, __LINE__);
@@ -1479,22 +1484,28 @@ namespace ANSCENTER
this->_logger.LogError("ANSALPR_OCR::RunInference", "Invalid license", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
this->_logger.LogError("ANSALPR_OCR::RunInference", "Model is not initialized", __FILE__, __LINE__);
// LoadEngine() may be running on another thread (3+ seconds for all
// models). Silently defer instead of spamming "Model is not
// initialized" — the caller will retry on the next frame.
if (_modelLoading.load(std::memory_order_acquire)) {
return {};
}
// Source-of-truth check on the actual engine pointers. If LoadEngine
// failed or Destroy() was called, _lpDetector / _ocrEngine are null
// and we want a real error logged so the operator sees it.
if (!_lpDetector || !_ocrEngine) {
this->_logger.LogError("ANSALPR_OCR::RunInference",
"Model not loaded (LoadEngine not called or failed)",
__FILE__, __LINE__);
return {};
}
if (input.empty() || input.cols < 5 || input.rows < 5) {
this->_logger.LogError("ANSALPR_OCR::RunInference", "Input image is empty or too small", __FILE__, __LINE__);
return {};
}
if (!_lpDetector) {
this->_logger.LogFatal("ANSALPR_OCR::RunInference", "_lpDetector is null", __FILE__, __LINE__);
return {};
}
if (!_ocrEngine) {
this->_logger.LogFatal("ANSALPR_OCR::RunInference", "_ocrEngine is null", __FILE__, __LINE__);
return {};
}
try {
// Convert grayscale to BGR if necessary
@@ -1856,19 +1867,25 @@ namespace ANSCENTER
this->_logger.LogError("ANSALPR_OCR::RunInferencesBatch", "Invalid license", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
this->_logger.LogError("ANSALPR_OCR::RunInferencesBatch", "Model is not initialized", __FILE__, __LINE__);
// LoadEngine() may be running on another thread (3+ seconds for all
// models). Silently defer instead of spamming "Model is not
// initialized" — the caller will retry on the next frame.
if (_modelLoading.load(std::memory_order_acquire)) {
return {};
}
// Source-of-truth check on the actual engine pointers. If LoadEngine
// failed or Destroy() was called, _lpDetector / _ocrEngine are null
// and we want a real error logged so the operator sees it.
if (!_lpDetector || !_ocrEngine) {
this->_logger.LogError("ANSALPR_OCR::RunInferencesBatch",
"Model not loaded (LoadEngine not called or failed)",
__FILE__, __LINE__);
return {};
}
if (input.empty() || input.cols < 5 || input.rows < 5) return {};
if (!_lpDetector) {
this->_logger.LogFatal("ANSALPR_OCR::RunInferencesBatch", "_lpDetector is null", __FILE__, __LINE__);
return {};
}
if (!_ocrEngine) {
this->_logger.LogFatal("ANSALPR_OCR::RunInferencesBatch", "_ocrEngine is null", __FILE__, __LINE__);
return {};
}
if (vehicleBoxes.empty()) return {};
try {