Initial OCR to support ALPR mode with country support

This commit is contained in:
2026-03-29 22:51:39 +11:00
parent 6861de8fb4
commit b735931c55
13 changed files with 911 additions and 27 deletions

View File

@@ -9,7 +9,65 @@
#include <vector>
#include "LabVIEWHeader/extcode.h"
#include "ANSLicense.h"
#include <map>
#include <regex>
namespace ANSCENTER {
// ── ALPR Enums ──────────────────────────────────────────────────────
enum OCRMode {
OCR_GENERAL = 0,
OCR_ALPR = 1
};
enum ALPRCountry {
ALPR_JAPAN = 0,
ALPR_VIETNAM = 1,
ALPR_CHINA = 2,
ALPR_USA = 3,
ALPR_AUSTRALIA = 4,
ALPR_CUSTOM = 99
};
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<std::string, std::string> corrections;
};
struct ALPRPlateFormat {
std::string name;
ALPRCountry country = ALPR_JAPAN;
int numRows = 2;
std::vector<ALPRZone> zones;
float rowSplitThreshold = 0.3f;
};
struct ALPRResult {
bool valid = false;
std::string formatName;
std::string fullPlateText;
std::map<std::string, std::string> parts;
float confidence = 0.0f;
cv::Rect plateBox;
};
struct OCRModelConfig {
bool userGPU = true;
bool useTensorRT = false;
@@ -91,6 +149,12 @@ namespace ANSCENTER {
OCRModelConfig _modelConfig;
int _engineMode; //0: Auto detect, 1 GPU, 2 CPU
SPDLogger& _logger = SPDLogger::GetInstance("OCR", false);
// ALPR settings
OCRMode _ocrMode = OCR_GENERAL;
ALPRCountry _alprCountry = ALPR_JAPAN;
std::vector<ALPRPlateFormat> _alprFormats;
void CheckLicense();
[[nodiscard]] bool Init(const std::string& licenseKey, OCRModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, int engineMode);
public:
@@ -100,6 +164,21 @@ namespace ANSCENTER {
[[nodiscard]] virtual std::vector<ANSCENTER::OCRObject> RunInference(const cv::Mat& input, const std::vector<cv::Rect>& Bbox) = 0;
[[nodiscard]] virtual std::vector<ANSCENTER::OCRObject> RunInference(const cv::Mat& input, const std::vector<cv::Rect>& 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<std::string, float> RecognizeText(const cv::Mat& croppedImage) { return {"", 0.0f}; }
// ALPR configuration methods
void SetOCRMode(OCRMode mode);
OCRMode GetOCRMode() const;
void SetALPRCountry(ALPRCountry country);
ALPRCountry GetALPRCountry() const;
void SetALPRFormat(const ALPRPlateFormat& format);
void AddALPRFormat(const ALPRPlateFormat& format);
void ClearALPRFormats();
void LoadDefaultFormats(ALPRCountry country);
const std::vector<ALPRPlateFormat>& GetALPRFormats() const;
~ANSOCRBase() {
try {
@@ -119,6 +198,19 @@ namespace ANSCENTER {
[[nodiscard]] static std::string PolygonToString(const std::vector<cv::Point2f>& polygon);
[[nodiscard]] static std::vector<cv::Point2f> RectToNormalizedPolygon(const cv::Rect& rect, float imageWidth, float imageHeight);
[[nodiscard]] static std::string KeypointsToString(const std::vector<float>& kps);
// ALPR post-processing
[[nodiscard]] static std::vector<ALPRResult> ALPRPostProcessing(
const std::vector<OCRObject>& ocrResults,
const std::vector<ALPRPlateFormat>& formats,
int imageWidth, int imageHeight,
ANSOCRBase* engine = nullptr,
const cv::Mat& originalImage = cv::Mat());
[[nodiscard]] static std::string ALPRResultToJsonString(const std::vector<ALPRResult>& 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:
};
}
@@ -155,6 +247,11 @@ extern "C" ANSOCR_API int RunInferenceInCroppedImages_LVWithCamID(ANSCENTER:
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 SetANSOCRALPRCountry(ANSCENTER::ANSOCRBase** Handle, int country);
extern "C" ANSOCR_API int SetANSOCRALPRFormat(ANSCENTER::ANSOCRBase** Handle, const char* formatJson);
// 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,