有關于人臉識別的C#代碼,改成vb.net的,
public struct FaceInfo
{
public FaceInfo(float iWidth, float iAngle, float iCenter_x, float iCenter_y, float iConf)
{
mWidth = iWidth;
mAngle = iAngle;
mCenter_x = iCenter_x;
mCenter_y = iCenter_y;
mConf = iConf;
}
public float mWidth; // rectangle width
public float mAngle;//; = 0.0F; // rectangle tilt angle [-45 45] in degrees
public float mCenter_y;// = 0.0F; // rectangle center y
public float mCenter_x;// = 0.0F; // rectangle center x
public float mConf;// = 0.0F;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TrackFaceInfo
{
[MarshalAs(UnmanagedType.Struct)]
public FaceInfo box;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 144)]
public int[] landmarks;// = new int[144];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public float[] headPose;// = new float[3];
public float score;// = 0.0F;
public UInt32 face_id;// = 0;
}
TrackFaceInfo[] track_info = new TrackFaceInfo[ilen];
for (int i = 0; i < ilen; i++)
{
track_info[i] = new TrackFaceInfo();
track_info[i].landmarks = new int[144];
track_info[i].headPose = new float[3];
track_info[i].face_id = 0;
track_info[i].score = 0;
}
int sizeTrack = Marshal.SizeOf(typeof(TrackFaceInfo));
將上面的代碼改成VB.NET :
Public Structure FaceInfo
Public Sub New(ByVal iWidth As Single, ByVal iAngle As Single, ByVal iCenter_x As Single, ByVal iCenter_y As Single, ByVal iConf As Single)
mWidth = iWidth
mAngle = iAngle
mCenter_x = iCenter_x
mCenter_y = iCenter_y
mConf = iConf
End Sub
Public mWidth As Single ' rectangle width() As mWidth
Public mAngle As Single
Public mCenter_y As Single
Public mCenter_x As Single
Public mConf As Single
End Structure
Public Structure TrackFaceInfo
<MarshalAs(UnmanagedType.Struct)>
Public box As FaceInfo
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=144)>
Public landmarks As Integer() ' = New int[144];() As Integer
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=3)>
Public headPose As Single() ' = new float[3];() As single
Public score As Single ' = 0.0F; As single
Public face_id As UInt32 ' = 0; As UInt32
End Structure
Dim ilen As Integer = 2 ';//傳入的人臉數
Dim track_info As TrackFaceInfo() = New TrackFaceInfo(ilen) {}
For i = 0 To ilen - 1
track_info(i) = New TrackFaceInfo()
track_info(i).landmarks = New Integer(144) {}
track_info(i).headPose = New Single(3) {}
track_info(i).face_id = 0
track_info(i).score = 0
Next
Dim sizeTrack As Integer = Marshal.SizeOf(track_info.GetType)
最后一句出錯:“System.ArgumentException”型別的未經處理的例外在 mscorlib.dll 中發生
其他資訊: 型別“WindowsApplication1.Module1+TrackFaceInfo[]”不能作為非托管結構進行封送處理;無法計算有意義的大小或偏移量。
最后一句不知該怎么寫?
uj5u.com熱心網友回復:
好像找到原因了Dim sizeTrack As Integer = Marshal.SizeOf(track_info.GetType)改成:
Dim sizeTrack As Integer = Marshal.SizeOf(track_info(0).GetType)
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
Marshal.SizeOf只能用于不含參考的全是值型別的類(結構)陣列肯定是不行的,所以就出了那個錯
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/146895.html
標籤:VB.NET
