@@ -32,8 +32,8 @@ public class CameraController : ControllerBase
32
32
private readonly ISynologyService _synologyService ;
33
33
private readonly ILogger < CameraController > _logger ;
34
34
35
- private static ConcurrentDictionary < string , bool > _runningCameraChecks = new ConcurrentDictionary < string , bool > ( StringComparer . OrdinalIgnoreCase ) ;
36
- private static ConcurrentDictionary < string , DateTime > _delayedCameraChecks = new ConcurrentDictionary < string , DateTime > ( StringComparer . OrdinalIgnoreCase ) ;
35
+ private static ConcurrentDictionary < string , bool > _runningCameraChecks = new ( StringComparer . OrdinalIgnoreCase ) ;
36
+ private static ConcurrentDictionary < string , DateTime > _delayedCameraChecks = new ( StringComparer . OrdinalIgnoreCase ) ;
37
37
38
38
public CameraController ( IAIService aiService , ISynologyService synologyService , ILogger < CameraController > logger , IHubContext < SynoAIHub > hubContext )
39
39
{
@@ -134,7 +134,7 @@ public async void Get(string id)
134
134
int maxSizeX = camera . GetMaxSizeX ( ) ;
135
135
int maxSizeY = camera . GetMaxSizeY ( ) ;
136
136
137
- List < AIPrediction > validPredictions = new List < AIPrediction > ( ) ;
137
+ List < AIPrediction > validPredictions = new ( ) ;
138
138
foreach ( AIPrediction prediction in predictions )
139
139
{
140
140
// Check if the prediction label is in the list of types the camera is looking for
@@ -178,14 +178,17 @@ public async void Get(string id)
178
178
179
179
if ( validPredictions . Count ( ) > 0 )
180
180
{
181
- // Generate text for notifications
182
- IEnumerable < string > labels = GetLabels ( validPredictions ) ;
183
-
184
181
// Process and save the snapshot
185
182
ProcessedImage processedImage = SnapshotManager . DressImage ( camera , snapshot , predictions , validPredictions , _logger ) ;
186
183
187
184
// Send Notifications
188
- await SendNotifications ( camera , processedImage , labels ) ;
185
+ Notification notification = new ( )
186
+ {
187
+ ProcessedImage = processedImage ,
188
+ ValidPredictions = validPredictions
189
+ } ;
190
+
191
+ await SendNotifications ( camera , notification ) ;
189
192
190
193
// Inform eventual web users about this new Snapshot, for the "realtime" option thru Web
191
194
await _hubContext . Clients . All . SendAsync ( "ReceiveSnapshot" , camera . Name , processedImage . FileName ) ;
@@ -239,10 +242,10 @@ private bool ShouldIncludePrediction(string id, Camera camera, Stopwatch overall
239
242
// Check if the prediction falls within the exclusion zones
240
243
if ( camera . Exclusions != null && camera . Exclusions . Count ( ) > 0 )
241
244
{
242
- Rectangle boundary = new Rectangle ( prediction . MinX , prediction . MinY , prediction . SizeX , prediction . SizeY ) ;
245
+ Rectangle boundary = new ( prediction . MinX , prediction . MinY , prediction . SizeX , prediction . SizeY ) ;
243
246
foreach ( Zone exclusion in camera . Exclusions )
244
247
{
245
- Rectangle exclusionZoneBoundary = new Rectangle ( exclusion . Start . X , exclusion . Start . Y , exclusion . End . X - exclusion . Start . X , exclusion . End . Y - exclusion . Start . Y ) ;
248
+ Rectangle exclusionZoneBoundary = new ( exclusion . Start . X , exclusion . Start . Y , exclusion . End . X - exclusion . Start . X , exclusion . End . Y - exclusion . Start . Y ) ;
246
249
bool exclude = exclusion . Mode == OverlapMode . Contains ? exclusionZoneBoundary . Contains ( boundary ) : exclusionZoneBoundary . IntersectsWith ( boundary ) ;
247
250
if ( exclude )
248
251
{
@@ -256,43 +259,6 @@ private bool ShouldIncludePrediction(string id, Camera camera, Stopwatch overall
256
259
return true ;
257
260
}
258
261
259
- /// <summary>
260
- /// Gets the labels from the predictions to use in the notifications.
261
- /// </summary>
262
- /// <param name="validPredictions">The predictions to process.</param>
263
- /// <returns>A list of labels.</returns>
264
- private IEnumerable < string > GetLabels ( IEnumerable < AIPrediction > validPredictions )
265
- {
266
- if ( Config . AlternativeLabelling && Config . DrawMode == DrawMode . Matches )
267
- {
268
- List < String > labels = new List < String > ( ) ;
269
- if ( validPredictions . Count ( ) == 1 )
270
- {
271
- // If there is only a single object, then don't add a correlating number and instead just
272
- // write out the label.
273
- decimal confidence = Math . Round ( validPredictions . First ( ) . Confidence , 0 , MidpointRounding . AwayFromZero ) ;
274
- labels . Add ( $ "{ validPredictions . First ( ) . Label . FirstCharToUpper ( ) } { confidence } %") ;
275
- }
276
- else
277
- {
278
- // Since there is more than one object detected, include correlating number
279
- int counter = 1 ;
280
- foreach ( AIPrediction prediction in validPredictions )
281
- {
282
- decimal confidence = Math . Round ( prediction . Confidence , 0 , MidpointRounding . AwayFromZero ) ;
283
- labels . Add ( $ "{ counter } . { prediction . Label . FirstCharToUpper ( ) } { confidence } %") ;
284
- counter ++ ;
285
- }
286
- }
287
-
288
- return labels ;
289
- }
290
- else
291
- {
292
- return validPredictions . Select ( x => x . Label . FirstCharToUpper ( ) ) . ToList ( ) ;
293
- }
294
- }
295
-
296
262
/// <summary>
297
263
/// Adds a delay for the specified camera.
298
264
/// </summary>
@@ -332,7 +298,7 @@ private void CleanupOldImages()
332
298
{
333
299
_cleanupOldImagesRunning = true ;
334
300
335
- DirectoryInfo directory = new DirectoryInfo ( Constants . DIRECTORY_CAPTURES ) ;
301
+ DirectoryInfo directory = new ( Constants . DIRECTORY_CAPTURES ) ;
336
302
IEnumerable < FileInfo > files = directory . GetFiles ( "*" , new EnumerationOptions ( ) { RecurseSubdirectories = true } ) ;
337
303
foreach ( FileInfo file in files )
338
304
{
@@ -350,7 +316,7 @@ private void CleanupOldImages()
350
316
}
351
317
}
352
318
private bool _cleanupOldImagesRunning ;
353
- private object _cleanUpOldImagesLock = new object ( ) ;
319
+ private object _cleanUpOldImagesLock = new ( ) ;
354
320
355
321
/// <summary>
356
322
/// Handles any required preprocessing of the captured image.
@@ -399,8 +365,8 @@ private SKBitmap Rotate(SKBitmap bitmap, double angle)
399
365
int rotatedWidth = ( int ) ( cosine * originalWidth + sine * originalHeight ) ;
400
366
int rotatedHeight = ( int ) ( cosine * originalHeight + sine * originalWidth ) ;
401
367
402
- SKBitmap rotatedBitmap = new SKBitmap ( rotatedWidth , rotatedHeight ) ;
403
- using ( SKCanvas canvas = new SKCanvas ( rotatedBitmap ) )
368
+ SKBitmap rotatedBitmap = new ( rotatedWidth , rotatedHeight ) ;
369
+ using ( SKCanvas canvas = new ( rotatedBitmap ) )
404
370
{
405
371
canvas . Clear ( ) ;
406
372
canvas . Translate ( rotatedWidth / 2 , rotatedHeight / 2 ) ;
@@ -417,22 +383,23 @@ private SKBitmap Rotate(SKBitmap bitmap, double angle)
417
383
/// Sends notifications, if there is any configured
418
384
/// </summary>
419
385
/// <param name="camera">The camera responsible for this snapshot.</param>
420
- /// <param name="processedImage">The path information for the snapshot.</param>
421
- /// <param name="labels">The text metadata for each existing valid object.</param>
422
- private async Task SendNotifications ( Camera camera , ProcessedImage processedImage , IEnumerable < string > labels )
386
+ /// <param name="notification">The notification data to process.</param>
387
+ private async Task SendNotifications ( Camera camera , Notification notification )
423
388
{
424
389
Stopwatch stopwatch = Stopwatch . StartNew ( ) ;
425
390
391
+ IEnumerable < string > labels = notification . ValidPredictions . Select ( x => x . Label ) . Distinct ( ) . ToList ( ) ;
392
+
426
393
IEnumerable < INotifier > notifiers = Config . Notifiers
427
394
. Where ( x =>
428
- ( x . Cameras == null || x . Cameras . Count ( ) == 0 || x . Cameras . Any ( c => c . Equals ( camera . Name , StringComparison . OrdinalIgnoreCase ) ) ) &&
429
- ( x . Types == null || x . Types . Count ( ) == 0 || x . Types . Any ( t => labels . Contains ( t , StringComparer . OrdinalIgnoreCase ) ) )
395
+ ( x . Cameras == null || ! x . Cameras . Any ( ) || x . Cameras . Any ( c => c . Equals ( camera . Name , StringComparison . OrdinalIgnoreCase ) ) ) &&
396
+ ( x . Types == null || ! x . Types . Any ( ) || x . Types . Any ( t => labels . Contains ( t , StringComparer . OrdinalIgnoreCase ) ) )
430
397
) . ToList ( ) ;
431
398
432
- List < Task > tasks = new List < Task > ( ) ;
399
+ List < Task > tasks = new ( ) ;
433
400
foreach ( INotifier notifier in notifiers )
434
401
{
435
- tasks . Add ( notifier . SendAsync ( camera , processedImage , labels , _logger ) ) ;
402
+ tasks . Add ( notifier . SendAsync ( camera , notification , _logger ) ) ;
436
403
}
437
404
438
405
await Task . WhenAll ( tasks ) ;
0 commit comments