Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.17.0)
# marks debug dlls
set(CMAKE_DEBUG_POSTFIX _d)

add_subdirectory(csharp)
add_subdirectory(empty)
add_subdirectory(dummy)
add_subdirectory(websocket)
39 changes: 39 additions & 0 deletions plugins/csharp/CLI/Bridge.cpp
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));
}
}
17 changes: 17 additions & 0 deletions plugins/csharp/CLI/Bridge.h
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);
}
21 changes: 21 additions & 0 deletions plugins/csharp/CLI/CMakeLists.txt
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>)
9 changes: 9 additions & 0 deletions plugins/csharp/CMakeLists.txt
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)
12 changes: 12 additions & 0 deletions plugins/csharp/Managed/Managed.csproj
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>
45 changes: 45 additions & 0 deletions plugins/csharp/Managed/Program.cs
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}");
}
}
}
}
}
10 changes: 10 additions & 0 deletions plugins/csharp/Managed/ResultOutput.cs
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);
}
}
}
122 changes: 122 additions & 0 deletions plugins/csharp/Managed/VideoSource.cs
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");
Copy link
Collaborator

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?

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);
}
}
35 changes: 35 additions & 0 deletions plugins/csharp/Native/CMakeLists.txt
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)
42 changes: 42 additions & 0 deletions plugins/csharp/Native/include/ResultOutput.h
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
Loading