-
Notifications
You must be signed in to change notification settings - Fork 2
[SDK-6187] Synchronous .NET Plugin #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Deneuki
wants to merge
18
commits into
main
Choose a base branch
from
SDK-6187_synchronous
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8cd674d
Rebase changes from original branch
Deneuki a1d5c0e
Documentation and simpler interface for customer
Deneuki cb8aa57
Line got cut off
Deneuki f7880f7
Document ResultOutput.cs and add documentation about integration with…
Deneuki 2a9a87d
Add more integration hints
Deneuki 4e5c361
Like the other branch, but synchronous
Deneuki b85c50d
"Main" method
Deneuki 8918897
Complete example
Deneuki 887301c
Get rid of some debug stuff
Deneuki 4ab51b5
Update documentation to match
Deneuki 24cf906
Prevent moving to next frame prematurely
Deneuki d4f2b2a
Register output action
Deneuki 0c20a76
Synchronization fixes
Deneuki 8e85f43
Remember to push changes
Deneuki 5e20a96
Progress push
Deneuki 87bbf10
This solution needn't a timeout
Deneuki 23bb791
Fix the bug
Deneuki 35121dd
Smart timeout
Deneuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #include <msclr/marshal.h> | ||
| #include <msclr/marshal_windows.h> | ||
|
|
||
| #include "Bridge.h" | ||
|
|
||
| #using MANAGED_PLUGIN_DLL | ||
|
|
||
| namespace neurala::dotnet { | ||
| void initialize() { | ||
| Neurala::Program::Main(); | ||
| } | ||
| } | ||
|
|
||
| namespace neurala::dotnet::result_output { | ||
| void invokeResultOutput(const char* metadata, const void* imageBytes, int width, int height) { | ||
| Neurala::ResultOutput::Invoke(msclr::interop::marshal_as<System::String^>(metadata), | ||
| msclr::interop::marshal_as<System::IntPtr>(const_cast<void*>(imageBytes)), | ||
| width, | ||
| height); | ||
| } | ||
| } | ||
|
|
||
| namespace neurala::dotnet::video_source { | ||
| void getMetadata(int& width, int& height) { | ||
| Neurala::VideoSource::GetMetadata(width, height); | ||
| } | ||
|
|
||
| void moveNextFrame(int& status) { | ||
| Neurala::VideoSource::MoveNextFrame(status); | ||
| } | ||
|
|
||
| void getFrame(void* buffer) { | ||
| Neurala::VideoSource::GetFrame(msclr::interop::marshal_as<System::IntPtr>(buffer)); | ||
| } | ||
|
|
||
| void execute(const char* action) { | ||
| Neurala::VideoSource::Execute(msclr::interop::marshal_as<System::String^>(action)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace neurala::dotnet { | ||
| void initialize(); | ||
| } | ||
|
|
||
| namespace neurala::dotnet::result_output { | ||
| void invokeResultOutput(const char* metadata, const void* imageBytes, int width, int height); | ||
| } | ||
|
|
||
| namespace neurala::dotnet::video_source { | ||
| void getMetadata(int& width, int& height); | ||
|
|
||
| void moveNextFrame(int& status); | ||
|
|
||
| void getFrame(void* buffer); | ||
|
|
||
| void execute(const char* action); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| add_library(cppcli SHARED Bridge.cpp) | ||
|
|
||
| set_target_properties(cppcli | ||
| PROPERTIES | ||
| COMMON_LANGUAGE_RUNTIME "" | ||
| CXX_STANDARD 17 | ||
| VS_DOTNET_REFERENCES "System" | ||
| WINDOWS_EXPORT_ALL_SYMBOLS ON) | ||
|
|
||
| target_compile_definitions(cppcli | ||
| PRIVATE | ||
| "MANAGED_PLUGIN_DLL=\"${MANAGED_PLUGIN_DLL}\"") | ||
|
|
||
| target_link_libraries(cppcli | ||
| PRIVATE | ||
| csharp_assemblies) | ||
|
|
||
| add_custom_command(TARGET cppcli POST_BUILD | ||
| COMMAND ${CMAKE_COMMAND} -E copy_if_different | ||
| "${MANAGED_PLUGIN_DLL}" | ||
| $<TARGET_FILE_DIR:cppcli>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| add_custom_command(OUTPUT Managed.dll | ||
| COMMAND dotnet build "${CMAKE_CURRENT_SOURCE_DIR}/Managed" -c Release) | ||
|
|
||
| add_library(csharp_assemblies INTERFACE Managed.dll) | ||
|
|
||
| set(MANAGED_PLUGIN_DLL "${CMAKE_CURRENT_SOURCE_DIR}/Managed/bin/x64/Release/net472/Managed.dll") | ||
|
|
||
| add_subdirectory(CLI) | ||
| add_subdirectory(Native) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net472</TargetFramework> | ||
| <Platforms>x64</Platforms> | ||
| <LangVersion>latest</LangVersion> | ||
| <ImplicitUsings>disable</ImplicitUsings> | ||
| <Nullable>disable</Nullable> | ||
| <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System; | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
| using System.IO; | ||
| using System.Threading; | ||
|
|
||
| namespace Neurala { | ||
| public static class Program { | ||
| public static void Main() { | ||
| // Get the images in the "C:\Images" directory. | ||
| var imageFiles = Directory.EnumerateFiles(@"C:\Images"); | ||
|
|
||
| // Simulate random pauses when this value is 0 to make sure SDKService | ||
| // can handle them. | ||
| var counter = 0; | ||
|
|
||
| // Run forever. | ||
| while (true) { | ||
| // For each file we found in the directory... | ||
| foreach (var imageFile in imageFiles) { | ||
| // Increment the counter modulo 16. | ||
| counter = (counter + 1) % 16; | ||
|
|
||
| // Simulate a delay 1/16 of the time. | ||
| if (counter == 0) | ||
| Thread.Sleep(1500); | ||
|
|
||
| // Say what file is being loaded. | ||
| Console.WriteLine($"Loading {imageFile} for processing."); | ||
|
|
||
| // Load the bitmap. | ||
| var bitmap = new Bitmap(imageFile); | ||
|
|
||
| // Send the image to SDKService and get back a result. | ||
| // If a result doesn't come back (e.g. if running in | ||
| // Live View mode), this will return an empty string. | ||
| var result = VideoSource.SendImage(bitmap); | ||
|
|
||
| // Print the result (if there is any). | ||
| Console.WriteLine($"Got result for image {imageFile}: {result}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Neurala { | ||
| public static class ResultOutput { | ||
| public static void Invoke(string metadata, IntPtr imageData, int width, int height) { | ||
| VideoSource.PushResult(metadata); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
| using System.Threading; | ||
|
|
||
| namespace Neurala { | ||
| public static class VideoSource { | ||
| private static Object Lock; | ||
| private static Bitmap PendingBitmap; | ||
| private static Bitmap CurrentBitmap; | ||
| private static String Result; | ||
| private static DateTime LastRequestTime; | ||
|
|
||
| private static bool WaitingForResult; | ||
|
|
||
| public static int Timeout { get; set; } | ||
|
|
||
| static VideoSource() { | ||
| Lock = new object(); | ||
| LastRequestTime = DateTime.Now; | ||
| Timeout = 1000; | ||
| } | ||
|
|
||
| public static string SendImage(Bitmap bitmap) { | ||
| lock (Lock) { | ||
| PendingBitmap = bitmap; | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} SENT IMAGE"); | ||
| Monitor.PulseAll(Lock); | ||
|
|
||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} AWAITING RESULT"); | ||
| LastRequestTime = DateTime.Now; | ||
| WaitingForResult = true; | ||
| while (Result == null) | ||
| Monitor.Wait(Lock); | ||
| WaitingForResult = false; | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} GOT RESULT"); | ||
| var result = Result; | ||
| Result = null; | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| internal static void PushResult(string result) { | ||
| lock (Lock) { | ||
| Result = result; | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} PUSHING RESULT"); | ||
| Monitor.PulseAll(Lock); | ||
| } | ||
| } | ||
|
|
||
| // Return (width, height) of current image. | ||
| public static void GetMetadata(out int width, out int height) { | ||
| var image = CurrentBitmap; | ||
|
|
||
| if (image != null) { | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} GETTING METADATA"); | ||
|
|
||
| width = image.Width; | ||
| height = image.Height; | ||
| } else { | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} CAN'T GET METADATA"); | ||
|
|
||
| width = 0; | ||
| height = 0; | ||
| } | ||
| } | ||
|
|
||
| public static void MoveNextFrame(out int status) { | ||
| lock (Lock) { | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} ENTERING MoveNextFrame"); | ||
|
|
||
| // Wait for a frame to come in. | ||
| if (PendingBitmap == null) | ||
| Monitor.Wait(Lock, Timeout); | ||
|
|
||
| // If nothing came in after the timeout... | ||
| if (PendingBitmap == null) { | ||
| // ...and if the user has been waiting for longer than normal... | ||
| if (WaitingForResult && (DateTime.Now - LastRequestTime).TotalMilliseconds > Timeout * 3) { | ||
| // ...then just use the current frame, because the pipeline must have been stopped at some | ||
| // point and restarted. | ||
| Console.WriteLine($" Using last frame, as the pipeline may have been stopped and restarted."); | ||
| PendingBitmap = CurrentBitmap; | ||
| } else { | ||
| // Otherwise, just get out of here. | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} TIMED OUT in MoveNextFrame"); | ||
| status = 1; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| CurrentBitmap = PendingBitmap; | ||
| PendingBitmap = null; | ||
| Console.WriteLine($"THREAD {Thread.CurrentThread.ManagedThreadId} EXITING MoveNextFrame"); | ||
| status = 0; | ||
| } | ||
| } | ||
|
|
||
| // Get current image frame data and copy to buffer. | ||
| public static void GetFrame(IntPtr buffer) { | ||
| var image = CurrentBitmap; | ||
| var bits = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), | ||
| ImageLockMode.ReadOnly, | ||
| image.PixelFormat); | ||
|
|
||
| CopyMemory(buffer, bits.Scan0, image.Width * image.Height * 3); | ||
|
|
||
| image.UnlockBits(bits); | ||
| } | ||
|
|
||
| // Execute output action. | ||
| public static void Execute(string action) { | ||
| Console.WriteLine($"Execute(\"{action}\")"); | ||
| } | ||
|
|
||
| [DllImport("kernel32.dll")] | ||
| private static extern void CopyMemory(IntPtr destination, IntPtr source, int length); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| cmake_minimum_required(VERSION 3.17.0) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
|
|
||
| add_library(dotnetCamera | ||
| SHARED | ||
| src/Plugin.cpp | ||
| src/ResultOutput.cpp | ||
| src/VideoSource.cpp) | ||
|
|
||
| target_compile_definitions(dotnetCamera PRIVATE NEURALA_EXPORT_PLUGIN) | ||
|
|
||
| add_executable(dotnetCameraTest | ||
| src/Main.cpp) | ||
|
|
||
| if(WIN32) | ||
| set_target_properties(dotnetCamera | ||
| PROPERTIES | ||
| WINDOWS_EXPORT_ALL_SYMBOLS ON) | ||
| endif() | ||
|
|
||
| target_include_directories(dotnetCamera | ||
| PUBLIC | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/include" | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/../CLI") | ||
|
|
||
| target_link_libraries(dotnetCamera | ||
| PUBLIC | ||
| cppcli | ||
| stub) | ||
|
|
||
| target_link_libraries(dotnetCameraTest | ||
| PRIVATE | ||
| ${CMAKE_DL_LIBS} | ||
| dotnetCamera) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright (c) Neurala Inc. 2013-2021 | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
| * associated documentation files (the "Software"), to deal in the Software without restriction, | ||
| * including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
| * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: The above copyright notice and this | ||
| * permission notice (including the next paragraph) shall be included in all copies or substantial | ||
| * portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
| * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| #ifndef NEURALA_CSHARP_RESULTS_OUTPUT_H | ||
| #define NEURALA_CSHARP_RESULTS_OUTPUT_H | ||
|
|
||
| #include "neurala/plugin/PluginArguments.h" | ||
| #include "neurala/plugin/PluginBindings.h" | ||
| #include "neurala/plugin/PluginErrorCallback.h" | ||
| #include "neurala/plugin/PluginManager.h" | ||
| #include "neurala/plugin/PluginStatus.h" | ||
| #include "neurala/utils/Version.h" | ||
| #include "neurala/utils/ResultsOutput.h" | ||
|
|
||
| namespace neurala | ||
| { | ||
| class PLUGIN_API CSharpResultOutput : public ResultsOutput | ||
| { | ||
| public: | ||
| virtual void operator()(const std::string& metadata, const dto::ImageView* image) noexcept override; | ||
|
|
||
| static void* create(PluginArguments&, PluginErrorCallback&); | ||
| static void destroy(void* p); | ||
| }; | ||
| } // namespace neurala | ||
|
|
||
| #endif // NEURALA_CSHARP_RESULTS_OUTPUT_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do better for debug log? Less spammy, or at least more control as to the level of spamminess?