- Remove ALPR_OD Tracker and voting system.

- Disable debugview
This commit is contained in:
2026-04-19 23:15:50 +10:00
parent 51fe507dfd
commit 3418090042
13 changed files with 1266 additions and 129 deletions

View File

@@ -14,6 +14,7 @@
#include <condition_variable>
#include <cstdint>
// DebugView: filter on "[ANSOCR]" — gated by ANSCORE_DEBUGVIEW in ANSLicense.h.
// Handle registry with refcount — prevents use-after-free when
// ReleaseANSOCRHandle is called while inference is still running.
@@ -37,14 +38,30 @@ static std::condition_variable& OCRHandleRegistryCV() {
static void RegisterOCRHandle(ANSCENTER::ANSOCRBase* h) {
std::lock_guard<std::mutex> lk(OCRHandleRegistryMutex());
OCRHandleRegistry()[h] = { 1, false };
ANS_DBG("ANSOCR","Register: handle=%p (uint=%llu) registrySize=%zu",
(void*)h, (unsigned long long)(uintptr_t)h, OCRHandleRegistry().size());
}
static ANSCENTER::ANSOCRBase* AcquireOCRHandle(ANSCENTER::ANSOCRBase* h) {
std::lock_guard<std::mutex> lk(OCRHandleRegistryMutex());
auto it = OCRHandleRegistry().find(h);
if (it == OCRHandleRegistry().end()) return nullptr;
if (it->second.destructionStarted) return nullptr;
if (it == OCRHandleRegistry().end()) {
ANS_DBG("ANSOCR","Acquire FAIL: handle=%p (uint=%llu) NOT in registry. registrySize=%zu",
(void*)h, (unsigned long long)(uintptr_t)h, OCRHandleRegistry().size());
size_t i = 0;
for (auto& kv : OCRHandleRegistry()) {
ANS_DBG("ANSOCR"," registry[%zu] = %p (uint=%llu) refcount=%d destructionStarted=%d",
i++, (void*)kv.first, (unsigned long long)(uintptr_t)kv.first,
kv.second.refcount, kv.second.destructionStarted ? 1 : 0);
}
return nullptr;
}
if (it->second.destructionStarted) {
ANS_DBG("ANSOCR","Acquire FAIL: handle=%p is being destroyed (destructionStarted=true)", (void*)h);
return nullptr;
}
it->second.refcount++;
ANS_DBG("ANSOCR","Acquire OK: handle=%p refcount=%d", (void*)h, it->second.refcount);
return h;
}
@@ -62,10 +79,15 @@ static bool ReleaseOCRHandleRef(ANSCENTER::ANSOCRBase* h) {
static bool UnregisterOCRHandle(ANSCENTER::ANSOCRBase* h) {
std::unique_lock<std::mutex> lk(OCRHandleRegistryMutex());
auto it = OCRHandleRegistry().find(h);
if (it == OCRHandleRegistry().end()) return false;
if (it == OCRHandleRegistry().end()) {
ANS_DBG("ANSOCR","Unregister: handle=%p NOT in registry (already gone)", (void*)h);
return false;
}
if (it->second.destructionStarted) {
ANS_DBG("ANSOCR","Unregister: handle=%p already being destroyed by another thread, returning false", (void*)h);
return false; // Another thread already owns the delete.
}
ANS_DBG("ANSOCR","Unregister: handle=%p starting (refcount before=%d)", (void*)h, it->second.refcount);
it->second.destructionStarted = true;
it->second.refcount--;
bool ok = OCRHandleRegistryCV().wait_for(lk, std::chrono::seconds(5), [&]() {
@@ -73,6 +95,7 @@ static bool UnregisterOCRHandle(ANSCENTER::ANSOCRBase* h) {
return it2 == OCRHandleRegistry().end() || it2->second.refcount <= 0;
});
if (!ok) {
ANS_DBG("ANSOCR","WARNING: Unregister timed out waiting for in-flight inference on handle=%p", (void*)h);
OutputDebugStringA("WARNING: UnregisterOCRHandle timed out waiting for in-flight inference\n");
}
OCRHandleRegistry().erase(h);
@@ -153,6 +176,10 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
int gpuId,
double detectorDBThreshold, double detectorDBBoxThreshold, double detectorDBUnclipRatio,
double classifierThreshold, int useDilation, int limitSideLen) {
ANS_DBG("ANSOCR","CreateEx called: HandlePtr=%p, *Handle(in)=%p, language=%d, engineMode=%d, gpuId=%d, limitSideLen=%d, modelPath=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), language, engineMode, gpuId, limitSideLen,
modelFilePath ? modelFilePath : "(null)");
if (Handle == nullptr) { ANS_DBG("ANSOCR","CreateEx FAIL: Handle is null"); return 0; }
try {
// Ensure all shared DLLs (OpenCV, OpenVINO, TRT, ORT) are pre-loaded
ANSCENTER::ANSLibsLoader::Initialize();
@@ -190,6 +217,7 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
if (isLegacyModel) {
(*Handle) = new ANSCENTER::ANSCPUOCR();
ANS_DBG("ANSOCR","CreateEx: legacy model detected, allocated ANSCPUOCR=%p", (void*)*Handle);
}
else {
// ANSRTOCR wraps PaddleOCRV5RTEngine which is strictly NVIDIA/CUDA:
@@ -224,8 +252,10 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
break;
}
}
if (*Handle == nullptr) return 0;
if (*Handle == nullptr) { ANS_DBG("ANSOCR","CreateEx FAIL: new returned null"); return 0; }
else {
ANS_DBG("ANSOCR","CreateEx: allocated handle=%p (uint=%llu), calling Initialize...",
(void*)*Handle, (unsigned long long)(uintptr_t)*Handle);
RegisterOCRHandle(*Handle);
ANSCENTER::OCRModelConfig modelConfig;
switch (language) {
@@ -287,6 +317,7 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
modelConfig.limitSideLen = limitSideLen;
int result = (*Handle)->Initialize(licenseKey, modelConfig, modelFilePath, modelFileZipPassword, engineMode);
if (!result) {
ANS_DBG("ANSOCR","CreateEx FAIL: Initialize returned false, tearing down handle=%p", (void*)*Handle);
// Initialize failed — tear down the engine we just registered so
// the caller, who sees a 0 return and (typically) does not call
// ReleaseANSOCRHandle, does not leak the engine + registry entry.
@@ -297,10 +328,12 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
*Handle = nullptr;
return 0;
}
ANS_DBG("ANSOCR","CreateEx OK: handle=%p (uint=%llu)", (void*)*Handle, (unsigned long long)(uintptr_t)*Handle);
return result;
}
}
catch (std::exception& e) {
ANS_DBG("ANSOCR","CreateEx EXCEPTION (std::exception): %s", e.what());
// Partially-constructed engine may already be registered — unwind it
// so the leak does not accumulate across repeated failed Create calls.
if (Handle && *Handle) {
@@ -313,6 +346,7 @@ extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle,
return 0;
}
catch (...) {
ANS_DBG("ANSOCR","CreateEx EXCEPTION (unknown)");
if (Handle && *Handle) {
if (UnregisterOCRHandle(*Handle)) {
try { (*Handle)->Destroy(); } catch (...) {}
@@ -332,6 +366,8 @@ extern "C" ANSOCR_API int CreateANSOCRHandle(ANSCENTER::ANSOCRBase** Handle, co
int gpuId,
double detectorDBThreshold, double detectorDBBoxThreshold, double detectorDBUnclipRatio,
double classifierThreshold, int useDilation) {
ANS_DBG("ANSOCR","CreateANSOCRHandle: HandlePtr=%p, language=%d, engineMode=%d, gpuId=%d (delegating to CreateEx, limitSideLen=960)",
(void*)Handle, language, engineMode, gpuId);
return CreateANSOCRHandleEx(Handle, licenseKey, modelFilePath, modelFileZipPassword,
language, engineMode, gpuId,
detectorDBThreshold, detectorDBBoxThreshold, detectorDBUnclipRatio,
@@ -352,6 +388,8 @@ static std::string SerializeOCRResults(ANSCENTER::ANSOCRBase* engine,
}
extern "C" ANSOCR_API std::string RunInference(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength) {
ANS_DBG("ANSOCR","RunInference: HandlePtr=%p, *Handle=%p, bufferLength=%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength);
if (!Handle || !*Handle) return "";
OCRHandleGuard guard(AcquireOCRHandle(*Handle));
if (!guard) return "";
@@ -369,6 +407,8 @@ extern "C" ANSOCR_API std::string RunInference(ANSCENTER::ANSOCRBase** Handle,
}
extern "C" ANSOCR_API std::string RunInferenceWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* cameraId) {
ANS_DBG("ANSOCR","RunInferenceWithCamID: HandlePtr=%p, *Handle=%p, bufferLength=%d, cameraId=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength, cameraId ? cameraId : "(null)");
if (!Handle || !*Handle) return "";
OCRHandleGuard guard(AcquireOCRHandle(*Handle));
if (!guard) return "";
@@ -386,6 +426,8 @@ extern "C" ANSOCR_API std::string RunInferenceWithCamID(ANSCENTER::ANSOCRBase**
}
extern "C" ANSOCR_API int RunInferenceCV(ANSCENTER::ANSOCRBase** Handle, const cv::Mat& image, std::string& ocrResult) {
ANS_DBG("ANSOCR","RunInferenceCV: HandlePtr=%p, *Handle=%p, image=%dx%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), image.cols, image.rows);
if (!Handle || !*Handle) return -1;
OCRHandleGuard guard(AcquireOCRHandle(*Handle));
if (!guard) return -3;
@@ -400,6 +442,8 @@ extern "C" ANSOCR_API int RunInferenceCV(ANSCENTER::ANSOCRBase** Handle, const c
}
extern "C" ANSOCR_API std::string RunInferenceBinary(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height) {
ANS_DBG("ANSOCR","RunInferenceBinary: HandlePtr=%p, *Handle=%p, %ux%u",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), width, height);
if (!Handle || !*Handle || !jpeg_bytes || width == 0 || height == 0) return "";
OCRHandleGuard guard(AcquireOCRHandle(*Handle));
if (!guard) return "";
@@ -417,17 +461,26 @@ extern "C" ANSOCR_API std::string RunInferenceBinary(ANSCENTER::ANSOCRBase** Ha
}
static int ReleaseANSOCRHandle_Impl(ANSCENTER::ANSOCRBase** Handle) {
try {
if (!Handle || !*Handle) return 0;
if (!UnregisterOCRHandle(*Handle)) {
if (!Handle || !*Handle) {
ANS_DBG("ANSOCR","Release: HandlePtr or *Handle is null, no-op");
return 0;
}
ANSCENTER::ANSOCRBase* h = *Handle;
ANS_DBG("ANSOCR","Release called: handle=%p (uint=%llu)", (void*)h, (unsigned long long)(uintptr_t)h);
if (!UnregisterOCRHandle(h)) {
ANS_DBG("ANSOCR","Release: Unregister returned false (already gone or being destroyed by another thread), handle=%p", (void*)h);
*Handle = nullptr;
return 0; // Not in registry — already freed
}
(*Handle)->Destroy();
delete *Handle;
*Handle = nullptr;
ANS_DBG("ANSOCR","Release OK: handle=%p deleted, registry now has %zu entries",
(void*)h, OCRHandleRegistry().size());
return 0;
}
catch (...) {
ANS_DBG("ANSOCR","Release EXCEPTION (unknown)");
if (Handle) *Handle = nullptr;
return 1;
}
@@ -438,6 +491,7 @@ extern "C" ANSOCR_API int ReleaseANSOCRHandle(ANSCENTER::ANSOCRBase** Handle) {
return ReleaseANSOCRHandle_Impl(Handle);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
ANS_DBG("ANSOCR","ReleaseANSOCRHandle: SEH exception caught");
return 1;
}
}
@@ -445,18 +499,24 @@ extern "C" ANSOCR_API int ReleaseANSOCRHandle(ANSCENTER::ANSOCRBase** Handle) {
// ── ALPR Configuration API ──────────────────────────────────────────
extern "C" ANSOCR_API int SetANSOCRMode(ANSCENTER::ANSOCRBase** Handle, int ocrMode) {
ANS_DBG("ANSOCR","SetANSOCRMode: HandlePtr=%p, *Handle=%p, ocrMode=%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), ocrMode);
if (!Handle || !*Handle) return -1;
(*Handle)->SetOCRMode(static_cast<ANSCENTER::OCRMode>(ocrMode));
return 0;
}
extern "C" ANSOCR_API int SetANSOCRCountry(ANSCENTER::ANSOCRBase** Handle, int country) {
ANS_DBG("ANSOCR","SetANSOCRCountry: HandlePtr=%p, *Handle=%p, country=%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), country);
if (!Handle || !*Handle) return -1;
(*Handle)->SetCountry(static_cast<ANSCENTER::Country>(country));
return 0;
}
extern "C" ANSOCR_API int SetANSOCRALPRFormat(ANSCENTER::ANSOCRBase** Handle, const char* formatJson) {
ANS_DBG("ANSOCR","SetANSOCRALPRFormat: HandlePtr=%p, *Handle=%p, formatJson=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), formatJson ? "(set)" : "(null)");
if (!Handle || !*Handle || !formatJson) return -1;
try {
nlohmann::json j = nlohmann::json::parse(formatJson);
@@ -504,6 +564,8 @@ extern "C" ANSOCR_API int SetANSOCRALPRFormat(ANSCENTER::ANSOCRBase** Handle, co
// - Raw UTF-8 encoded strings
// Pure ASCII input is passed through directly (no conversion overhead).
extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM) {
ANS_DBG("ANSOCR","ANSOCR_ConvertUTF8ToUTF16LE: utf8Str=%s, includeBOM=%d",
utf8Str ? "(set)" : "(null)", includeBOM);
try {
if (!utf8Str || !result) return -1;
int len = (int)strlen(utf8Str);
@@ -565,6 +627,8 @@ extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrH
}
extern "C" ANSOCR_API int ANSOCR_ConvertUTF16LEToUTF8(const unsigned char* utf16leBytes, int byteLen, LStrHandle result) {
ANS_DBG("ANSOCR","ANSOCR_ConvertUTF16LEToUTF8: utf16leBytes=%s, byteLen=%d",
utf16leBytes ? "(set)" : "(null)", byteLen);
try {
if (!utf16leBytes || byteLen <= 0 || !result) return -1;
// Check if input is already pure ASCII (no high bytes, or not valid UTF-16LE)
@@ -609,6 +673,8 @@ extern "C" ANSOCR_API int ANSOCR_ConvertUTF16LEToUTF8(const unsigned char* utf16
}
extern "C" ANSOCR_API std::string RunInferenceImagePath(ANSCENTER::ANSOCRBase** Handle, const char* imageFilePath) {
ANS_DBG("ANSOCR","RunInferenceImagePath: HandlePtr=%p, *Handle=%p, imageFilePath=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), imageFilePath ? imageFilePath : "(null)");
if (!Handle || !*Handle) return "";
OCRHandleGuard guard(AcquireOCRHandle(*Handle));
if (!guard) return "";
@@ -628,6 +694,8 @@ extern "C" ANSOCR_API std::string RunInferenceImagePath(ANSCENTER::ANSOCRBase**
extern "C" ANSOCR_API std::string RunInferenceInCroppedImages(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes) {
ANS_DBG("ANSOCR","RunInferenceInCroppedImages: HandlePtr=%p, *Handle=%p, bufferLength=%d, strBboxes=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength, strBboxes ? "(set)" : "(null)");
if (!Handle || !*Handle) return "";
OCRHandleGuard guard(AcquireOCRHandle(*Handle));
if (!guard) return "";
@@ -645,6 +713,9 @@ extern "C" ANSOCR_API std::string RunInferenceInCroppedImages(ANSCENTER::ANSOCR
catch (...) { return ""; }
}
extern "C" ANSOCR_API std::string RunInferenceInCroppedImagesWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, const char* cameraId) {
ANS_DBG("ANSOCR","RunInferenceInCroppedImagesWithCamID: HandlePtr=%p, *Handle=%p, bufferLength=%d, strBboxes=%s, cameraId=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength,
strBboxes ? "(set)" : "(null)", cameraId ? cameraId : "(null)");
if (!Handle || !*Handle) return "";
OCRHandleGuard guard(AcquireOCRHandle(*Handle));
if (!guard) return "";
@@ -663,6 +734,8 @@ extern "C" ANSOCR_API std::string RunInferenceInCroppedImagesWithCamID(ANSCENTE
}
//// For LabVIEW API
extern "C" ANSOCR_API int RunInference_LV(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInference_LV: HandlePtr=%p, *Handle=%p, bufferLength=%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength);
try {
std::string st = RunInference(Handle, jpeg_string, bufferLength);
if (st.empty()) return 0;
@@ -686,6 +759,8 @@ extern "C" ANSOCR_API int RunInference_LV(ANSCENTER::ANSOCRBase** Handl
}
extern "C" ANSOCR_API int RunInference_LVWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* cameraId, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInference_LVWithCamID: HandlePtr=%p, *Handle=%p, bufferLength=%d, cameraId=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength, cameraId ? cameraId : "(null)");
try {
std::string st = RunInferenceWithCamID(Handle, jpeg_string, bufferLength, cameraId);
if (st.empty()) return 0;
@@ -708,6 +783,8 @@ extern "C" ANSOCR_API int RunInference_LVWithCamID(ANSCENTER::ANSOCRBas
}
}
extern "C" ANSOCR_API int RunInferenceBinary_LV(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceBinary_LV: HandlePtr=%p, *Handle=%p, %ux%u",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), width, height);
try {
std::string st = RunInferenceBinary(Handle, jpeg_bytes, width, height);
if (st.empty()) return 0;
@@ -730,6 +807,8 @@ extern "C" ANSOCR_API int RunInferenceBinary_LV(ANSCENTER::ANSOCRBase**
}
}
extern "C" ANSOCR_API int RunInferenceImagePath_LV(ANSCENTER::ANSOCRBase** Handle, const char* imageFilePath, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceImagePath_LV: HandlePtr=%p, *Handle=%p, imageFilePath=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), imageFilePath ? imageFilePath : "(null)");
try {
std::string st = RunInferenceImagePath(Handle, imageFilePath);
if (st.empty()) return 0;
@@ -753,6 +832,8 @@ extern "C" ANSOCR_API int RunInferenceImagePath_LV(ANSCENTER::ANSOCRBase** H
}
extern "C" ANSOCR_API int ANSOCRUnitTest(const char* modelFilePath, const char* imageFilePath, LStrHandle detectionResult)
{
ANS_DBG("ANSOCR","ANSOCRUnitTest: modelFilePath=%s, imageFilePath=%s",
modelFilePath ? modelFilePath : "(null)", imageFilePath ? imageFilePath : "(null)");
try {
ANSCENTER::ANSOCRBase* infHandle;
std::string licenseKey = "";
@@ -782,6 +863,8 @@ extern "C" ANSOCR_API int ANSOCRUnitTest(const char* modelFilePath, const ch
}
extern "C" ANSOCR_API int RunInferenceInCroppedImages_LV(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceInCroppedImages_LV: HandlePtr=%p, *Handle=%p, bufferLength=%d, strBboxes=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength, strBboxes ? "(set)" : "(null)");
try {
std::string st = RunInferenceInCroppedImages(Handle, jpeg_string, bufferLength, strBboxes);
if (st.empty()) return 0;
@@ -804,6 +887,9 @@ extern "C" ANSOCR_API int RunInferenceInCroppedImages_LV(ANSCENTER::ANS
}
}
extern "C" ANSOCR_API int RunInferenceInCroppedImages_LVWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, const char* cameraId, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceInCroppedImages_LVWithCamID: HandlePtr=%p, *Handle=%p, bufferLength=%d, strBboxes=%s, cameraId=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), (int)bufferLength,
strBboxes ? "(set)" : "(null)", cameraId ? cameraId : "(null)");
try {
std::string st = RunInferenceInCroppedImagesWithCamID(Handle, jpeg_string, bufferLength, strBboxes, cameraId);
if (st.empty()) return 0;
@@ -835,6 +921,8 @@ extern "C" ANSOCR_API int RunInferenceComplete_LV(
LStrHandle detectionResult,
LStrHandle imageStr)
{
ANS_DBG("ANSOCR","RunInferenceComplete_LV: HandlePtr=%p, *Handle=%p, cameraId=%s, getJpegString=%d, jpegImageSize=%d",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), cameraId ? cameraId : "(null)", getJpegString, jpegImageSize);
if (!Handle || !*Handle) return -1;
if (!cvImage || !(*cvImage) || (*cvImage)->empty()) return -2;
@@ -926,6 +1014,8 @@ extern "C" ANSOCR_API int RunInferenceComplete_LV(
catch (...) { return 0; }
}
extern "C" ANSOCR_API int RunInferencesComplete_LV(ANSCENTER::ANSOCRBase** Handle, cv::Mat** cvImage, const char* cameraId, int maxImageSize, const char* strBboxes, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferencesComplete_LV: HandlePtr=%p, *Handle=%p, cameraId=%s, maxImageSize=%d, strBboxes=%s",
(void*)Handle, (void*)(Handle ? *Handle : nullptr), cameraId ? cameraId : "(null)", maxImageSize, strBboxes ? "(set)" : "(null)");
if (!Handle || !*Handle) return -1;
if (!cvImage || !(*cvImage) || (*cvImage)->empty()) return -2;
@@ -1009,6 +1099,8 @@ extern "C" ANSOCR_API int RunInferencesComplete_LV(ANSCENTER::ANSOCRBase** Handl
ANSCENTER::ANSOCRBase** Handle = &_v2Arr[0];
extern "C" ANSOCR_API int RunInference_LV_V2(uint64_t handleVal, unsigned char* jpeg_string, int32 bufferLength, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInference_LV_V2: handleVal=%llu (handle=%p), bufferLength=%d",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, (int)bufferLength);
try {
OCR_V2_HANDLE_SETUP(handleVal);
std::string st = RunInference(Handle, jpeg_string, bufferLength);
@@ -1026,6 +1118,8 @@ extern "C" ANSOCR_API int RunInference_LV_V2(uint64_t handleVal, unsigned char*
}
extern "C" ANSOCR_API int RunInference_LVWithCamID_V2(uint64_t handleVal, unsigned char* jpeg_string, int32 bufferLength, const char* cameraId, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInference_LVWithCamID_V2: handleVal=%llu (handle=%p), bufferLength=%d, cameraId=%s",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, (int)bufferLength, cameraId ? cameraId : "(null)");
try {
OCR_V2_HANDLE_SETUP(handleVal);
std::string st = RunInferenceWithCamID(Handle, jpeg_string, bufferLength, cameraId);
@@ -1043,6 +1137,8 @@ extern "C" ANSOCR_API int RunInference_LVWithCamID_V2(uint64_t handleVal, unsign
}
extern "C" ANSOCR_API int RunInferenceBinary_LV_V2(uint64_t handleVal, unsigned char* jpeg_bytes, unsigned int width, unsigned int height, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceBinary_LV_V2: handleVal=%llu (handle=%p), %ux%u",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, width, height);
try {
OCR_V2_HANDLE_SETUP(handleVal);
std::string st = RunInferenceBinary(Handle, jpeg_bytes, width, height);
@@ -1060,6 +1156,8 @@ extern "C" ANSOCR_API int RunInferenceBinary_LV_V2(uint64_t handleVal, unsigned
}
extern "C" ANSOCR_API int RunInferenceImagePath_LV_V2(uint64_t handleVal, const char* imageFilePath, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceImagePath_LV_V2: handleVal=%llu (handle=%p), imageFilePath=%s",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, imageFilePath ? imageFilePath : "(null)");
try {
OCR_V2_HANDLE_SETUP(handleVal);
std::string st = RunInferenceImagePath(Handle, imageFilePath);
@@ -1077,6 +1175,8 @@ extern "C" ANSOCR_API int RunInferenceImagePath_LV_V2(uint64_t handleVal, const
}
extern "C" ANSOCR_API int RunInferenceInCroppedImages_LV_V2(uint64_t handleVal, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceInCroppedImages_LV_V2: handleVal=%llu (handle=%p), bufferLength=%d, strBboxes=%s",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, (int)bufferLength, strBboxes ? "(set)" : "(null)");
try {
OCR_V2_HANDLE_SETUP(handleVal);
std::string st = RunInferenceInCroppedImages(Handle, jpeg_string, bufferLength, strBboxes);
@@ -1094,6 +1194,9 @@ extern "C" ANSOCR_API int RunInferenceInCroppedImages_LV_V2(uint64_t handleVal,
}
extern "C" ANSOCR_API int RunInferenceInCroppedImages_LVWithCamID_V2(uint64_t handleVal, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, const char* cameraId, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferenceInCroppedImages_LVWithCamID_V2: handleVal=%llu (handle=%p), bufferLength=%d, strBboxes=%s, cameraId=%s",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, (int)bufferLength,
strBboxes ? "(set)" : "(null)", cameraId ? cameraId : "(null)");
try {
OCR_V2_HANDLE_SETUP(handleVal);
std::string st = RunInferenceInCroppedImagesWithCamID(Handle, jpeg_string, bufferLength, strBboxes, cameraId);
@@ -1119,6 +1222,8 @@ extern "C" ANSOCR_API int RunInferenceComplete_LV_V2(
LStrHandle detectionResult,
LStrHandle imageStr)
{
ANS_DBG("ANSOCR","RunInferenceComplete_LV_V2: handleVal=%llu (handle=%p), cameraId=%s, getJpegString=%d, jpegImageSize=%d",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, cameraId ? cameraId : "(null)", getJpegString, jpegImageSize);
ANSCENTER::ANSOCRBase* directHandle = reinterpret_cast<ANSCENTER::ANSOCRBase*>(handleVal);
if (directHandle == nullptr) return -1;
if (!cvImage || !(*cvImage) || (*cvImage)->empty()) return -2;
@@ -1212,6 +1317,8 @@ extern "C" ANSOCR_API int RunInferenceComplete_LV_V2(
}
extern "C" ANSOCR_API int RunInferencesComplete_LV_V2(uint64_t handleVal, cv::Mat** cvImage, const char* cameraId, int maxImageSize, const char* strBboxes, LStrHandle detectionResult) {
ANS_DBG("ANSOCR","RunInferencesComplete_LV_V2: handleVal=%llu (handle=%p), cameraId=%s, maxImageSize=%d, strBboxes=%s",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal, cameraId ? cameraId : "(null)", maxImageSize, strBboxes ? "(set)" : "(null)");
ANSCENTER::ANSOCRBase* directHandle = reinterpret_cast<ANSCENTER::ANSOCRBase*>(handleVal);
if (directHandle == nullptr) return -1;
if (!cvImage || !(*cvImage) || (*cvImage)->empty()) return -2;
@@ -1290,6 +1397,8 @@ extern "C" ANSOCR_API uint64_t CreateANSOCRHandleEx_V2(const char* licenseKey,
int gpuId,
double detectorDBThreshold, double detectorDBBoxThreshold, double detectorDBUnclipRatio,
double classifierThreshold, int useDilation, int limitSideLen) {
ANS_DBG("ANSOCR","CreateANSOCRHandleEx_V2: language=%d, engineMode=%d, gpuId=%d, limitSideLen=%d, modelPath=%s",
language, engineMode, gpuId, limitSideLen, modelFilePath ? modelFilePath : "(null)");
try {
ANSCENTER::ANSOCRBase* handle = nullptr;
int result = CreateANSOCRHandleEx(&handle, licenseKey, modelFilePath, modelFileZipPassword,
@@ -1308,6 +1417,8 @@ extern "C" ANSOCR_API uint64_t CreateANSOCRHandle_V2(const char* licenseKey,
int gpuId,
double detectorDBThreshold, double detectorDBBoxThreshold, double detectorDBUnclipRatio,
double classifierThreshold, int useDilation) {
ANS_DBG("ANSOCR","CreateANSOCRHandle_V2: language=%d, engineMode=%d, gpuId=%d (delegating to CreateEx_V2, limitSideLen=960)",
language, engineMode, gpuId);
return CreateANSOCRHandleEx_V2(licenseKey, modelFilePath, modelFileZipPassword,
language, engineMode, gpuId,
detectorDBThreshold, detectorDBBoxThreshold, detectorDBUnclipRatio,
@@ -1315,17 +1426,26 @@ extern "C" ANSOCR_API uint64_t CreateANSOCRHandle_V2(const char* licenseKey,
}
extern "C" ANSOCR_API int ReleaseANSOCRHandle_V2(uint64_t handleVal) {
ANS_DBG("ANSOCR","ReleaseANSOCRHandle_V2: handleVal=%llu (handle=%p)",
(unsigned long long)handleVal, (void*)(uintptr_t)handleVal);
try {
ANSCENTER::ANSOCRBase* directHandle = reinterpret_cast<ANSCENTER::ANSOCRBase*>(handleVal);
if (directHandle == nullptr) return 0;
if (directHandle == nullptr) {
ANS_DBG("ANSOCR","Release_V2: handleVal is 0/null, no-op");
return 0;
}
if (!UnregisterOCRHandle(directHandle)) {
ANS_DBG("ANSOCR","Release_V2: Unregister returned false (already gone or being destroyed by another thread), handle=%p", (void*)directHandle);
return 0; // Not in registry — already freed
}
directHandle->Destroy();
delete directHandle;
ANS_DBG("ANSOCR","Release_V2 OK: handle=%p deleted, registry now has %zu entries",
(void*)directHandle, OCRHandleRegistry().size());
return 0;
}
catch (...) {
ANS_DBG("ANSOCR","Release_V2 EXCEPTION (unknown)");
return 1;
}
}