Add CPU/GPU gate and support new ANSALPR using OCR

This commit is contained in:
2026-04-12 17:16:16 +10:00
parent 27083a6530
commit 0a8aaed215
30 changed files with 1870 additions and 2166 deletions

View File

@@ -680,6 +680,19 @@ namespace ANSCENTER {
std::vector<float> ANSFaceRecognizer::RunArcFace(const cv::Mat& inputImage) {
std::vector<float> embedding;
// Defense-in-depth: this function uses m_gpuStream / cv::cuda::GpuMat
// upload path, which is only valid on NVIDIA hardware. Callers in
// Feature() and ExtractEmbeddings() already gate on engineType, but
// the method is public — refuse to run on AMD/Intel/CPU so we never
// touch m_gpuStream (lazy-initialized, nullptr on non-NVIDIA) or
// m_gpuRgb.upload() which would activate the CUDA runtime.
if (engineType != EngineType::NVIDIA_GPU) {
_logger.LogError("ANSFaceRecognizer::RunArcFace",
"RunArcFace is NVIDIA-only; called on engineType="
+ std::to_string(static_cast<int>(engineType)), __FILE__, __LINE__);
return embedding;
}
// Early validation before locking
if (inputImage.empty()) {
_logger.LogError("ANSFaceRecognizer::RunArcFace",
@@ -701,6 +714,13 @@ namespace ANSCENTER {
return embedding;
}
if (!m_gpuStream || !m_trtEngine) {
_logger.LogError("ANSFaceRecognizer::RunArcFace",
"GPU stream or TRT engine not available (engineType="
+ std::to_string(static_cast<int>(engineType)) + ")", __FILE__, __LINE__);
return embedding;
}
try {
// CPU preprocessing: resize + BGR→RGB before GPU upload
// Reduces PCIe transfer and eliminates GPU cvtColor/resize overhead
@@ -761,6 +781,17 @@ namespace ANSCENTER {
{
std::vector<std::vector<float>> embeddings;
// Defense-in-depth: TensorRT + cv::cuda::GpuMat batch path is NVIDIA-only.
// Callers in ExtractEmbeddings() already gate on engineType, but this is a
// public method — refuse to run on AMD/Intel/CPU so we never touch the
// TRT engine or cv::cuda primitives on non-NVIDIA hardware.
if (engineType != EngineType::NVIDIA_GPU) {
_logger.LogError("ANSFaceRecognizer::RunArcFaceBatch",
"RunArcFaceBatch is NVIDIA-only; called on engineType="
+ std::to_string(static_cast<int>(engineType)), __FILE__, __LINE__);
return embeddings;
}
try {
// Early validation checks
if (!_isInitialized) {
@@ -775,6 +806,12 @@ namespace ANSCENTER {
return embeddings;
}
if (!m_gpuStream) {
_logger.LogError("ANSFaceRecognizer::RunArcFaceBatch",
"GPU stream not initialized", __FILE__, __LINE__);
return embeddings;
}
if (faceROIs.empty()) {
return embeddings;
}