초기 커밋.

This commit is contained in:
2026-02-03 11:33:58 +09:00
parent 90b3415a1f
commit b6b823b1c4
71 changed files with 4794 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
namespace DeepLearning_Model_Sharp
{
public enum ENUM_MODEL_TYPE
{
// CPU
_TYPE_C_SSD = 0,
_TYPE_C_YLv4,
_TYPE_C_YLv5,
_TYPE_C_YLvX, // v5, v8
// DARKNET
_TYPE_D_TINY,
_TYPE_D_NORM,
// GPU
_TYPE_G_NORM,
_TYPE_G_CUDA,
_TYPE_G_VINO,
_TYPE_G_TRT,
_TYPE_G_DML,
_TYPE_G_DNNL,
// TRT
_TYPE_T_TRT,
}
public enum ENUM_MODEL_DLL
{
_MODEL_DLL_CPU = 0, // CPU 21.4.2
_MODEL_DLL_CPU2, // CPU 22.3.0 XP
_MODEL_DLL_CPU3, // CPU 23.0.0
_MODEL_DLL_GPU, // DARKNET
_MODEL_DLL_GPU2, // GPU
_MODEL_DLL_GPU3, // TRT
}
public enum ENUM_DLL_RESULT
{
_DLL_RST_OK = 1,
_DLL_RST_ERROR = 0,
_DLL_RST_ERROR_KEY = -1,
_DLL_RST_ERROR_MODEL = -2,
}
}

View File

@@ -0,0 +1,372 @@
using System.Runtime.InteropServices;
using DeepLearningSharp;
using OpenCvSharp;
namespace DeepLearning_Model_Sharp
{
public struct BBOX_T
{
public uint x, y, w, h;
public float prob;
public uint obj_id;
}
public struct LPR_Boxes
{
public uint x, y, w, h;
public float prob;
public uint obj_id;
}
public struct LPR_RESULT_PTR
{
public IntPtr vLPR_Boxes_Ptr;
public int nLPR_Boxes;
public int nRST_LP;
public string mRST_Code;
}
public struct LPR_RESULT_RST
{
public List<LPR_Boxes> vLPR_Boxes;
public LPR_Boxes mRect_LP;
public int nRST_LP;
public string mRST_Code;
}
public class DeepLearning_Wrapper : IDisposable
{
public const string DLL_DETECTION = "DeepLearningLPR26.dll";
public const string DLL_OCR = "DeepLearningLPR.dll";
private IntPtr _pModel = IntPtr.Zero;
private readonly int _nModel_DLL = (int)ENUM_MODEL_DLL._MODEL_DLL_CPU3;
private readonly int _nModel_Type = (int)ENUM_MODEL_TYPE._TYPE_C_YLvX;
private readonly int _nMulti = 1;
private readonly int _nDecryption = 1;
private readonly int _GPUID = 0;
private int _nDLL_RST = (int)ENUM_DLL_RESULT._DLL_RST_ERROR;
private readonly string _cf = "";
private readonly string _wf;
private readonly DeepLearning_Model_Wrapper mModel_Wrapper;
public DeepLearning_Wrapper(int nModel_DLL, string cf, string wf, int nModel_Type, int nMulti, int nDecryption)
{
_nModel_DLL = nModel_DLL;
_nModel_Type = nModel_Type;
_nMulti = nMulti;
_nDecryption = nDecryption;
_cf = cf;
_wf = wf;
mModel_Wrapper = new DeepLearning_Model_Wrapper();
}
public DeepLearning_Wrapper()
{
_wf = DLL_DETECTION;
mModel_Wrapper = new DeepLearning_Model_Wrapper();
}
public DeepLearning_Wrapper(string wf)
{
_wf = wf;
mModel_Wrapper = new DeepLearning_Model_Wrapper();
}
public int Model_Wrapper_Construct()
{
try
{
_nDLL_RST = mModel_Wrapper._Model_Construct(_nModel_DLL, ref _pModel, _cf, _wf, _nModel_Type, _nDecryption, _GPUID);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
return _nDLL_RST;
}
public int Model_Wrapper_Destruct()
{
try
{
_nDLL_RST = mModel_Wrapper._Model_Destruct(_nModel_DLL, ref _pModel);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
return _nDLL_RST;
}
public int Model_Wrapper_Getversion(ref string strVersion)
{
try
{
_nDLL_RST = mModel_Wrapper._Model_Getversion(_nModel_DLL, _pModel, ref strVersion);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
return _nDLL_RST;
}
public int Model_Wrapper_Detect(Mat mat_SRC, ref List<BBOX_T> vBBOX_T)
{
IntPtr matPtr = mat_SRC.CvPtr;
try
{
IntPtr vBBOX_TPtr = IntPtr.Zero;
int nBBOX_T = 0;
_nDLL_RST = mModel_Wrapper._Model_Detect(_nModel_DLL, _pModel, matPtr, ref vBBOX_TPtr, ref nBBOX_T, _nModel_Type);
if (vBBOX_TPtr != IntPtr.Zero)
{
// Calculate the size of each struct element
int structSize = Marshal.SizeOf(typeof(BBOX_T));
// Iterate over the results and convert them to _LPR_Boxes structs
for (int i = 0; i < nBBOX_T; i++)
{
IntPtr structPtr = IntPtr.Add(vBBOX_TPtr, i * structSize);
BBOX_T resultItem = Marshal.PtrToStructure<BBOX_T>(structPtr);
vBBOX_T.Add(resultItem);
}
// Clean up the allocated memory
mModel_Wrapper._Model_FreeHGlobal(vBBOX_TPtr);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
finally
{
;
}
return _nDLL_RST;
}
public int Model_Wrapper_LPR_Plate(Mat mat_LPR, Rect mRect_ROI, ref List<LPR_Boxes> vLPR_Boxes)
{
IntPtr matPtr = mat_LPR.CvPtr;
IntPtr rectPtr = IntPtr.Zero;
try
{
rectPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Rect)));
Marshal.StructureToPtr(mRect_ROI, rectPtr, false);
IntPtr vLPR_BoxesPtr = IntPtr.Zero;
int nLPR_Boxes = 0;
_nDLL_RST = mModel_Wrapper._Model_LPR_Plate(_nModel_DLL, _pModel, matPtr, rectPtr, ref vLPR_BoxesPtr, ref nLPR_Boxes, _nMulti, _nModel_Type);
if (vLPR_BoxesPtr != IntPtr.Zero)
{
// Calculate the size of each struct element
int structSize = Marshal.SizeOf(typeof(LPR_Boxes));
// Iterate over the results and convert them to _LPR_Boxes structs
for (int i = 0; i < nLPR_Boxes; i++)
{
IntPtr structPtr = IntPtr.Add(vLPR_BoxesPtr, i * structSize);
LPR_Boxes resultItem = Marshal.PtrToStructure<LPR_Boxes>(structPtr);
vLPR_Boxes.Add(resultItem);
}
// Clean up the allocated memory
mModel_Wrapper._Model_FreeHGlobal(vLPR_BoxesPtr);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
finally
{
if (rectPtr != IntPtr.Zero) Marshal.FreeHGlobal(rectPtr);
}
return _nDLL_RST;
}
public int Model_Wrapper_LPR_Code(Mat mat_LPR, Rect mRect_LP, ref LPR_RESULT_RST mLPR_RESULT_RST)
{
IntPtr matPtr = mat_LPR.CvPtr;
IntPtr rectPtr = IntPtr.Zero;
try
{
rectPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Rect)));
Marshal.StructureToPtr(mRect_LP, rectPtr, false);
IntPtr LPR_ResultPtr = IntPtr.Zero;
int nLPR_Result = 0;
_nDLL_RST = mModel_Wrapper._Model_LPR_Code(_nModel_DLL, _pModel, matPtr, rectPtr, ref LPR_ResultPtr, ref nLPR_Result, _nModel_Type);
if (LPR_ResultPtr != IntPtr.Zero)
{
// Calculate the size of each struct element
int structSize = Marshal.SizeOf(typeof(LPR_RESULT_PTR));
List<LPR_RESULT_PTR> vLPR_RESULT = [];
// Iterate over the results and convert them to _LPR_Boxes struct
for (int i = 0; i < nLPR_Result; i++)
{
IntPtr structPtr = IntPtr.Add(LPR_ResultPtr, i * structSize);
LPR_RESULT_PTR resultItem = Marshal.PtrToStructure<LPR_RESULT_PTR>(structPtr);
vLPR_RESULT.Add(resultItem);
}
for (int k = 0; k < vLPR_RESULT.Count; k++)
{
IntPtr vLPR_BoxesPtr = vLPR_RESULT[k].vLPR_Boxes_Ptr;
int nLPR_Boxes = vLPR_RESULT[k].nLPR_Boxes;
if (vLPR_BoxesPtr != IntPtr.Zero)
{
// Calculate the size of each struct element
int structSize_Boxes = Marshal.SizeOf(typeof(LPR_Boxes));
mLPR_RESULT_RST.vLPR_Boxes = [];
// Iterate over the results and convert them to _LPR_Boxes struct
for (int i = 0; i < nLPR_Boxes; i++)
{
IntPtr structPtr = IntPtr.Add(vLPR_BoxesPtr, i * structSize);
LPR_Boxes resultItem = Marshal.PtrToStructure<LPR_Boxes>(structPtr);
if (i == 0) mLPR_RESULT_RST.mRect_LP = resultItem;
else mLPR_RESULT_RST.vLPR_Boxes.Add(resultItem);
}
mLPR_RESULT_RST.nRST_LP = vLPR_RESULT[k].nRST_LP;
mLPR_RESULT_RST.mRST_Code = vLPR_RESULT[k].mRST_Code;
// Clean up the allocated memory
mModel_Wrapper._Model_FreeHGlobal(vLPR_BoxesPtr);
}
}
// Clean up the allocated memory
mModel_Wrapper._Model_FreeHGlobal(LPR_ResultPtr);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
finally
{
if (rectPtr != IntPtr.Zero) Marshal.FreeHGlobal(rectPtr);
}
return _nDLL_RST;
}
public int Model_Wrapper_LPR_RST(Mat mat_LPR, Rect mRect_ROI, ref List<LPR_RESULT_RST> vLPR_RESULT_RST)
{
IntPtr matPtr = mat_LPR.CvPtr;
IntPtr rectPtr = IntPtr.Zero;
try
{
rectPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Rect)));
Marshal.StructureToPtr(mRect_ROI, rectPtr, false);
IntPtr LPR_ResultPtr = IntPtr.Zero;
int nLPR_Result = 0;
_nDLL_RST = mModel_Wrapper._Model_LPR_RST(_nModel_DLL, _pModel, matPtr, rectPtr, ref LPR_ResultPtr, ref nLPR_Result, _nMulti, _nModel_Type);
if (LPR_ResultPtr != IntPtr.Zero)
{
// Calculate the size of each struct element
int structSize = Marshal.SizeOf(typeof(LPR_RESULT_PTR));
List<LPR_RESULT_PTR> vLPR_RESULT = [];
// Iterate over the results and convert them to _LPR_Boxes struct
for (int i = 0; i < nLPR_Result; i++)
{
IntPtr structPtr = IntPtr.Add(LPR_ResultPtr, i * structSize);
LPR_RESULT_PTR resultItem = Marshal.PtrToStructure<LPR_RESULT_PTR>(structPtr);
vLPR_RESULT.Add(resultItem);
}
LPR_RESULT_RST mLPR_RESULT_RST = new();
for (int k = 0; k < vLPR_RESULT.Count; k++)
{
IntPtr vLPR_BoxesPtr = vLPR_RESULT[k].vLPR_Boxes_Ptr;
int nLPR_Boxes = vLPR_RESULT[k].nLPR_Boxes;
if (vLPR_BoxesPtr != IntPtr.Zero)
{
// Calculate the size of each struct element
int structSize_Boxes = Marshal.SizeOf(typeof(LPR_Boxes));
mLPR_RESULT_RST.vLPR_Boxes = [];
// Iterate over the results and convert them to _LPR_Boxes struct
for (int i = 0; i < nLPR_Boxes; i++)
{
IntPtr structPtr = IntPtr.Add(vLPR_BoxesPtr, i * structSize);
LPR_Boxes resultItem = Marshal.PtrToStructure<LPR_Boxes>(structPtr);
if (i == 0) mLPR_RESULT_RST.mRect_LP = resultItem;
else mLPR_RESULT_RST.vLPR_Boxes.Add(resultItem);
}
mLPR_RESULT_RST.nRST_LP = vLPR_RESULT[k].nRST_LP;
mLPR_RESULT_RST.mRST_Code = vLPR_RESULT[k].mRST_Code;
vLPR_RESULT_RST.Add(mLPR_RESULT_RST);
// Clean up the allocated memory
mModel_Wrapper._Model_FreeHGlobal(vLPR_BoxesPtr);
}
}
// Clean up the allocated memory
mModel_Wrapper._Model_FreeHGlobal(LPR_ResultPtr);
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
finally
{
if (rectPtr != IntPtr.Zero) Marshal.FreeHGlobal(rectPtr);
}
return _nDLL_RST;
}
public void Dispose()
{
try
{
_nDLL_RST = Model_Wrapper_Destruct();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
GC.SuppressFinalize(this);
}
}
}

56
Inno.LPR/Inno.LPR.csproj Normal file
View File

@@ -0,0 +1,56 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenCvSharp4" Version="4.9.0.20240103" />
</ItemGroup>
<ItemGroup>
<Reference Include="DeepLearning_Sharp">
<HintPath>Libraries\DeepLearning_Sharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="Libraries\DeepLearningLPR.dll">
<Link>DeepLearningLPR.dll</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Libraries\DeepLearningLPR26.dll">
<Link>DeepLearningLPR26.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Libraries\DeepLearning_CPU3.dll">
<Link>DeepLearning_CPU3.dll</Link>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="Libraries\DeepLearning_Model.dll">
<Link>DeepLearning_Model.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Libraries\DeepLearning_Sharp.dll">
<Link>DeepLearning_Sharp.dll</Link>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="Libraries\opencv_world4120.dll">
<Link>opencv_world4120.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Libraries\opencv_world470.dll">
<Link>opencv_world470.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Libraries\tbb12.dll">
<Link>tbb12.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,90 @@
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<LPR_Boxes> DetectLicensePlateExistence(Mat image, Rect roi)
{
List<LPR_Boxes> 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<LPR_Boxes> 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<LPR_Boxes> 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);
}
}
}

View File

@@ -0,0 +1,45 @@
using DeepLearning_Model_Sharp;
using OpenCvSharp;
namespace Inno.LPR
{
public class LicensePlateRecognitionMotionData
{
public Mat OriginalImage { get; private set; }
public DateTime ImageCreatedTime { get; init; }
public List<LPR_Boxes> DetectedLprBoxList { get; init; } = [];
public LPR_Boxes SelectedTrustedLprBox { get; private set; }
public bool IsLicensePlateExist { get; private set; } = false;
public List<string> LicensePlateCodeList { get; set; } = [];
public string LicensePlateCode { get; private set; } = string.Empty;
public int FrameNumber { get; set; } = -1;
public LicensePlateRecognitionMotionData(Mat originalImage, DateTime imageCreatedTime, List<LPR_Boxes> lprBoxes, bool isLicensePlateExist)
{
OriginalImage = originalImage.Clone();
ImageCreatedTime = imageCreatedTime;
DetectedLprBoxList.AddRange(lprBoxes);
if (DetectedLprBoxList.Count > 0)
{
SelectedTrustedLprBox = DetectedLprBoxList.OrderByDescending(box => box.y).First();
}
IsLicensePlateExist = isLicensePlateExist;
}
public void DrawLicensePlateCharacterBoxes(List<LPR_Boxes> boxes, uint coordinateOriginX, uint coordinateOriginY)
{
if (!IsLicensePlateExist) { return; }
//foreach (_LPR_Boxes mLPR_Chars in boxes)
//{
// Rect rect_Char = new((int)mLPR_Chars.x + (int)coordinateOriginX, (int)mLPR_Chars.y + (int)coordinateOriginY,
// (int)mLPR_Chars.w, (int)mLPR_Chars.h);
// Cv2.Rectangle(LicensePlateDisplayImage, rect_Char, Scalar.Blue, 1);
//}
}
public void AddLicensePlateCode(string lpCode)
{
LicensePlateCode = lpCode;
}
}
}

View File

@@ -0,0 +1,13 @@
using OpenCvSharp;
namespace Inno.LPR
{
public class MotionImageData(Mat image, Rect roi, int minimumArea, int maximumArea)
{
public Mat Image { get; private set; } = image;
public DateTime CreatedTime { get; private set; } = DateTime.Now;
public Rect ROI { get; private set; } = roi;
public int MinimumArea { get; private set; } = minimumArea;
public int MaximumArea { get; private set; } = maximumArea;
}
}