Tino Hager
2018-06-24 b2c03b1615535dc717e196c560901e9e420c96d6
fix build error, add c# wrapper class
2 files modified
1 files added
96 ■■■■■ changed files
.gitignore 3 ●●●● patch | view | raw | blame | history
build/darknet/YoloWrapper.cs 83 ●●●●● patch | view | raw | blame | history
src/yolo_v2_class.hpp 10 ●●●● patch | view | raw | blame | history
.gitignore
@@ -14,7 +14,8 @@
decaf/
submission/
cfg/
darknet
build/darknet/*
!build/darknet/YoloWrapper.cs
.fuse*
# OS Generated #
build/darknet/YoloWrapper.cs
New file
@@ -0,0 +1,83 @@
using System;
using System.Runtime.InteropServices;
namespace Darknet
{
    public class YoloWrapper : IDisposable
    {
        private const string YoloLibraryName = "yolo_cpp_dll.dll";
        [DllImport(YoloLibraryName, EntryPoint = "init")]
        public static extern int InitializeYolo(string configurationFilename, string weightsFilename, int gpu);
        [DllImport(YoloLibraryName, EntryPoint = "detect_image")]
        public static extern int DetectImage(string filename, ref BboxContainer container);
        [DllImport(YoloLibraryName, EntryPoint = "detect_mat")]
        public static extern int DetectImage(IntPtr pArray, int nSize, ref BboxContainer container);
        [DllImport(YoloLibraryName, EntryPoint = "dispose")]
        public 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;
        };
        [StructLayout(LayoutKind.Sequential, Size = 10)]
        public struct BboxContainer
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
            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);
                DetectImage(pnt, imageData.Length, ref container);
            }
            catch (Exception exception)
            {
                return null;
            }
            finally
            {
                // Free the unmanaged memory.
                Marshal.FreeHGlobal(pnt);
            }
            return container.candidates;
        }
    }
}
src/yolo_v2_class.hpp
@@ -45,11 +45,11 @@
#include "opencv2/imgproc/imgproc_c.h"  // C
#endif  // OPENCV
extern "C" __declspec(dllexport) int max_objects();
extern "C" __declspec(dllexport) int init(const char *configurationFilename, const char *weightsFilename, int gpu);
extern "C" __declspec(dllexport) int detect_image(const char *filename, bbox_t_container &container);
extern "C" __declspec(dllexport) int detect_mat(const uint8_t* data, const size_t data_length, bbox_t_container &container);
extern "C" __declspec(dllexport) int dispose();
extern "C" YOLODLL_API int max_objects();
extern "C" YOLODLL_API int init(const char *configurationFilename, const char *weightsFilename, int gpu);
extern "C" YOLODLL_API int detect_image(const char *filename, bbox_t_container &container);
extern "C" YOLODLL_API int detect_mat(const uint8_t* data, const size_t data_length, bbox_t_container &container);
extern "C" YOLODLL_API int dispose();
class Detector {
    std::shared_ptr<void> detector_gpu_ptr;