Commit: aa34d60
Parent: fa0e37b

Non blocking camera stopping

Mårten Åsberg committed on 2026-06-10 at 15:27
src/Integrations/FaceCamera/FaceDetectionService.cs +11 -3
diff --git a/src/Integrations/FaceCamera/FaceDetectionService.cs b/src/Integrations/FaceCamera/FaceDetectionService.cs
index 93ae5e8..e010e4d 100644
@@ -26,6 +26,7 @@ public sealed class FaceDetectionService : IDisposable
private CaptureDevice? videoDevice;
private readonly InferenceSession session;
private readonly DenseTensor<float> tensor;
private readonly TimeSpan interval;
private DateTimeOffset nextCheckTime;
@@ -67,8 +68,15 @@ public sealed class FaceDetectionService : IDisposable
faceDetected -= value;
if (faceDetected is null)
{
videoDevice?.StopAsync().GetAwaiter().GetResult();
videoDevice?.Dispose();
if (videoDevice is { } vd)
{
vd.StopAsync()
.ContinueWith(_ =>
{
vd.Dispose();
logger.LogInformation("Video capture stopped");
});
}
}
}
}
@@ -96,6 +104,7 @@ public sealed class FaceDetectionService : IDisposable
);
session = new(options.Value.Model);
tensor = new DenseTensor<float>([1, 3, targetSize.Height, targetSize.Width]);
}
private void OnPixelBufferArrived(PixelBufferScope bufferScope)
@@ -106,7 +115,6 @@ public sealed class FaceDetectionService : IDisposable
}
var data = bufferScope.Buffer.ReferImage();
var tensor = new DenseTensor<float>([1, 3, targetSize.Height, targetSize.Width]);
using (var image = new Image<Rgb24>(videoSize.Width, videoSize.Height))
{
var pixels = new ArraySegment<byte>(data.Array!, data.Offset + 54, videoSize.Width * videoSize.Height * 2);