The Azure.EventGrid.Image.Detect application downloads images from a security camera, processes them with the default YoloV8(by Ultralytic) object detection model, then publishes the results to an Azure Event Grid MQTT broker topic.
The Unv ADZK-10 camera used in this sample has a Hypertext Transfer Protocol (HTTP) Uniform Resource Locator(URL) for downloading the current image. Like the YoloV8.Detect.SecurityCamera.Stream sample the image “streamed” using the HttpClient.GetStreamAsync to the YoloV8 DetectAsync method.
private async void ImageUpdateTimerCallback(object? state)
{
DateTime requestAtUtc = DateTime.UtcNow;
// Just incase - stop code being called while photo or prediction already in progress
if (_ImageProcessing)
{
return;
}
_ImageProcessing = true;
try
{
_logger.LogDebug("Camera request start");
DetectionResult result;
using (Stream cameraStream = await _httpClient.GetStreamAsync(_applicationSettings.CameraUrl))
{
result = await _predictor.DetectAsync(cameraStream);
}
_logger.LogInformation("Speed Preprocess:{Preprocess} Postprocess:{Postprocess}", result.Speed.Preprocess, result.Speed.Postprocess);
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Detection results");
foreach (var box in result.Boxes)
{
_logger.LogDebug(" Class {box.Class} {Confidence:f1}% X:{box.Bounds.X} Y:{box.Bounds.Y} Width:{box.Bounds.Width} Height:{box.Bounds.Height}", box.Class, box.Confidence * 100.0, box.Bounds.X, box.Bounds.Y, box.Bounds.Width, box.Bounds.Height);
}
}
var message = new MQTT5PublishMessage
{
Topic = string.Format(_applicationSettings.PublishTopic, _applicationSettings.UserName),
Payload = Encoding.ASCII.GetBytes(JsonSerializer.Serialize(new
{
result.Boxes,
})),
QoS = _applicationSettings.PublishQualityOfService,
};
_logger.LogDebug("HiveMQ.Publish start");
var resultPublish = await _mqttclient.PublishAsync(message);
_logger.LogDebug("HiveMQ.Publish done");
}
catch (Exception ex)
{
_logger.LogError(ex, "Camera image download, processing, or telemetry failed");
}
finally
{
_ImageProcessing = false;
}
TimeSpan duration = DateTime.UtcNow - requestAtUtc;
_logger.LogDebug("Camera Image download, processing and telemetry done {TotalSeconds:f2} sec", duration.TotalSeconds);
}
The application uses a Timer(with configurable Due and Period times) to poll the security camera, detect objects in the image then publish a JavaScript Object Notation(JSON) representation of the results to Azure Event Grid MQTT broker topic using a HiveMQ client.
The uses the Microsoft.Extensions.Logging library to publish diagnostic information to the console while debugging the application.
To check the results I put a breakpoint in the timer just after DetectAsync method is called and then used the Visual Studio 2022 Debugger QuickWatch functionality to inspect the contents of the DetectionResult object.
To check the JSON payload of the MQTT message I put a breakpoint just before the HiveMQ PublishAsync method. I then inspected the payload using the Visual Studio 2022 JSON Visualizer.
This application can also be deployed as a Linux systemd Service so it will start then run in the background. The same approach as the YoloV8.Detect.SecurityCamera.Stream sample is used because the image doesn’t have to be saved on the local filesystem.