Darknet.cs
3.1 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace eyemLib_Sharp
{
public class YoloWrapper : IDisposable
{
private const int MaxObjects = 1000;
[DllImport("yolo_cpp_dll.dll", EntryPoint = "init")]
private static extern int InitializeYolo(string configurationFilename, string weightsFilename, int gpu);
[DllImport("yolo_cpp_dll.dll", EntryPoint = "detect_image")]
private static extern int DetectImage(string filename, ref BboxContainer container);
[DllImport("yolo_cpp_dll.dll", EntryPoint = "detect_mat")]
private static extern int DetectImage(IntPtr pArray, int nSize, ref BboxContainer container);
[DllImport("yolo_cpp_dll.dll", 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)
{
InitializeYolo(configurationFilename, weightsFilename, gpu);
}
public void Dispose()
{
DisposeYolo();
}
public bbox_t[] Detect(string filename)
{
var container = new BboxContainer();
var count = DetectImage(filename, ref container);
return container.candidates;
}
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($"{"yolo_cpp_dll.dll"} has no OpenCV support");
}
}
catch (Exception exception)
{
return null;
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
return container.candidates;
}
}
}