373 lines
13 KiB
C#
373 lines
13 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|