Beispiel #1
0
        public async Task <Message> Run(
            [BlobTrigger("car-images/cameras/{camera}/{name}", Connection = "SECCTRL_CAR_IMAGES")] CloudBlockBlob imageBlob,
            string camera,
            string name,
            ILogger log,
            [DurableClient] IDurableOrchestrationClient 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"));
            }
        }
        private static void SetEmailContents(string FileName, int intAttachmentId, string strFullFilePath, string ConnectionString, ref DTOTaskDetail objDTOTaskDetail)
        {
            GeneralSettings objGeneralSettings = new GeneralSettings(ConnectionString);

            if (objGeneralSettings.StorageFileType == "AzureStorage")
            {
                CloudStorageAccount storageAccount     = null;
                CloudBlobContainer  cloudBlobContainer = null;

                // Retrieve the connection string for use with the application.
                string storageConnectionString = objGeneralSettings.AzureStorageConnection;

                // Check whether the connection string can be parsed.
                if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
                {
                    // Ensure there is a AdefHelpDesk Container
                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                    cloudBlobContainer = cloudBlobClient.GetContainerReference("adefhelpdesk-files");
                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(FileName);

                    // Download
                    cloudBlockBlob.FetchAttributesAsync().Wait();
                    long   fileByteLength = cloudBlockBlob.Properties.Length;
                    byte[] fileContent    = new byte[fileByteLength];
                    for (int i = 0; i < fileByteLength; i++)
                    {
                        fileContent[i] = 0x20;
                    }

                    cloudBlockBlob.DownloadToByteArrayAsync(fileContent, 0).Wait();

                    using (MemoryStream memstream = new MemoryStream(fileContent))
                    {
                        var message = MimeMessage.Load(memstream);

                        var visitor = new HtmlPreviewVisitor();
                        message.Accept(visitor);
                        objDTOTaskDetail.emailDescription = Utility.CleanOutlookFontDefinitions(visitor.HtmlBody);

                        // Add attachments (if any)
                        objDTOTaskDetail.colDTOAttachment = new List <DTOAttachment>();

                        foreach (var item in visitor.Attachments)
                        {
                            if (item.ContentDisposition.FileName != null)
                            {
                                DTOAttachment objDTOAttachment = new DTOAttachment();

                                objDTOAttachment.attachmentID     = intAttachmentId;
                                objDTOAttachment.attachmentPath   = "EML";
                                objDTOAttachment.userId           = "-1";
                                objDTOAttachment.fileName         = item.ContentDisposition.FileName;
                                objDTOAttachment.originalFileName = strFullFilePath;

                                objDTOTaskDetail.colDTOAttachment.Add(objDTOAttachment);
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("AzureStorage configured but AzureStorageConnection value cannot connect.");
                }
            }
            else
            {
                using (var fileContents = System.IO.File.OpenRead(strFullFilePath))
                {
                    var message = MimeMessage.Load(fileContents);

                    var visitor = new HtmlPreviewVisitor();
                    message.Accept(visitor);
                    objDTOTaskDetail.emailDescription = Utility.CleanOutlookFontDefinitions(visitor.HtmlBody);

                    // Add attachments (if any)
                    objDTOTaskDetail.colDTOAttachment = new List <DTOAttachment>();

                    foreach (var item in visitor.Attachments)
                    {
                        if (item.ContentDisposition.FileName != null)
                        {
                            DTOAttachment objDTOAttachment = new DTOAttachment();

                            objDTOAttachment.attachmentID     = intAttachmentId;
                            objDTOAttachment.attachmentPath   = "EML";
                            objDTOAttachment.userId           = "-1";
                            objDTOAttachment.fileName         = item.ContentDisposition.FileName;
                            objDTOAttachment.originalFileName = strFullFilePath;

                            objDTOTaskDetail.colDTOAttachment.Add(objDTOAttachment);
                        }
                    }
                }
            }
        }
Beispiel #3
0
 public Task <int> DownloadToByteArrayAsync(byte[] target, int index, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return(blockBlob.DownloadToByteArrayAsync(target, index, accessCondition, options, operationContext, cancellationToken));
 }
Beispiel #4
0
 /// <summary>
 /// Download data to the stream.
 /// </summary>
 /// <param name="blob">The cloud blob.</param>
 /// <param name="target">The byte array where to write to.</param>
 /// <param name="index">The offset index.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The async task.</returns>
 public async Task <int> DownloadAsync(CloudBlockBlob blob, byte[] target, int index, CancellationToken cancellationToken)
 {
     return(await blob.DownloadToByteArrayAsync(target, index, cancellationToken));
 }
Beispiel #5
0
 /// <summary>
 /// Download data to the stream.
 /// </summary>
 /// <param name="blob">The cloud blob.</param>
 /// <param name="target">The byte array where to write to.</param>
 /// <param name="index">The offset index.</param>
 /// <returns>The async task.</returns>
 public async Task <int> DownloadAsync(CloudBlockBlob blob, byte[] target, int index)
 {
     return(await blob.DownloadToByteArrayAsync(target, index));
 }
Beispiel #6
0
        public async Task <IActionResult> imageResultAsync()
        {
            StorageCredentials storageCredentials = new StorageCredentials(_storage_config.AccountName, _storage_config.AccountKey);
            // Create cloudstorage account by passing the storagecredentials
            CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
            CloudBlobContainer    container       = blobClient.GetContainerReference(_storage_config.ImageContainer);
            CloudBlobContainer    d_container     = blobClient.GetContainerReference(_storage_config.DetectedContainer);
            List <MessageModel>   messageModels   = _db_context.entry.ToList();
            List <ImageViewModel> imageViewModels = new List <ImageViewModel>();

            foreach (MessageModel m in messageModels)
            {
                CloudBlockBlob c1 = container.GetBlockBlobReference(m.filename1);
                CloudBlockBlob c2 = container.GetBlockBlobReference(m.filename2);
                CloudBlockBlob c3 = null;
                try
                {
                    c3 = d_container.GetBlockBlobReference(m.guid);
                }
                catch
                {
                    c3 = null;
                }
                await c1.FetchAttributesAsync();

                long   fileByteLength = c1.Properties.Length;
                byte[] im1            = new byte[fileByteLength];
                await c1.DownloadToByteArrayAsync(im1, 0);

                await c2.FetchAttributesAsync();

                long   fileByteLength2 = c2.Properties.Length;
                byte[] im2             = new byte[fileByteLength2];
                await c2.DownloadToByteArrayAsync(im2, 0);

                byte[] im3;
                long   fileByteLength3 = 0;
                if (c3 != null)
                {
                    try
                    {
                        await c3.FetchAttributesAsync();

                        fileByteLength3 = c3.Properties.Length;
                    }
                    catch (Exception e)
                    {
                        fileByteLength3 = 0;
                        Exception e1 = e;
                    }
                }

                im3 = new byte[fileByteLength3];
                if (fileByteLength3 > 0)
                {
                    await c3.DownloadToByteArrayAsync(im3, 0);
                }
                ImageViewModel imageView = new ImageViewModel()
                {
                    image1 = "data:image; base64," + Convert.ToBase64String(im1), image2 = "data:image; base64," + Convert.ToBase64String(im2)
                };
                if (fileByteLength3 > 0)
                {
                    imageView.result = "data:image; base64," + Convert.ToBase64String(im3);
                }
                imageViewModels.Add(imageView);
            }
            ViewBag.images = imageViewModels;
            return(View());
        }