From c94d90437a163894a866a4eb25478d3043498545 Mon Sep 17 00:00:00 2001 From: euquiq <31453004+euquiq@users.noreply.github.com> Date: Thu, 22 Jul 2021 16:26:14 -0300 Subject: [PATCH] small fixes / enhancements --- SynoAI/AIs/DeepStack/DeepStackAI.cs | 3 +- SynoAI/Controllers/CameraController.cs | 44 +++++++++++++++----------- SynoAI/Services/SnapshotManager.cs | 2 +- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/SynoAI/AIs/DeepStack/DeepStackAI.cs b/SynoAI/AIs/DeepStack/DeepStackAI.cs index 769e476..306b3a6 100644 --- a/SynoAI/AIs/DeepStack/DeepStackAI.cs +++ b/SynoAI/AIs/DeepStack/DeepStackAI.cs @@ -39,7 +39,8 @@ public async override Task> Process(ILogger logger, Ca DeepStackResponse deepStackResponse = await GetResponse(response); if (deepStackResponse.Success) { - IEnumerable predictions = deepStackResponse.Predictions.Where(x=> x.Confidence >= minConfidence).Select(x => new AIPrediction() + + IEnumerable predictions = deepStackResponse.Predictions.Where(x=> x.Confidence >= minConfidence).Select(x => new AIPrediction() { Confidence = x.Confidence * 100, Label = x.Label, diff --git a/SynoAI/Controllers/CameraController.cs b/SynoAI/Controllers/CameraController.cs index e6e39a5..3b5c972 100644 --- a/SynoAI/Controllers/CameraController.cs +++ b/SynoAI/Controllers/CameraController.cs @@ -73,12 +73,16 @@ public async void Get(string id) // Take the snapshot from Surveillance Station byte[] snapshot = await GetSnapshot(id); _logger.LogInformation($"Snapshot {snapshotCount} of {Config.MaxSnapshots} received at EVENT TIME {overallStopwatch.ElapsedMilliseconds}ms."); + + //See if the image needs to be rotated (or further processing in the future ?) previous to being analyzed by AI snapshot = PreProcessSnapshot(camera, snapshot); // Use the AI to get the valid predictions and then get all the valid predictions, which are all the AI predictions where the result from the AI is // in the list of types and where the size of the object is bigger than the defined value. IEnumerable predictions = await GetAIPredications(camera, snapshot); + _logger.LogInformation($"Snapshot {snapshotCount} of {Config.MaxSnapshots} processed {predictions.Count()} objects at EVENT TIME {overallStopwatch.ElapsedMilliseconds}ms."); + if (predictions != null) { IEnumerable validPredictions = predictions.Where(x => @@ -88,6 +92,11 @@ public async void Get(string id) if (validPredictions.Count() > 0) { + + // Because we don't want to process the image if it isn't even required, then we pass the snapshot manager to the notifiers. It will then perform + // the necessary actions when it's GetImage method is called. + SnapshotManager snapshotManager = new SnapshotManager(snapshot, predictions, validPredictions, _snapshotManagerLogger); + // Save the original unprocessed image if required if (Config.SaveOriginalSnapshot) { @@ -95,10 +104,6 @@ public async void Get(string id) SnapshotManager.SaveOriginalImage(_logger, camera, snapshot); } - // Because we don't want to process the image if it isn't even required, then we pass the snapshot manager to the notifiers. It will then perform - // the necessary actions when it's GetImage method is called. - SnapshotManager snapshotManager = new SnapshotManager(snapshot, predictions, validPredictions, _snapshotManagerLogger); - // Generate text for notifications IList labels = new List(); @@ -170,23 +175,27 @@ public async void Get(string id) /// A byte array of the image. private byte[] PreProcessSnapshot(Camera camera, byte[] snapshot) { - if (camera.Rotate == 0) + if (camera.Rotate != 0) { - return snapshot; - } + Stopwatch stopwatch = Stopwatch.StartNew(); - Stopwatch stopwatch = Stopwatch.StartNew(); + // Load the bitmap & rotate the image + //SKBitmap bitmap = SKBitmap.Decode(new MemoryStream(snapshot)); + SKBitmap bitmap = SKBitmap.Decode(snapshot); - // Load the bitmap & rotate the image - SKBitmap bitmap = SKBitmap.Decode(new MemoryStream(snapshot)); - _logger.LogInformation($"{camera.Name}: Rotating image {camera.Rotate} degrees."); - bitmap = Rotate(bitmap, camera.Rotate); + _logger.LogInformation($"{camera.Name}: Rotating image {camera.Rotate} degrees."); + bitmap = Rotate(bitmap, camera.Rotate); - using (SKPixmap pixmap = bitmap.PeekPixels()) - using (SKData data = pixmap.Encode(SKEncodedImageFormat.Jpeg, 100)) - { - _logger.LogInformation($"{camera.Name}: Image preprocessing complete ({stopwatch.ElapsedMilliseconds}ms)."); - return data.ToArray(); + using (SKPixmap pixmap = bitmap.PeekPixels()) + using (SKData data = pixmap.Encode(SKEncodedImageFormat.Jpeg, 100)) + { + _logger.LogInformation($"{camera.Name}: Image preprocessing complete ({stopwatch.ElapsedMilliseconds}ms)."); + return data.ToArray(); + } + } + else + { + return snapshot; } } @@ -273,7 +282,6 @@ private async Task> GetAIPredications(Camera camera, b if (predictions == null) { _logger.LogError($"{camera}: Failed to get get predictions."); - return null; } else if (_logger.IsEnabled(LogLevel.Information)) { diff --git a/SynoAI/Services/SnapshotManager.cs b/SynoAI/Services/SnapshotManager.cs index 2909127..3053cb5 100644 --- a/SynoAI/Services/SnapshotManager.cs +++ b/SynoAI/Services/SnapshotManager.cs @@ -82,7 +82,7 @@ private SKBitmap ProcessImage(Camera camera) _logger.LogInformation($"{camera.Name}: Processing image boundaries."); // Load the bitmap - SKBitmap image = SKBitmap.Decode(new MemoryStream(_snapshot)); + SKBitmap image = SKBitmap.Decode(_snapshot); // Don't process the drawing if the drawing mode is off if (Config.DrawMode == DrawMode.Off)