-
Notifications
You must be signed in to change notification settings - Fork 1
/
YoloWrapper.cs
139 lines (119 loc) · 4.78 KB
/
YoloWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Runtime.InteropServices;
namespace Darknet
{
public class YoloWrapper : IDisposable
{
private const string YoloLibraryName = "yolo_cpp_dll.dll";
private const int MaxObjects = 1000;
[DllImport(YoloLibraryName, EntryPoint = "init")]
private static extern int InitializeYolo(string configurationFilename, string weightsFilename, int gpu, int batch_size);
[DllImport(YoloLibraryName, EntryPoint = "detect_image")]
private static extern int DetectImage(string filename, ref BboxContainer container);
[DllImport(YoloLibraryName, EntryPoint = "detect_mat")]
private static extern int DetectImage(IntPtr pArray, int nSize, ref BboxContainer container);
[DllImport(YoloLibraryName, EntryPoint = "detect_mat2")]
private static extern int DetectImage2(IntPtr pArray, int nSize, ref BboxContainer container);
[DllImport(YoloLibraryName, EntryPoint = "dispose")]
private static extern int DisposeYolo();
[StructLayout(LayoutKind.Sequential)]
public struct bbox_t
{
public UInt32 x, y, w, h; // (x,y) - top-left corner, (w, h) - width & height of bounded box
public float prob; // confidence - probability that the object was found correctly
public UInt32 obj_id; // class of object - from range [0, classes-1]
public UInt32 track_id; // tracking id for video (0 - untracked, 1 - inf - tracked object)
public UInt32 frames_counter;
public float x_3d, y_3d, z_3d; // 3-D coordinates, if there is used 3D-stereo camera
};
[StructLayout(LayoutKind.Sequential)]
public struct BboxContainer
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxObjects)]
public bbox_t[] candidates;
}
public YoloWrapper(string configurationFilename, string weightsFilename, int gpu, int batch_size = 1)
{
InitializeYolo(configurationFilename, weightsFilename, gpu, batch_size);
}
public void Dispose()
{
DisposeYolo();
}
/// <summary>
/// yolov3.detect(image file path), the fastest method
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bbox_t[] Detect(string filename)
{
var container = new BboxContainer();
var count = DetectImage(filename, ref container);
return container.candidates;
}
/// <summary>
/// image -> mat (yolo orignal method)
/// little faster than Detect2(byte[])
/// </summary>
/// <param name="imageData"></param>
/// <returns></returns>
public bbox_t[] Detect(byte[] imageData)
{
var container = new BboxContainer();
var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
var pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(imageData, 0, pnt, imageData.Length);
var count = DetectImage(pnt, imageData.Length, ref container);
if (count == -1)
{
throw new NotSupportedException($"{YoloLibraryName} has no OpenCV support");
}
}
catch (Exception exception)
{
return null;
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
return container.candidates;
}
/// <summary>
/// image -> mat -> image_t
/// smith method, slower than Detect(byte[])
/// </summary>
/// <param name="imageData"></param>
/// <returns></returns>
public bbox_t[] Detect2(byte[] imageData)
{
var container = new BboxContainer();
var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
var pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(imageData, 0, pnt, imageData.Length);
var count = DetectImage2(pnt, imageData.Length, ref container);
if (count == -1)
{
throw new NotSupportedException($"{YoloLibraryName} has no OpenCV support");
}
}
catch (Exception exception)
{
return null;
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
return container.candidates;
}
}
}