using DeepLearning_Model_Sharp; using OpenCvSharp; namespace Inno.LPR { public class LicensePlateMotionDetector : IDisposable { private readonly DeepLearning_Wrapper _wrapperDetection; private readonly DeepLearning_Wrapper _wrapperRecognition; public string ModelVersion { get; private set; } public LicensePlateMotionDetector() { try { _wrapperDetection = new DeepLearning_Wrapper(DeepLearning_Wrapper.DLL_DETECTION); _wrapperRecognition = new DeepLearning_Wrapper(DeepLearning_Wrapper.DLL_OCR); var constructResult1 = _wrapperDetection.Model_Wrapper_Construct(); if (constructResult1 != 1) { throw new InvalidOperationException("Detection Construct Fail"); } var constructResult2 = _wrapperRecognition.Model_Wrapper_Construct(); if (constructResult2 != 1) { throw new InvalidOperationException("Recognition Construct Fail"); } string modelVersion = string.Empty; var getVersionResult = _wrapperDetection.Model_Wrapper_Getversion(ref modelVersion); if (getVersionResult != 1) { throw new InvalidOperationException("Getversion Fail"); } ModelVersion = modelVersion; } catch (Exception) { throw; } } public List DetectLicensePlateExistence(Mat image, Rect roi) { List vLPR_Boxes = []; _ = _wrapperDetection.Model_Wrapper_LPR_Plate(image, roi, ref vLPR_Boxes); return vLPR_Boxes; } public LicensePlateRecognitionMotionData DetectLicensePlateExistence(MotionImageData data) { using var originalImage = data.Image; Rect rectangleROI = data.ROI; List vLPR_Boxes = []; var isLicensePlateExist = _wrapperDetection.Model_Wrapper_LPR_Plate(originalImage, rectangleROI, ref vLPR_Boxes); bool value = isLicensePlateExist == 1 && vLPR_Boxes.Exists(x => x.w * x.h > data.MinimumArea && x.w * x.h < data.MaximumArea); var motionData = new LicensePlateRecognitionMotionData(originalImage, data.CreatedTime, vLPR_Boxes, value); return motionData; } public LPR_RESULT_RST DetectLicensePlateCode(Mat image, List lpList) { var mLPR_Boxes = lpList.OrderByDescending(box => box.y).First(); Rect rect_LP = new((int)mLPR_Boxes.x, (int)mLPR_Boxes.y, (int)mLPR_Boxes.w, (int)mLPR_Boxes.h); LPR_RESULT_RST mLPR_RESULT_RST = new(); _ = _wrapperRecognition.Model_Wrapper_LPR_Code(image, rect_LP, ref mLPR_RESULT_RST); return mLPR_RESULT_RST; } public LPR_RESULT_RST DetectLicensePlateCode(Mat image, LPR_Boxes mLPR_Boxes) { Rect rect_LP = new((int)mLPR_Boxes.x, (int)mLPR_Boxes.y, (int)mLPR_Boxes.w, (int)mLPR_Boxes.h); LPR_RESULT_RST mLPR_RESULT_RST = new(); _ = _wrapperRecognition.Model_Wrapper_LPR_Code(image, rect_LP, ref mLPR_RESULT_RST); return mLPR_RESULT_RST; } public void DetectLicensePlateCode(LicensePlateRecognitionMotionData motionData) { if (!motionData.IsLicensePlateExist) { return; } var mLPR_Boxes = motionData.SelectedTrustedLprBox; Rect rect_LP = new((int)mLPR_Boxes.x, (int)mLPR_Boxes.y, (int)mLPR_Boxes.w, (int)mLPR_Boxes.h); LPR_RESULT_RST mLPR_RESULT_RST = new(); var isExistCode = _wrapperRecognition.Model_Wrapper_LPR_Code(motionData.OriginalImage, rect_LP, ref mLPR_RESULT_RST); if (isExistCode != 1) { return; } //motionData.DrawLicensePlateCharacterBoxes(mLPR_RESULT_RST.vLPR_Boxes, mLPR_Boxes.x, mLPR_Boxes.y); motionData.AddLicensePlateCode(mLPR_RESULT_RST.mRST_Code); } public void Dispose() { _wrapperDetection?.Dispose(); _wrapperRecognition?.Dispose(); GC.SuppressFinalize(this); } } }