Skip to content

Commit fd21528

Browse files
author
Chris Maunder
committed
Re-org of SDK namespace / simplifying meshbuilder
1 parent 8359a5a commit fd21528

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+216
-264
lines changed

modules/ObjectDetectionYOLOv5Net/ObjectDetectionWorker.cs renamed to modules/ObjectDetectionYOLOv5Net/ObjectDetectionModuleRunner.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5+
using System.Dynamic;
56
using System.IO;
67
using System.Linq;
78

89
using Microsoft.Extensions.Configuration;
910
using Microsoft.Extensions.Logging;
1011
using Microsoft.Extensions.Hosting;
1112

12-
using CodeProject.AI.SDK;
1313
using CodeProject.AI.SDK.API;
14+
using CodeProject.AI.SDK.Backend;
15+
using CodeProject.AI.SDK.Client;
16+
using CodeProject.AI.SDK.Common;
1417
using CodeProject.AI.SDK.Utils;
1518

1619
using Yolov5Net.Scorer;
17-
using System.Dynamic;
1820

1921
#pragma warning disable CS0162 // unreachable code
2022

@@ -27,7 +29,7 @@ namespace CodeProject.AI.Modules.ObjectDetection.YOLOv5
2729
/// While intended for development and tests, this also demonstrates how a backend service can
2830
/// be created with the .NET Core framework.
2931
/// </summary>
30-
public class ObjectDetectionWorker : ModuleWorkerBase
32+
public class ObjectDetectionModuleRunner : ModuleRunnerBase
3133
{
3234
private const bool ShowTrace = false;
3335

@@ -49,20 +51,20 @@ public class ObjectDetectionWorker : ModuleWorkerBase
4951
/// <param name="logger">The Logger.</param>
5052
/// <param name="config">The app configuration values.</param>
5153
/// <param name="hostApplicationLifetime">The applicationLifetime object</param>
52-
public ObjectDetectionWorker(ILogger<ObjectDetector> logger, IConfiguration config,
54+
public ObjectDetectionModuleRunner(ILogger<ObjectDetector> logger, IConfiguration config,
5355
IHostApplicationLifetime hostApplicationLifetime)
5456
: base(logger, config, hostApplicationLifetime)
5557
{
5658
_logger = logger;
5759

5860
_mode = config.GetValue("MODEL_SIZE", "Medium") ?? "Medium";
59-
_modelDir = config.GetValue("MODELS_DIR", Path.Combine(moduleDirPath!, "assets")) ?? "assets";
60-
_customDir = config.GetValue("CUSTOM_MODELS_DIR", Path.Combine(moduleDirPath!, "custom-models")) ?? "custom-models";
61+
_modelDir = config.GetValue("MODELS_DIR", Path.Combine(ModuleDirPath!, "assets")) ?? "assets";
62+
_customDir = config.GetValue("CUSTOM_MODELS_DIR", Path.Combine(ModuleDirPath!, "custom-models")) ?? "custom-models";
6163

62-
if (!_modelDir.EndsWith("/") || !_modelDir.EndsWith("\\"))
64+
if (!_modelDir.EndsWith('/') || !_modelDir.EndsWith('\\'))
6365
_modelDir += "/";
6466

65-
if (!_customDir.EndsWith("/") || !_customDir.EndsWith("\\"))
67+
if (!_customDir.EndsWith('/') || !_customDir.EndsWith('\\'))
6668
_customDir += "/";
6769

6870
_modelDir = Text.FixSlashes(_modelDir);
@@ -234,7 +236,7 @@ protected override int SelfTest()
234236
{
235237
RequestPayload payload = new RequestPayload("detect");
236238
payload.SetValue("minconfidence", "0.4");
237-
payload.AddFile(Path.Combine(moduleDirPath!, "test/home-office.jpg"));
239+
payload.AddFile(Path.Combine(ModuleDirPath!, "test/home-office.jpg"));
238240

239241
var request = new BackendRequest(payload);
240242
ModuleResponse response = Process(request);

modules/ObjectDetectionYOLOv5Net/ObjectDetectionYOLOv5Net.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@
8080
</PropertyGroup>
8181

8282
<ItemGroup>
83-
<PackageReference Include="CodeProject.AI.Module.SDK" Version="1.0.1" />
83+
<!-- <PackageReference Include="CodeProject.AI.Module.SDK" Version="1.1.0" /> -->
84+
<ProjectReference Include="..\..\src\SDK\NET\NET.csproj" />
8485

8586
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.3" Condition="'$(GpuType)'=='CPU'" />
86-
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.19.2" Condition="'$(GpuType)'=='CUDA'" />
87+
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.19.2" Condition="'$(GpuType)'=='CUDA'" />
8788
<PackageReference Include="Microsoft.ML.OnnxRuntime.OpenVino" Version="1.13.1-dev-20221026-1209-861125ccb" Condition="'$(GpuType)'=='OpenVINO'" />
8889
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.19.2" Condition="'$(GpuType)'=='DirectML'" />
8990
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.3" Condition="'$(GpuType)'=='MPS'" />

modules/ObjectDetectionYOLOv5Net/ObjectDetector.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
using SkiaSharp;
1010
using Yolov5Net.Scorer;
1111
using Yolov5Net.Scorer.Models;
12-
using CodeProject.AI.SDK;
12+
1313
using CodeProject.AI.SDK.API;
14+
using CodeProject.AI.SDK.Client;
1415
using CodeProject.AI.SDK.Utils;
1516

1617
namespace CodeProject.AI.Modules.ObjectDetection.YOLOv5

modules/ObjectDetectionYOLOv5Net/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ internal static class Program
88
{
99
static async Task Main(string[]? args)
1010
{
11-
ObjectDetectionWorker.ProcessArguments(args);
11+
ObjectDetectionModuleRunner.ProcessArguments(args);
1212

1313
IHost host = Host.CreateDefaultBuilder(args)
1414
.ConfigureServices(services =>
1515
{
16-
services.AddHostedService<ObjectDetectionWorker>();
16+
services.AddHostedService<ObjectDetectionModuleRunner>();
1717
})
1818
.Build();
1919

modules/ObjectDetectionYOLOv5Net/modulesettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"ObjectDetectionYOLOv5Net": {
55
"Name": "Object Detection (YOLOv5 .NET)",
6-
"Version": "1.12.0",
6+
"Version": "1.13.0",
77

88
"PublishingInfo" : {
99
"Description": "Provides Object Detection using YOLOv5 ONNX models with DirectML. This module is best for those on Windows and Linux without CUDA enabled GPUs",
@@ -70,7 +70,8 @@
7070
{ "ModuleVersion": "1.10.1", "ServerVersionRange": [ "2.6.3", "2.7.0" ], "ReleaseDate": "2024-04-05", "ReleaseNotes": "Corrected reported Inference device" },
7171
{ "ModuleVersion": "1.10.2", "ServerVersionRange": [ "2.6.5", "2.7.0" ], "ReleaseDate": "2024-02-26", "ReleaseNotes": "Corrections for backwards compatibility for 2.6.5" },
7272
{ "ModuleVersion": "1.11.0", "ServerVersionRange": [ "2.8.0", "2.9.0" ], "ReleaseDate": "2024-08-02", "ReleaseNotes": "Updated for server 2.8" },
73-
{ "ModuleVersion": "1.12.0", "ServerVersionRange": [ "2.9.1", "" ], "ReleaseDate": "2024-11-17", "ReleaseNotes": "Updated to .NET 9" }
73+
{ "ModuleVersion": "1.12.0", "ServerVersionRange": [ "2.9.1", "2.9.1" ], "ReleaseDate": "2024-11-17", "ReleaseNotes": "Updated to .NET 9" },
74+
{ "ModuleVersion": "1.13.0", "ServerVersionRange": [ "2.9.2", "" ], "ReleaseDate": "2024-11-24", "ReleaseNotes": "Updated to reflect updated CodeProject.AI SDK" }
7475
]
7576
},
7677

src/SDK/NET/API/ModuleResponses.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44

5+
using CodeProject.AI.SDK.Backend;
6+
57
namespace CodeProject.AI.SDK.API
68
{
79
public delegate Task<ModuleResponse> LongProcessMethod(BackendRequest request,

src/SDK/NET/API/ServerResponses.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System.Net;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
24

35
using CodeProject.AI.SDK.Modules;
6+
using CodeProject.AI.SDK.Client;
47
using CodeProject.AI.SDK.Common;
5-
using CodeProject.AI.SDK.Server;
6-
using System.Collections.Generic;
7-
using System;
88

99
namespace CodeProject.AI.SDK.API
1010
{

src/SDK/NET/Backend/BackendClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using System.Threading.Tasks;
1111
using Microsoft.Extensions.Logging;
1212

13-
namespace CodeProject.AI.SDK
13+
namespace CodeProject.AI.SDK.Backend
1414
{
1515
/// <summary>
1616
/// Represents an HTTP client for modules that gets requests and returns responses to the

src/SDK/NET/Backend/BackendRequests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Text.Json.Serialization;
33

4-
namespace CodeProject.AI.SDK
4+
using CodeProject.AI.SDK.Common;
5+
6+
namespace CodeProject.AI.SDK.Backend
57
{
68
#pragma warning disable IDE1006 // Naming Styles
79

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@
88
using System.Net.Http.Json;
99
using System.Threading;
1010
using System.Threading.Tasks;
11-
using CodeProject.AI.SDK.API;
12-
using CodeProject.AI.SDK.Utils;
1311

1412
using Microsoft.Extensions.Configuration;
1513
using Microsoft.Extensions.Hosting;
1614
using Microsoft.Extensions.Logging;
1715

18-
namespace CodeProject.AI.SDK
16+
using CodeProject.AI.SDK.API;
17+
using CodeProject.AI.SDK.Utils;
18+
19+
namespace CodeProject.AI.SDK.Backend
1920
{
2021
/// <summary>
2122
/// The base class from which a module should be derived.
2223
/// </summary>
23-
public abstract class ModuleWorkerBase : BackgroundService
24+
public abstract class ModuleRunnerBase : BackgroundService
2425
{
25-
private readonly string[] _doNotLogCommands = {
26+
private readonly string[] _doNotLogCommands = [
2627
"list-custom",
2728
"get_module_status", "status", "get_status", // status is deprecated alias
2829
"get_command_status"
29-
};
30+
];
3031

3132
private readonly TimeSpan _status_delay = TimeSpan.FromSeconds(2);
3233

@@ -61,7 +62,7 @@ public abstract class ModuleWorkerBase : BackgroundService
6162
/// <summary>
6263
/// Gets or sets the path to this Module
6364
/// </summary>
64-
public string? moduleDirPath { get; set; }
65+
public string? ModuleDirPath { get; set; }
6566

6667
/// <summary>
6768
/// Gets or sets a value indicating whether or not this module supports GPU acceleration
@@ -95,13 +96,13 @@ public abstract class ModuleWorkerBase : BackgroundService
9596
/// <param name="logger">The Logger.</param>
9697
/// <param name="configuration">The app configuration values.</param>
9798
/// <param name="hostApplicationLifetime">The applicationLifetime object</param>
98-
public ModuleWorkerBase(ILogger logger, IConfiguration configuration,
99+
public ModuleRunnerBase(ILogger logger, IConfiguration configuration,
99100
IHostApplicationLifetime hostApplicationLifetime)
100101
{
101102
// _logger = logger;
102103

103104
using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
104-
_logger = factory.CreateLogger<ModuleWorkerBase>();
105+
_logger = factory.CreateLogger<ModuleRunnerBase>();
105106

106107
_cancelled = false;
107108
_appLifetime = hostApplicationLifetime;
@@ -112,23 +113,29 @@ public ModuleWorkerBase(ILogger logger, IConfiguration configuration,
112113
// TODO: We could load the settings directly from the modulesettings files rather than rely
113114
// rely environment variables:
114115
/*
115-
// Load up the module's settings and start the module
116-
var config = new ConfigurationBuilder();
117-
config.AddModuleSettingsConfigFiles(moduleDirPath, false);
118-
IConfiguration configuration = config.Build();
116+
// Load up the modulesettings.*.json files
117+
var configBuilder = new IConfigurationBuilder();
118+
configBuilder.AddModuleSettingsConfigFiles(ModuleDirPath, false);
119+
IConfiguration config = configBuilder.Build();
119120
120121
// Bind the values in the configuration to a ModuleConfig object
121-
string moduleId = [get module id from config]
122122
var moduleConfig = new ModuleConfig();
123-
configuration.Bind($"Modules:{moduleId}", moduleConfig);
123+
try
124+
{
125+
config.Bind($"Modules:{moduleId}", moduleConfig);
126+
}
127+
catch (Exception)
128+
{
129+
moduleConfig = null;
130+
}
124131
125132
// Complete the ModuleConfig's setup.
126-
if (moduleConfig.Initialise(moduleId, moduleDirPath, ModuleLocation.Internal))
133+
if (moduleConfig.Initialise(moduleId, ModuleDirPath, ModuleLocation.Internal))
127134
*/
128135

129136
_moduleId = configuration.GetValue<string?>("CPAI_MODULE_ID", null) ?? currentDirName;
130137
ModuleName = configuration.GetValue<string?>("CPAI_MODULE_NAME", null) ?? _moduleId;
131-
moduleDirPath = configuration.GetValue<string?>("CPAI_MODULE_PATH", null) ?? currentModuleDirPath;
138+
ModuleDirPath = configuration.GetValue<string?>("CPAI_MODULE_PATH", null) ?? currentModuleDirPath;
132139
_queueName = configuration.GetValue<string?>("CPAI_MODULE_QUEUENAME", null) ?? _moduleId.ToLower() + "_queue";
133140

134141
int port = configuration.GetValue<int>("CPAI_PORT", 32168);
@@ -638,8 +645,6 @@ protected string GetModuleDirectoryPath()
638645
string moduleDirPath = AppContext.BaseDirectory;
639646
DirectoryInfo? info = new DirectoryInfo(moduleDirPath);
640647

641-
// HACK: If we're running this server from the build output dir in dev environment
642-
// then the root path will be wrong.
643648
if (SystemInfo.IsDevelopmentCode)
644649
{
645650
while (info != null)
@@ -652,6 +657,11 @@ protected string GetModuleDirectoryPath()
652657
}
653658
}
654659
}
660+
else
661+
{
662+
if (info?.Name.ToLower() == "bin")
663+
info = info.Parent;
664+
}
655665

656666
if (info != null)
657667
return info.FullName;

0 commit comments

Comments
 (0)