-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
208 lines (176 loc) · 6.6 KB
/
Copy pathProgram.cs
File metadata and controls
208 lines (176 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using CapCognition.Net.BarcodeScanning.Processor;
using CapCognition.Net.CaptureSources;
using CapCognition.Net.Core.Capture;
using CapCognition.Net.LicensePlateDetection.Processor;
using CapCognition.Net.YoloModelDetection.Processor;
using CapCognitionNetLTS_Samples.OwnProcessor;
using SkiaSharp;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace CapCognitionNetLTS_Samples;
public class Program
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
PrintUsage();
return;
}
var loggingFactory = BuildLoggerFactory();
InitializeCapCognition(loggingFactory);
var recognitionResultPath = GetRecognitionResultPath();
var command = args[0].ToLowerInvariant();
switch (command)
{
case "recognize":
RunFileRecognition(recognitionResultPath);
break;
case "stream":
RunRtspStream(args);
break;
case "streamhls":
RunStreamHls(args);
break;
case "ownrecognizer":
RunOwnRecognizer(recognitionResultPath);
break;
case "yolomodel":
RunYoloModel(recognitionResultPath);
break;
default:
PrintUsage();
break;
}
}
private static ILoggerFactory BuildLoggerFactory()
{
return LoggerFactory.Create(builder =>
{
builder.SetMinimumLevel(LogLevel.Debug);
builder.AddConsole();
builder.AddDebug();
});
}
private static void InitializeCapCognition(ILoggerFactory loggingFactory)
{
CapCognition.Net.Core.CapCognition.Initialize(
loggingFactory,
[
VideoStreamCapturing.Use(/* Here comes your license */),
BarcodeRecognition.Use(/* Here comes your license */),
LicensePlateDetection.Use(/* Here comes your license */),
YoloModelDetection.Use(/* Here comes your license */),
YourOwnRecognizer.Use(/* Here comes your license */),
]);
CapCognition.Net.Core.CapCognition.EnableImageProcessingLogs = true;
CaptureControl.Instance.InitializeAsync().GetAwaiter().GetResult();
}
private static string GetRecognitionResultPath()
{
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
return Path.Combine(assemblyDirectory, "Resources");
}
private static void RunFileRecognition(string recognitionResultPath)
{
foreach (var file in Directory.GetFiles(recognitionResultPath))
{
if (file.Contains("_result.png"))
{
continue;
}
new FileRecognitionDemo().Recognize(
recognizePath: file,
useBarcodeDetection: true,
useLicensePlateDetection: true,
useParallelProcessing: true,
downloadModelsFromInternet: false);
}
}
private static void RunRtspStream(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Please provide the rtsp url");
return;
}
var url = args[1];
new StreamRecognitionDemo().Stream(url);
}
private static void RunStreamHls(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Please provide the rtsp url");
return;
}
var url = args[1];
var streamingDemo = new StreamHLSDemo();
//create http streaming host
var httpStreamingHost = new HttpStreamingHostDemo(streamingDemo.PublicRoutePath);
httpStreamingHost.Start(args);
//start streaming
streamingDemo.Stream(url);
//give stream time to start -> can be optimized with better java script while loading the playlist
Thread.Sleep(TimeSpan.FromSeconds(5));
//open demo web page in browser
var demoWebPage = "http://localhost:5000/HLSDisplay.html";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo(demoWebPage) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", demoWebPage);
}
else
{
Console.WriteLine("Cannot determine OS to open browser.");
}
//run for 30 seconds
Thread.Sleep(TimeSpan.FromSeconds(30));
//stop streaming
streamingDemo.StopStream();
}
private static void RunOwnRecognizer(string recognitionResultPath)
{
foreach (var file in Directory.GetFiles(recognitionResultPath))
{
if (file.Contains("_result.png"))
{
continue;
}
new OwnProcessorDemo().Recognize(file);
}
}
private static void RunYoloModel(string recognitionResultPath)
{
foreach (var file in Directory.GetFiles(recognitionResultPath))
{
if (file.Contains("_result.png"))
{
continue;
}
var bitmap = SKBitmap.Decode(file);
var demo = new YoloModelDemo();
//prepare processor with the image, so that the model is loaded before processing the next images
//because the images can have different sizes, the processor needs to be prepared with an image before recognition, so that the model is loaded and ready for the next images.
//If the images have the same size, the processor can be prepared with the first image and then all images can be processed without preparing the processor again.
demo.PrepareProcessor(bitmap);
demo.Recognize(recognitionResultPath, Path.GetFileNameWithoutExtension(file), bitmap);
//disposing demo, so models are unloaded and resources are freed
demo.Dispose();
bitmap.Dispose();
}
}
private static void PrintUsage()
{
Console.WriteLine("Unknown or missing command");
Console.WriteLine("Usage:");
Console.WriteLine(" recognize Process all images in the Resources folder");
Console.WriteLine(" stream <rtspUrl> Process RTSP stream");
Console.WriteLine(" streamHLS <rtspUrl> Process RTSP stream and expose as HLS");
Console.WriteLine(" ownRecognizer Run OwnProcessor demo on the Resources folder");
}
}