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

@@ -181,7 +181,7 @@ namespace ANSCENTER {
};
[[nodiscard]] virtual bool Destroy() = 0;
};
class ANSOCRUtility
class ANSOCR_API ANSOCRUtility
{
public:
[[nodiscard]] static std::string OCRDetectionToJsonString(const std::vector<OCRObject>& dets);

View File

@@ -137,6 +137,18 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
// Ensure all shared DLLs (OpenCV, OpenVINO, TRT, ORT) are pre-loaded
ANSCENTER::ANSLibsLoader::Initialize();
ANSCENTER::EngineType engineType = ANSCENTER::ANSLicenseHelper::CheckHardwareInformation();
{
const char* vendorTag =
engineType == ANSCENTER::EngineType::NVIDIA_GPU ? "NVIDIA_GPU (TensorRT OCR enabled)" :
engineType == ANSCENTER::EngineType::AMD_GPU ? "AMD_GPU (ONNX Runtime / DirectML, TensorRT OCR DISABLED)" :
engineType == ANSCENTER::EngineType::OPENVINO_GPU ? "OPENVINO_GPU (ONNX Runtime / OpenVINO, TensorRT OCR DISABLED)" :
"CPU (ONNX Runtime, TensorRT OCR DISABLED)";
char buf[192];
snprintf(buf, sizeof(buf),
"[ANSOCR] CreateANSOCRHandleEx: detected engineType=%d [%s], engineMode=%d\n",
static_cast<int>(engineType), vendorTag, engineMode);
OutputDebugStringA(buf);
}
// Release existing handle if called twice (prevents leak from LabVIEW)
if (*Handle) {
@@ -159,13 +171,29 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
(*Handle) = new ANSCENTER::ANSCPUOCR();
}
else {
// ANSRTOCR wraps PaddleOCRV5RTEngine which is strictly NVIDIA/CUDA:
// RTOCRDetector/Classifier/Recognizer use cv::cuda::GpuMat, cudaMalloc,
// cudaMemcpy and link against cudart. Instantiating it on AMD, Intel
// or pure-CPU machines either crashes in the CUDA runtime loader or
// hangs in amdkmdag when DirectML and TRT coexist. We therefore
// hard-gate the TRT path on NVIDIA_GPU and fall back to ANSONNXOCR
// (which uses CPU-side NV12 conversion and ONNX Runtime's EP auto-
// select, including DirectML for AMD).
const bool isNvidia = (engineType == ANSCENTER::EngineType::NVIDIA_GPU);
switch (engineMode) {
case 0:// Auto-detect, always use ONNX for better compatibility, especially on AMD GPUs and high-res images
(*Handle) = new ANSCENTER::ANSONNXOCR();
break;
case 1:// GPU — use TensorRT engine.
limitSideLen = 960;
(*Handle) = new ANSCENTER::ANSRTOCR();
case 1:// GPU — use TensorRT engine ONLY on NVIDIA hardware.
if (isNvidia) {
limitSideLen = 960;
(*Handle) = new ANSCENTER::ANSRTOCR();
} else {
// AMD / Intel / CPU requested GPU mode — ANSRTOCR would crash.
// Fall back to ANSONNXOCR which picks the right ORT provider
// (DirectML on AMD, OpenVINO/CPU on Intel, CPU otherwise).
(*Handle) = new ANSCENTER::ANSONNXOCR();
}
break;
case 2:// CPU
(*Handle) = new ANSCENTER::ANSONNXOCR();