Skip to content

Commit

Permalink
small fixes / enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
euquiq committed Jul 22, 2021
1 parent 315e4eb commit 5b4658c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
3 changes: 2 additions & 1 deletion SynoAI/AIs/DeepStack/DeepStackAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public async override Task<IEnumerable<AIPrediction>> Process(ILogger logger, Ca
DeepStackResponse deepStackResponse = await GetResponse(response);
if (deepStackResponse.Success)
{
IEnumerable<AIPrediction> predictions = deepStackResponse.Predictions.Where(x=> x.Confidence >= minConfidence).Select(x => new AIPrediction()

IEnumerable<AIPrediction> predictions = deepStackResponse.Predictions.Where(x=> x.Confidence >= minConfidence).Select(x => new AIPrediction()
{
Confidence = x.Confidence * 100,
Label = x.Label,
Expand Down
44 changes: 26 additions & 18 deletions SynoAI/Controllers/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AIPrediction> 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<AIPrediction> validPredictions = predictions.Where(x =>
Expand All @@ -88,17 +92,18 @@ 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)
{
_logger.LogInformation($"{id}: Saving original image");
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<String> labels = new List<String>();

Expand Down Expand Up @@ -157,23 +162,27 @@ public async void Get(string id)
/// <returns>A byte array of the image.</returns>
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;
}
}

Expand Down Expand Up @@ -260,7 +269,6 @@ private async Task<IEnumerable<AIPrediction>> GetAIPredications(Camera camera, b
if (predictions == null)
{
_logger.LogError($"{camera}: Failed to get get predictions.");
return null;
}
else if (_logger.IsEnabled(LogLevel.Information))
{
Expand Down
2 changes: 1 addition & 1 deletion SynoAI/Services/SnapshotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5b4658c

Please sign in to comment.