private static Message CreateMessage(EmptyPlateRead read, string quality) { var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(read)); var message = new Message(bytes) { ContentType = "application/json" }; message.UserProperties["ReadQuality"] = quality; return(message); }
public async Task <Message> Run( [BlobTrigger("car-images/cameras/{camera}/{name}", Connection = "SECCTRL_CAR_IMAGES")] CloudBlockBlob imageBlob, string camera, string name, ILogger log, [OrchestrationClient] DurableOrchestrationClient orchestrationClient) { log.LogInformation($"Start processing of new image {name} from camera {camera}"); // Store timestamp when image was received in blob storage var timestamp = imageBlob.Properties.LastModified; // Read image file from blob storage and convert content to Base64 log.LogInformation("Downloading image data"); var file = new byte[imageBlob.Properties.Length]; await imageBlob.DownloadToByteArrayAsync(file, 0); // Try to recognize the license plate log.LogInformation("Starting license plate recognition"); var recognitionResult = await licensePlateRecognizer.RecognizeAsync(file, configuration); // Move image to archive log.LogInformation("Moving image to archive folder"); var archiveImageId = Guid.NewGuid().ToString(); var archiveBlob = imageBlob.Container.GetBlockBlobReference($"archive/{archiveImageId}.jpg"); await archiveBlob.StartCopyAsync(imageBlob); await imageBlob.DeleteAsync(); log.LogInformation("Building plate read result"); if (recognitionResult != null) { // We have recognized a license plate var read = new PlateRead { ReadTimestamp = timestamp.Value.Ticks, CameraID = camera, LicensePlate = recognitionResult.Plate, Confidence = recognitionResult.Confidence, Nationality = recognitionResult.Region, NationalityConfidence = recognitionResult.RegionConfidence, ImageID = archiveImageId }; var readQuality = "low"; if (recognitionResult.Confidence >= 75d && recognitionResult.RegionConfidence >= 25d) { readQuality = "high"; } if (readQuality == "low") { var instanceId = await orchestrationClient.StartNewAsync( "OrchestrateRequestApproval", new PlateReadApproval { Read = read }); log.LogInformation($"Durable Function Ochestration started: {instanceId}"); } return(CreateMessage(read, readQuality)); } else { // No license plate found var read = new EmptyPlateRead { ReadTimestamp = timestamp.Value.Ticks, CameraID = camera, ImageID = archiveImageId }; return(CreateMessage(read, "empty")); } }