From 00903aebd3d4979ff5128c981d2f13e5595454c6 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sat, 23 Jun 2018 11:02:37 +0200 Subject: [PATCH] .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;