#ifndef ANSOCRBASE_H #define ANSOCRBASE_H #define ANSOCR_API __declspec(dllexport) #pragma once #include "opencv2/core.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/imgproc.hpp" #include #include #include "LabVIEWHeader/extcode.h" #include "ANSLicense.h" #include #include namespace ANSCENTER { // ── ALPR Enums ────────────────────────────────────────────────────── enum OCRMode { OCR_GENERAL = 0, OCR_ALPR = 1 }; enum ALPRCharClass { CHAR_DIGIT = 0, CHAR_LATIN_ALPHA = 1, CHAR_ALPHANUMERIC = 2, CHAR_HIRAGANA = 3, CHAR_KATAKANA = 4, CHAR_KANJI = 5, CHAR_CJK_ANY = 6, CHAR_ANY = 7 }; // ── ALPR Structs ──────────────────────────────────────────────────── struct ALPRZone { std::string name; int row = 0; int col = 0; ALPRCharClass charClass = CHAR_ANY; int minLength = 1; int maxLength = 10; std::string validationRegex; std::map corrections; }; struct ALPRPlateFormat { std::string name; Country country = JAPAN; int numRows = 2; std::vector zones; float rowSplitThreshold = 0.3f; }; struct ALPRResult { bool valid = false; std::string formatName; std::string fullPlateText; std::map parts; float confidence = 0.0f; cv::Rect plateBox; }; struct OCRModelConfig { bool userGPU = true; bool useTensorRT = false; int gpuId = 0; int gpuMemory = 4000; int cpuThreads = 10; bool enableMKLDNN = false; bool ensureASCII = true; OCRLanguage ocrLanguage; std::string precisionType; std::string ocrType; std::string limitType; bool useDetector = true; bool useRecognizer = true; bool useCLS = true; bool useTable = false; bool useLayout = false; std::string detectionModelDir; std::string detectionModelFile; std::string detectionModelParam; std::string detectionScoreMode; std::string recognizerModelDir; std::string recognizerModelFile; std::string recognizerModelParam; std::string recogizerCharDictionaryPath; std::string clsModelDir; std::string clsModelFile; std::string clsModelParam; std::string layoutModelDir; std::string layourDictionaryPath; std::string tableModelDir; std::string tableCharDictionaryPath; int limitSideLen = 960; double detectionDBThreshold = 0.3; double detectionBoxThreshold = 0.6; double detectionDBUnclipRatio = 1.5; bool useDilation = false; bool useAngleCLS = false; double clsThreshold = 0.9; int clsBatchNumber = 1; int recognizerBatchNum = 6; int recoginzerImageHeight = 48; int recoginzerImageWidth = 320; double layoutScoreThreshold = 0.5; double layoutNMSThreshold = 0.5; int tableModelMaxLengh = 488; int tableBatchNum = 1; bool mergeNoSpanStructure = true; }; struct OCRObject { int classId{ 0 }; int trackId{ 0 }; std::string className{}; float confidence{ 0.0 }; cv::Rect box{}; std::vector polygon; // polygon that contain x1 ,y1,x2,y2,x3,y3,x4,y4 std::vector kps{}; // Containing keypoints cv::Mat mask{}; // jpeg string of the mask std::string extraInfo; // More information such as facial recognition std::string cameraId; //std::string attributes; // Attributes information in JSON string }; class ANSOCR_API ANSOCRBase { protected: bool _licenseValid{ false }; bool _isInitialized{ false }; std::string _licenseKey; std::string _modelFolder; std::string _modelConfigFile; OCRModelConfig _modelConfig; int _engineMode; //0: Auto detect, 1 GPU, 2 CPU SPDLogger& _logger = SPDLogger::GetInstance("OCR", false); // ALPR settings OCRMode _ocrMode = OCR_GENERAL; Country _alprCountry = JAPAN; std::vector _alprFormats; void CheckLicense(); [[nodiscard]] bool Init(const std::string& licenseKey, OCRModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, int engineMode); public: [[nodiscard]] virtual bool Initialize(const std::string& licenseKey, OCRModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, int engineMode); [[nodiscard]] virtual std::vector RunInference(const cv::Mat& input) = 0; [[nodiscard]] virtual std::vector RunInference(const cv::Mat& input, const std::string& cameraId) = 0; [[nodiscard]] virtual std::vector RunInference(const cv::Mat& input, const std::vector& Bbox) = 0; [[nodiscard]] virtual std::vector RunInference(const cv::Mat& input, const std::vector& Bbox, const std::string &cameraId) = 0; // Run recognizer only on a pre-cropped text image (skips detection) // Returns recognized text and confidence. Default returns empty. virtual std::pair RecognizeText(const cv::Mat& croppedImage) { return {"", 0.0f}; } // ALPR configuration methods void SetOCRMode(OCRMode mode); OCRMode GetOCRMode() const; void SetCountry(Country country); Country GetCountry() const; void SetALPRFormat(const ALPRPlateFormat& format); void AddALPRFormat(const ALPRPlateFormat& format); void ClearALPRFormats(); void LoadDefaultFormats(Country country); const std::vector& GetALPRFormats() const; ~ANSOCRBase() { try { } catch (std::exception& e) { std::cout << "ANSOCRBase::Destroy()" << e.what(); } }; [[nodiscard]] virtual bool Destroy() = 0; }; class ANSOCRUtility { public: [[nodiscard]] static std::string OCRDetectionToJsonString(const std::vector& dets); [[nodiscard]] static std::vector GetBoundingBoxes(const std::string& strBBoxes); [[nodiscard]] static std::string PolygonToString(const std::vector& polygon); [[nodiscard]] static std::vector RectToNormalizedPolygon(const cv::Rect& rect, float imageWidth, float imageHeight); [[nodiscard]] static std::string KeypointsToString(const std::vector& kps); // ALPR post-processing [[nodiscard]] static std::vector ALPRPostProcessing( const std::vector& ocrResults, const std::vector& formats, int imageWidth, int imageHeight, ANSOCRBase* engine = nullptr, const cv::Mat& originalImage = cv::Mat()); [[nodiscard]] static std::string ALPRResultToJsonString(const std::vector& results); // UTF-8 character classification helpers static uint32_t NextUTF8Codepoint(const std::string& str, size_t& pos); static bool IsCharClass(uint32_t codepoint, ALPRCharClass charClass); private: }; } // Original signature — backward compatible with third-party apps built against older DLL extern "C" ANSOCR_API int CreateANSOCRHandle(ANSCENTER::ANSOCRBase** Handle, const char* licenseKey, const char* modelFilePath, const char* modelFileZipPassword, int language, int engineMode, int gpuId = 0, double detectorDBThreshold = 0.3, double detectorDBBoxThreshold = 0.6, double detectorDBUnclipRatio = 1.5, double classifierThreshold = 0.9, int useDilation = 0); // Extended version with limitSideLen parameter — new callers should use this extern "C" ANSOCR_API int CreateANSOCRHandleEx(ANSCENTER::ANSOCRBase** Handle, const char* licenseKey, const char* modelFilePath, const char* modelFileZipPassword, int language, int engineMode, int gpuId = 0, double detectorDBThreshold = 0.3, double detectorDBBoxThreshold = 0.6, double detectorDBUnclipRatio = 1.5, double classifierThreshold = 0.9, int useDilation = 0, int limitSideLen = 960); extern "C" ANSOCR_API std::string RunInference(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength); extern "C" ANSOCR_API std::string RunInferenceWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* cameraId); extern "C" ANSOCR_API int RunInferenceCV(ANSCENTER::ANSOCRBase** Handle, const cv::Mat &image, std::string &ocrResult); extern "C" ANSOCR_API std::string RunInferenceBinary(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height); extern "C" ANSOCR_API int ReleaseANSOCRHandle(ANSCENTER::ANSOCRBase** Handle); extern "C" ANSOCR_API std::string RunInferenceImagePath(ANSCENTER::ANSOCRBase** Handle, const char* imageFilePath); extern "C" ANSOCR_API std::string RunInferenceInCroppedImages(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes); extern "C" ANSOCR_API std::string RunInferenceInCroppedImagesWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, const char* cameraId); //// For LabVIEW API extern "C" ANSOCR_API int RunInference_LV(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, LStrHandle detectionResult); extern "C" ANSOCR_API int RunInference_LVWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* cameraId,LStrHandle detectionResult); extern "C" ANSOCR_API int RunInferenceBinary_LV(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height, LStrHandle detectionResult); extern "C" ANSOCR_API int RunInferenceImagePath_LV(ANSCENTER::ANSOCRBase** Handle, const char* imageFilePath, LStrHandle detectionResult); extern "C" ANSOCR_API int ANSOCRUnitTest(const char* modelFilePath, const char* imageFilePath, LStrHandle detectionResult); extern "C" ANSOCR_API int RunInferenceInCroppedImages_LV(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, LStrHandle detectionResult); extern "C" ANSOCR_API int RunInferenceInCroppedImages_LVWithCamID(ANSCENTER::ANSOCRBase** Handle, unsigned char* jpeg_string, int32 bufferLength, const char* strBboxes, const char* cameraId, LStrHandle detectionResult); extern "C" ANSOCR_API int RunInferenceComplete_LV(ANSCENTER::ANSOCRBase** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize, LStrHandle detectionResult, LStrHandle imageStr); extern "C" ANSOCR_API int RunInferencesComplete_LV(ANSCENTER::ANSOCRBase** Handle, cv::Mat** cvImage, const char* cameraId, int maxImageSize, const char* strBboxes, LStrHandle detectionResult); // ALPR configuration API extern "C" ANSOCR_API int SetANSOCRMode(ANSCENTER::ANSOCRBase** Handle, int ocrMode); extern "C" ANSOCR_API int SetANSOCRCountry(ANSCENTER::ANSOCRBase** Handle, int country); extern "C" ANSOCR_API int SetANSOCRALPRFormat(ANSCENTER::ANSOCRBase** Handle, const char* formatJson); // Unicode conversion utilities for LabVIEW wrapper classes extern "C" ANSOCR_API int ANSOCR_ConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result); extern "C" ANSOCR_API int ANSOCR_ConvertUTF16LEToUTF8(const unsigned char* utf16leBytes, int byteLen, LStrHandle result); // V2 Create / Release — handle as uint64_t by value (no pointer-to-pointer) extern "C" ANSOCR_API uint64_t CreateANSOCRHandleEx_V2(const char* licenseKey, const char* modelFilePath, const char* modelFileZipPassword, int language, int engineMode, int gpuId, double detectorDBThreshold, double detectorDBBoxThreshold, double detectorDBUnclipRatio, double classifierThreshold, int useDilation, int limitSideLen); extern "C" ANSOCR_API uint64_t CreateANSOCRHandle_V2(const char* licenseKey, const char* modelFilePath, const char* modelFileZipPassword, int language, int engineMode, int gpuId, double detectorDBThreshold, double detectorDBBoxThreshold, double detectorDBUnclipRatio, double classifierThreshold, int useDilation); extern "C" ANSOCR_API int ReleaseANSOCRHandle_V2(uint64_t handleVal); #endif