From 00903aebd3d4979ff5128c981d2f13e5595454c6 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sat, 23 Jun 2018 11:02:37 +0200 Subject: [PATCH 1/5] .NET/C# support integration Add logic for .NET/C# integration (DllImport) --- build/darknet/darknet.sln | 11 ++++-- build/darknet/darknet.vcxproj | 29 ++++++++------- build/darknet/yolo_cpp_dll.vcxproj | 16 ++++---- build/darknet/yolo_cpp_dll_no_gpu.vcxproj | 24 ++++++------ src/yolo_v2_class.cpp | 45 +++++++++++++++++++++++ src/yolo_v2_class.hpp | 11 ++++++ 6 files changed, 99 insertions(+), 37 deletions(-) diff --git a/build/darknet/darknet.sln b/build/darknet/darknet.sln index c49e10ef..3d7efef2 100644 --- a/build/darknet/darknet.sln +++ b/build/darknet/darknet.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.25420.1 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2018 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "darknet", "darknet.vcxproj", "{4CF5694F-12A5-4012-8D94-9A0915E9FEB5}" EndProject @@ -13,8 +13,8 @@ Global Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.ActiveCfg = Debug|Win32 - {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.Build.0 = Debug|Win32 + {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.ActiveCfg = Debug|x64 + {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.Build.0 = Debug|x64 {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|x64.ActiveCfg = Debug|x64 {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|x64.Build.0 = Debug|x64 {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Release|Win32.ActiveCfg = Release|Win32 @@ -25,4 +25,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C904B7F0-81ED-4CBE-82EC-94566A77D9AF} + EndGlobalSection EndGlobal diff --git a/build/darknet/darknet.vcxproj b/build/darknet/darknet.vcxproj index 8fbf93c3..92861642 100644 --- a/build/darknet/darknet.vcxproj +++ b/build/darknet/darknet.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,38 +21,38 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 8.1 + 10.0.17134.0 Application true - v140 + v141 MultiByte Application true - v140 + v141 MultiByte Application false - v140 + v141 true MultiByte Application false - v140 + v141 true MultiByte - + @@ -88,15 +88,16 @@ Level3 Disabled true - C:\opencv_3.0\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include - CUDNN;_CRTDBG_MAP_ALLOC;_MBCS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - OPENCV; + C:\opencv\opencv340\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include + OPENCV;CUDNN;_CRTDBG_MAP_ALLOC;_MBCS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + + true stdlib.h;crtdbg.h;%(ForcedIncludeFiles) true - C:\opencv_3.0\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc12\lib;C:\opencv_2.4.13\opencv\build\x64\vc14\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) + C:\opencv\opencv340\build\x64\vc15\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) $(OutDir)\$(TargetName)$(TargetExt) ..\..\3rdparty\lib\x64\pthreadVC2.lib;cublas.lib;curand.lib;cudart.lib;%(AdditionalDependencies) true @@ -132,7 +133,7 @@ true true true - C:\opencv_3.0\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include + C:\opencv\opencv340\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include OPENCV;CUDNN;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) c11 c++1y @@ -146,7 +147,7 @@ true true true - C:\opencv_3.0\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc12\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) + C:\opencv\opencv340\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc12\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) ..\..\3rdparty\lib\x64\pthreadVC2.lib;cublas.lib;curand.lib;cudart.lib;%(AdditionalDependencies) $(OutDir)\$(TargetName)$(TargetExt) @@ -288,6 +289,6 @@ - + \ No newline at end of file diff --git a/build/darknet/yolo_cpp_dll.vcxproj b/build/darknet/yolo_cpp_dll.vcxproj index c33be37f..5deb9d41 100644 --- a/build/darknet/yolo_cpp_dll.vcxproj +++ b/build/darknet/yolo_cpp_dll.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,38 +21,38 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 8.1 + 10.0.17134.0 Application true - v140 + v141 MultiByte DynamicLibrary true - v140 + v141 MultiByte Application false - v140 + v141 true MultiByte DynamicLibrary false - v140 + v141 true MultiByte - + @@ -291,6 +291,6 @@ - + \ No newline at end of file diff --git a/build/darknet/yolo_cpp_dll_no_gpu.vcxproj b/build/darknet/yolo_cpp_dll_no_gpu.vcxproj index 56753c45..123b96e7 100644 --- a/build/darknet/yolo_cpp_dll_no_gpu.vcxproj +++ b/build/darknet/yolo_cpp_dll_no_gpu.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,32 +21,32 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 8.1 + 10.0.17134.0 Application true - v140 + v141 MultiByte DynamicLibrary true - v140 + v141 MultiByte Application false - v140 + v141 true MultiByte DynamicLibrary false - v140 + v141 true MultiByte @@ -111,7 +111,7 @@ true true true - C:\opencv_2.4.9\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include + C:\opencv\opencv340\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include YOLODLL_EXPORTS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) true @@ -119,7 +119,7 @@ true true true - C:\opencv_2.4.9\opencv\build\x86\vc14\lib;C:\opencv_2.4.9\opencv\build\x86\vc12\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) + C:\opencv\opencv340\build\x64\vc15\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) ..\..\3rdparty\lib\x86\pthreadVC2.lib;cudart.lib;cublas.lib;curand.lib;%(AdditionalDependencies) @@ -130,8 +130,8 @@ true true true - ..\..\3rdparty\include;%(AdditionalIncludeDirectories) - YOLODLL_EXPORTS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + ..\..\3rdparty\include;C:\opencv\opencv340\build\include;%(AdditionalIncludeDirectories) + YOLODLL_EXPORTS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;OPENCV;%(PreprocessorDefinitions) c11 c++1y CompileAsCpp @@ -140,12 +140,14 @@ true true + + true true true - %(AdditionalLibraryDirectories) + C:\opencv\opencv340\build\x64\vc15\lib;%(AdditionalLibraryDirectories) ..\..\3rdparty\lib\x64\pthreadVC2.lib;%(AdditionalDependencies) $(OutDir)\$(TargetName)$(TargetExt) diff --git a/src/yolo_v2_class.cpp b/src/yolo_v2_class.cpp index faaf8d11..6147e3c4 100644 --- a/src/yolo_v2_class.cpp +++ b/src/yolo_v2_class.cpp @@ -22,6 +22,51 @@ extern "C" { #define FRAMES 3 +int max_objects() { return C_SHARP_MAX_OBJECTS; } + +static Detector* detector; +//static std::unique_ptr detector; + +int init(const char *configurationFilename, const char *weightsFilename, int gpu) { + std::string configurationFilenameString; + configurationFilenameString = configurationFilename; + std::string weightsFilenameString; + weightsFilenameString = weightsFilename; + + detector = new Detector(configurationFilenameString, weightsFilenameString, gpu); + return 1; +} + +int detect_image(const char *filename, bbox_t_container &container) { + std::string filenameString; + filenameString = filename; + + std::vector detection = detector->detect(filenameString); + for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i) + container.candidates[i] = detection[i]; + return detection.size(); +} + +int detect_image2(const uint8_t* data, const size_t data_length, bbox_t_container &container) { +#ifdef OPENCV + std::vector vdata(data, data + data_length); + cv::Mat image = imdecode(cv::Mat(vdata), 1); + + std::vector detection = detector->detect(image); + for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i) + container.candidates[i] = detection[i]; + return detection.size(); +#else + return -1; +#endif // OPENCV +} + +int dispose() { + detector->~Detector(); + //detector.reset(); + return 1; +} + #ifdef GPU void check_cuda(cudaError_t status) { if (status != cudaSuccess) { diff --git a/src/yolo_v2_class.hpp b/src/yolo_v2_class.hpp index fd00dc06..2416050b 100644 --- a/src/yolo_v2_class.hpp +++ b/src/yolo_v2_class.hpp @@ -28,6 +28,11 @@ struct image_t { float *data; // pointer to the image data }; +#define C_SHARP_MAX_OBJECTS 1000 +struct bbox_t_container { + bbox_t candidates[C_SHARP_MAX_OBJECTS]; +}; + #ifdef __cplusplus #include #include @@ -40,6 +45,12 @@ struct image_t { #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_image2(const uint8_t* data, const size_t data_length, bbox_t_container &container); +extern "C" __declspec(dllexport) int dispose(); + class Detector { std::shared_ptr detector_gpu_ptr; std::deque> prev_bbox_vec_deque; From 79caf3837a955c1a80186e43e3c0a9eaf1130332 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sat, 23 Jun 2018 11:05:26 +0200 Subject: [PATCH 2/5] Revert ".NET/C# support integration" This reverts commit 00903aebd3d4979ff5128c981d2f13e5595454c6. --- build/darknet/darknet.sln | 11 ++---- build/darknet/darknet.vcxproj | 29 +++++++-------- build/darknet/yolo_cpp_dll.vcxproj | 16 ++++---- build/darknet/yolo_cpp_dll_no_gpu.vcxproj | 24 ++++++------ src/yolo_v2_class.cpp | 45 ----------------------- src/yolo_v2_class.hpp | 11 ------ 6 files changed, 37 insertions(+), 99 deletions(-) diff --git a/build/darknet/darknet.sln b/build/darknet/darknet.sln index 3d7efef2..c49e10ef 100644 --- a/build/darknet/darknet.sln +++ b/build/darknet/darknet.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2018 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "darknet", "darknet.vcxproj", "{4CF5694F-12A5-4012-8D94-9A0915E9FEB5}" EndProject @@ -13,8 +13,8 @@ Global Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.ActiveCfg = Debug|x64 - {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.Build.0 = Debug|x64 + {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.ActiveCfg = Debug|Win32 + {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|Win32.Build.0 = Debug|Win32 {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|x64.ActiveCfg = Debug|x64 {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Debug|x64.Build.0 = Debug|x64 {4CF5694F-12A5-4012-8D94-9A0915E9FEB5}.Release|Win32.ActiveCfg = Release|Win32 @@ -25,7 +25,4 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {C904B7F0-81ED-4CBE-82EC-94566A77D9AF} - EndGlobalSection EndGlobal diff --git a/build/darknet/darknet.vcxproj b/build/darknet/darknet.vcxproj index 92861642..8fbf93c3 100644 --- a/build/darknet/darknet.vcxproj +++ b/build/darknet/darknet.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,38 +21,38 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 10.0.17134.0 + 8.1 Application true - v141 + v140 MultiByte Application true - v141 + v140 MultiByte Application false - v141 + v140 true MultiByte Application false - v141 + v140 true MultiByte - + @@ -88,16 +88,15 @@ Level3 Disabled true - C:\opencv\opencv340\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include - OPENCV;CUDNN;_CRTDBG_MAP_ALLOC;_MBCS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - - + C:\opencv_3.0\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include + CUDNN;_CRTDBG_MAP_ALLOC;_MBCS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + OPENCV; true stdlib.h;crtdbg.h;%(ForcedIncludeFiles) true - C:\opencv\opencv340\build\x64\vc15\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) + C:\opencv_3.0\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc12\lib;C:\opencv_2.4.13\opencv\build\x64\vc14\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) $(OutDir)\$(TargetName)$(TargetExt) ..\..\3rdparty\lib\x64\pthreadVC2.lib;cublas.lib;curand.lib;cudart.lib;%(AdditionalDependencies) true @@ -133,7 +132,7 @@ true true true - C:\opencv\opencv340\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include + C:\opencv_3.0\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include OPENCV;CUDNN;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) c11 c++1y @@ -147,7 +146,7 @@ true true true - C:\opencv\opencv340\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc12\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) + C:\opencv_3.0\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc14\lib;C:\opencv_2.4.13\opencv\build\x64\vc12\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) ..\..\3rdparty\lib\x64\pthreadVC2.lib;cublas.lib;curand.lib;cudart.lib;%(AdditionalDependencies) $(OutDir)\$(TargetName)$(TargetExt) @@ -289,6 +288,6 @@ - + \ No newline at end of file diff --git a/build/darknet/yolo_cpp_dll.vcxproj b/build/darknet/yolo_cpp_dll.vcxproj index 5deb9d41..c33be37f 100644 --- a/build/darknet/yolo_cpp_dll.vcxproj +++ b/build/darknet/yolo_cpp_dll.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,38 +21,38 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 10.0.17134.0 + 8.1 Application true - v141 + v140 MultiByte DynamicLibrary true - v141 + v140 MultiByte Application false - v141 + v140 true MultiByte DynamicLibrary false - v141 + v140 true MultiByte - + @@ -291,6 +291,6 @@ - + \ No newline at end of file diff --git a/build/darknet/yolo_cpp_dll_no_gpu.vcxproj b/build/darknet/yolo_cpp_dll_no_gpu.vcxproj index 123b96e7..56753c45 100644 --- a/build/darknet/yolo_cpp_dll_no_gpu.vcxproj +++ b/build/darknet/yolo_cpp_dll_no_gpu.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,32 +21,32 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 10.0.17134.0 + 8.1 Application true - v141 + v140 MultiByte DynamicLibrary true - v141 + v140 MultiByte Application false - v141 + v140 true MultiByte DynamicLibrary false - v141 + v140 true MultiByte @@ -111,7 +111,7 @@ true true true - C:\opencv\opencv340\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include + C:\opencv_2.4.9\opencv\build\include;..\..\3rdparty\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(cudnn)\include YOLODLL_EXPORTS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;GPU;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) true @@ -119,7 +119,7 @@ true true true - C:\opencv\opencv340\build\x64\vc15\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) + C:\opencv_2.4.9\opencv\build\x86\vc14\lib;C:\opencv_2.4.9\opencv\build\x86\vc12\lib;$(CUDA_PATH)lib\$(PlatformName);$(cudnn)\lib\x64;%(AdditionalLibraryDirectories) ..\..\3rdparty\lib\x86\pthreadVC2.lib;cudart.lib;cublas.lib;curand.lib;%(AdditionalDependencies) @@ -130,8 +130,8 @@ true true true - ..\..\3rdparty\include;C:\opencv\opencv340\build\include;%(AdditionalIncludeDirectories) - YOLODLL_EXPORTS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;OPENCV;%(PreprocessorDefinitions) + ..\..\3rdparty\include;%(AdditionalIncludeDirectories) + YOLODLL_EXPORTS;_TIMESPEC_DEFINED;_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) c11 c++1y CompileAsCpp @@ -140,14 +140,12 @@ true true - - true true true - C:\opencv\opencv340\build\x64\vc15\lib;%(AdditionalLibraryDirectories) + %(AdditionalLibraryDirectories) ..\..\3rdparty\lib\x64\pthreadVC2.lib;%(AdditionalDependencies) $(OutDir)\$(TargetName)$(TargetExt) diff --git a/src/yolo_v2_class.cpp b/src/yolo_v2_class.cpp index 6147e3c4..faaf8d11 100644 --- a/src/yolo_v2_class.cpp +++ b/src/yolo_v2_class.cpp @@ -22,51 +22,6 @@ extern "C" { #define FRAMES 3 -int max_objects() { return C_SHARP_MAX_OBJECTS; } - -static Detector* detector; -//static std::unique_ptr detector; - -int init(const char *configurationFilename, const char *weightsFilename, int gpu) { - std::string configurationFilenameString; - configurationFilenameString = configurationFilename; - std::string weightsFilenameString; - weightsFilenameString = weightsFilename; - - detector = new Detector(configurationFilenameString, weightsFilenameString, gpu); - return 1; -} - -int detect_image(const char *filename, bbox_t_container &container) { - std::string filenameString; - filenameString = filename; - - std::vector detection = detector->detect(filenameString); - for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i) - container.candidates[i] = detection[i]; - return detection.size(); -} - -int detect_image2(const uint8_t* data, const size_t data_length, bbox_t_container &container) { -#ifdef OPENCV - std::vector vdata(data, data + data_length); - cv::Mat image = imdecode(cv::Mat(vdata), 1); - - std::vector detection = detector->detect(image); - for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i) - container.candidates[i] = detection[i]; - return detection.size(); -#else - return -1; -#endif // OPENCV -} - -int dispose() { - detector->~Detector(); - //detector.reset(); - return 1; -} - #ifdef GPU void check_cuda(cudaError_t status) { if (status != cudaSuccess) { diff --git a/src/yolo_v2_class.hpp b/src/yolo_v2_class.hpp index 2416050b..fd00dc06 100644 --- a/src/yolo_v2_class.hpp +++ b/src/yolo_v2_class.hpp @@ -28,11 +28,6 @@ struct image_t { float *data; // pointer to the image data }; -#define C_SHARP_MAX_OBJECTS 1000 -struct bbox_t_container { - bbox_t candidates[C_SHARP_MAX_OBJECTS]; -}; - #ifdef __cplusplus #include #include @@ -45,12 +40,6 @@ struct bbox_t_container { #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_image2(const uint8_t* data, const size_t data_length, bbox_t_container &container); -extern "C" __declspec(dllexport) int dispose(); - class Detector { std::shared_ptr detector_gpu_ptr; std::deque> prev_bbox_vec_deque; From 8d2f2653c8a9f7101dbd4c0727beccaa48c98634 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sat, 23 Jun 2018 11:06:43 +0200 Subject: [PATCH 3/5] .NET/C# support integration Add logic for .NET/C# integration (DllImport) --- src/yolo_v2_class.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++ src/yolo_v2_class.hpp | 11 +++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/yolo_v2_class.cpp b/src/yolo_v2_class.cpp index faaf8d11..6147e3c4 100644 --- a/src/yolo_v2_class.cpp +++ b/src/yolo_v2_class.cpp @@ -22,6 +22,51 @@ extern "C" { #define FRAMES 3 +int max_objects() { return C_SHARP_MAX_OBJECTS; } + +static Detector* detector; +//static std::unique_ptr detector; + +int init(const char *configurationFilename, const char *weightsFilename, int gpu) { + std::string configurationFilenameString; + configurationFilenameString = configurationFilename; + std::string weightsFilenameString; + weightsFilenameString = weightsFilename; + + detector = new Detector(configurationFilenameString, weightsFilenameString, gpu); + return 1; +} + +int detect_image(const char *filename, bbox_t_container &container) { + std::string filenameString; + filenameString = filename; + + std::vector detection = detector->detect(filenameString); + for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i) + container.candidates[i] = detection[i]; + return detection.size(); +} + +int detect_image2(const uint8_t* data, const size_t data_length, bbox_t_container &container) { +#ifdef OPENCV + std::vector vdata(data, data + data_length); + cv::Mat image = imdecode(cv::Mat(vdata), 1); + + std::vector detection = detector->detect(image); + for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i) + container.candidates[i] = detection[i]; + return detection.size(); +#else + return -1; +#endif // OPENCV +} + +int dispose() { + detector->~Detector(); + //detector.reset(); + return 1; +} + #ifdef GPU void check_cuda(cudaError_t status) { if (status != cudaSuccess) { diff --git a/src/yolo_v2_class.hpp b/src/yolo_v2_class.hpp index fd00dc06..2416050b 100644 --- a/src/yolo_v2_class.hpp +++ b/src/yolo_v2_class.hpp @@ -28,6 +28,11 @@ struct image_t { float *data; // pointer to the image data }; +#define C_SHARP_MAX_OBJECTS 1000 +struct bbox_t_container { + bbox_t candidates[C_SHARP_MAX_OBJECTS]; +}; + #ifdef __cplusplus #include #include @@ -40,6 +45,12 @@ struct image_t { #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_image2(const uint8_t* data, const size_t data_length, bbox_t_container &container); +extern "C" __declspec(dllexport) int dispose(); + class Detector { std::shared_ptr detector_gpu_ptr; std::deque> prev_bbox_vec_deque; From 9ed3e2df4a2dc765245fa2e75ac4a9f4e6187b83 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sun, 24 Jun 2018 21:22:50 +0200 Subject: [PATCH 4/5] rename c++ method detect_image2 -> detect_mat --- src/yolo_v2_class.cpp | 2 +- src/yolo_v2_class.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yolo_v2_class.cpp b/src/yolo_v2_class.cpp index 6147e3c4..adba7324 100644 --- a/src/yolo_v2_class.cpp +++ b/src/yolo_v2_class.cpp @@ -47,7 +47,7 @@ int detect_image(const char *filename, bbox_t_container &container) { return detection.size(); } -int detect_image2(const uint8_t* data, const size_t data_length, bbox_t_container &container) { +int detect_mat(const uint8_t* data, const size_t data_length, bbox_t_container &container) { #ifdef OPENCV std::vector vdata(data, data + data_length); cv::Mat image = imdecode(cv::Mat(vdata), 1); diff --git a/src/yolo_v2_class.hpp b/src/yolo_v2_class.hpp index 2416050b..2715ce39 100644 --- a/src/yolo_v2_class.hpp +++ b/src/yolo_v2_class.hpp @@ -48,7 +48,7 @@ struct bbox_t_container { 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_image2(const uint8_t* data, const size_t data_length, 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(); class Detector { From b2c03b1615535dc717e196c560901e9e420c96d6 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sun, 24 Jun 2018 22:45:07 +0200 Subject: [PATCH 5/5] fix build error, add c# wrapper class --- .gitignore | 3 +- build/darknet/YoloWrapper.cs | 83 ++++++++++++++++++++++++++++++++++++ src/yolo_v2_class.hpp | 10 ++--- 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 build/darknet/YoloWrapper.cs diff --git a/.gitignore b/.gitignore index bbfed659..1bd67d08 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,8 @@ convnet/ decaf/ submission/ cfg/ -darknet +build/darknet/* +!build/darknet/YoloWrapper.cs .fuse* # OS Generated # diff --git a/build/darknet/YoloWrapper.cs b/build/darknet/YoloWrapper.cs new file mode 100644 index 00000000..ade00597 --- /dev/null +++ b/build/darknet/YoloWrapper.cs @@ -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; + } + } +} diff --git a/src/yolo_v2_class.hpp b/src/yolo_v2_class.hpp index 2715ce39..98758783 100644 --- a/src/yolo_v2_class.hpp +++ b/src/yolo_v2_class.hpp @@ -45,11 +45,11 @@ struct bbox_t_container { #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 detector_gpu_ptr;