From 66c67dff26a747d48b2ba20446bfe17322b1ebd5 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Tue, 5 May 2026 16:23:01 +1000 Subject: [PATCH] Fix onnx runtime loader for custom model --- .claude/settings.local.json | 6 ++- modules/ANSODEngine/ANSCUSTOMDETECTOR.cpp | 49 ++++++++++++++++++- .../ANSODEngine-UnitTest.cpp | 21 ++++---- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/.claude/settings.local.json b/.claude/settings.local.json index a809e01..db3449b 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -221,7 +221,11 @@ "PowerShell($env:Path = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Tools\\\\Llvm\\\\x64\\\\bin;\" + $env:Path; & \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 2>&1 | Out-Null; Get-Command ninja -ErrorAction SilentlyContinue | Select-Object Source)", "PowerShell($ninjaPaths = @\\(\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\CMake\\\\Ninja\\\\ninja.exe\", \"C:\\\\ProgramData\\\\chocolatey\\\\bin\\\\ninja.exe\", \"C:\\\\tools\\\\ninja\\\\ninja.exe\"\\); foreach \\($p in $ninjaPaths\\) { if \\(Test-Path $p\\) { Write-Output $p } })", "PowerShell(cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul 2>&1 && cd /d C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release && \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\\\IDE\\\\CommonExtensions\\\\Microsoft\\\\CMake\\\\Ninja\\\\ninja.exe\" ANSODEngine 2>&1')", - "Bash(python build_anslpr_devguide_pdf.py)" + "Bash(python build_anslpr_devguide_pdf.py)", + "Bash(where dumpbin.exe)", + "Bash(where llvm-symbolizer.exe)", + "Bash(where cdb.exe)", + "Bash(\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/llvm-symbolizer.exe\" --obj=\"C:/Projects/AIVISION/ANSFallDetection/ANSCustomFall/Dll1/x64/Release/ANSCustomFall.dll\" 0xb1d1)" ] } } diff --git a/modules/ANSODEngine/ANSCUSTOMDETECTOR.cpp b/modules/ANSODEngine/ANSCUSTOMDETECTOR.cpp index 55b9631..a085218 100644 --- a/modules/ANSODEngine/ANSCUSTOMDETECTOR.cpp +++ b/modules/ANSODEngine/ANSCUSTOMDETECTOR.cpp @@ -1,5 +1,6 @@ #include "ANSCUSTOMDetector.h" #include "Utility.h" +#include "EPLoader.h" #include namespace ANSCENTER @@ -279,12 +280,56 @@ namespace ANSCENTER return false; } + // 2b. Pre-load ANSCENTER's onnxruntime.dll into the process before + // LoadLibrary on the custom plugin. The plugin DLL has a static + // import of "onnxruntime.dll"; if no module with that base name is + // loaded yet, the Windows loader falls back to the safe DLL search + // path and binds to C:\Windows\System32\onnxruntime.dll (the + // Windows ML build, ORT 1.17). That older runtime does not support + // the API version the plugin's headers ask for, so its static-init + // of Ort::Global::api_ becomes nullptr and the first Ort:: + // call inside the plugin crashes. Once EPLoader has loaded the + // matching ORT, by-name match in the loader binds the plugin's + // import to it instead. + try { + const auto& ep = ANSCENTER::EPLoader::Current(); + ANS_DBG("ANSCUSTOM_CreateDet", + "EP_PRELOADED type=%s dir=%s fromSubdir=%d", + ANSCENTER::EPLoader::EngineTypeName(ep.type), + ep.libraryDir.c_str(), + ep.fromSubdir ? 1 : 0); + } + catch (const std::exception& e) { + ANS_DBG("ANSCUSTOM_CreateDet", + "ERROR EPLoader::Current threw — plugin may bind to wrong onnxruntime.dll: %s", + e.what()); + this->_logger.LogFatal("ANSCUSTOMDETECTOR::Initialize", + (std::string("EPLoader::Current failed: ") + e.what()).c_str(), + __FILE__, __LINE__); + return false; + } + // 3. Load the library + // + // Use LoadLibraryExW with LOAD_LIBRARY_SEARCH_DEFAULT_DIRS so the + // plugin's transitive imports (notably "onnxruntime.dll") are + // resolved against directories added with AddDllDirectory — i.e. + // the staging dir EPLoader copied the matching ORT into. Plain + // LoadLibrary uses legacy safe-DLL search, which only consults + // app dir + System32 + PATH and ignores AddDllDirectory entirely; + // that path picks up C:\Windows\System32\onnxruntime.dll (the + // Windows ML build, ORT 1.17), and the plugin's static-init of + // Ort::Global::api_ becomes nullptr — first Ort:: call + // crashes. LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR also lets a custom + // model bundle its own deps next to the plugin DLL if it wants. std::wstring dllPath = String2WString(_modelFilePath); - hMod = LoadLibrary(dllPath.c_str()); + hMod = LoadLibraryExW( + dllPath.c_str(), + nullptr, + LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR); if (!hMod) { DWORD err = GetLastError(); - ANS_DBG("ANSCUSTOM_CreateDet", "ERROR LoadLibrary failed path=%s err=%lu", + ANS_DBG("ANSCUSTOM_CreateDet", "ERROR LoadLibraryEx failed path=%s err=%lu", _modelFilePath.c_str(), (unsigned long)err); this->_logger.LogFatal("ANSCUSTOMDETECTOR::Initialize", "Cannot open library", __FILE__, __LINE__); return false; diff --git a/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp b/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp index b4d56a4..bd88aa1 100644 --- a/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp +++ b/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp @@ -1835,14 +1835,11 @@ int ElasticGPUTest() { } -int FireNSmokeTest() { +int FireNSmokeGetCofigurationTest() { int modelType = 16; // Custom model type for helmet detection, adjust as needed based on how the model is set up in the engine std::string modelFilePath = "C:\\Projects\\ANSVIS\\Models\\ANS_FireSmoke_v2.0.zip"; std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\Helmet\\HM1.mp4"; - - ANSCENTER::ANSODBase* infHandle; - std::string licenseKey = ""; std::string modelZipFilePassword = ""; double detectionScoreThreshold = 0.3; @@ -1851,18 +1848,15 @@ int FireNSmokeTest() { int loadEngine = 0; CreateANSODHandle(&infHandle, licenseKey.c_str(), modelFilePath.c_str(), modelZipFilePassword.c_str(), detectionScoreThreshold, modelConfThreshold, modelNMSThreshold, 1, modelType, 1, loadEngine); - std::string stParam; GetConfiguredParameters_CPP(&infHandle, stParam); std::cout << "Configured parameters:\n" << stParam << std::endl; - ReleaseANSODHandle(&infHandle); - - return 0; } + int ALPROD_Test() { int modelType = 30; // ONNX YOLO (30) RT YOLO (31) std::string onnmodelFilePath = "E:\\Programs\\DemoAssets\\ALPR_Test\\ANS_ALPR_2204(GPU)_1.zip"; @@ -1870,10 +1864,19 @@ int ALPROD_Test() { ImageDetectionEngine(onnmodelFilePath, imageFile, modelType);//yolov8 return 0; } + +int FallDetectorTest() { + int modelType = 16; // Custom model type for helmet detection, adjust as needed based on how the model is set up in the engine + std::string modelFilePath = "E:\\Programs\\CustomModelDev\\ANSFallDetector_v1.0.zip"; + std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\Fall\\video3.mp4"; + VideoDetectorEngine(modelFilePath, videoFile, modelType); + return 0; +} int main() { - ALPROD_Test(); + FallDetectorTest(); + //ALPROD_Test(); //FireNSmokeTest(); //ElasticGPUTest();