public async static Task Run([QueueTrigger("%ResizedImageQueueName%", Connection = "ResizedImageQueueStorageConnectionString")] string myQueueItem, TraceWriter log) { log.Info($"ML Query Request received : {myQueueItem}"); QueueNotificationImageUploaded notificationImageUploaded = JsonConvert.DeserializeObject <QueueNotificationImageUploaded>(myQueueItem); string trackingDbConnectionString = ConfigurationManager.AppSettings["TrackingDbConnectionString"]; //Update the result ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(trackingDbConnectionString); IDatabase database = connectionMultiplexer.GetDatabase(); TrackingInfo trackingInfo = JsonConvert.DeserializeObject <TrackingInfo>(await database.StringGetAsync(notificationImageUploaded.TrackingId.ToString())); //Update Status trackingInfo.SearchStatus = "Search Started"; await database.StringSetAsync(notificationImageUploaded.TrackingId.ToString(), JsonConvert.SerializeObject(trackingInfo)); string[] defaultImageList = { "0004/12000004fe.jpg", "0004/12010004pm.jpg", "0004/37760004mp.jpg" }; //Query the model string[] images = await QueryMLModel(log, notificationImageUploaded); images = images?.Length > 0 ? images : defaultImageList; //Update Results trackingInfo.SearchStatus = "Search Complete"; trackingInfo.ImageUrls = new List <string>(); foreach (string imageUrl in images) { trackingInfo.ImageUrls.Add(imageUrl); } await database.StringSetAsync(notificationImageUploaded.TrackingId.ToString(), JsonConvert.SerializeObject(trackingInfo)); }
private static async Task UpdateTrackingDatabase(QueueNotificationImageUploaded notificationImageUploaded, string trackingDbConnectionString) { //Update Azure Redis cache ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(trackingDbConnectionString); IDatabase database = connectionMultiplexer.GetDatabase(); TrackingInfo trackingInfo = JsonConvert.DeserializeObject <TrackingInfo>(await database.StringGetAsync(notificationImageUploaded.TrackingId.ToString())); trackingInfo.SearchStatus = "Image Resized"; await database.StringSetAsync(notificationImageUploaded.TrackingId.ToString(), JsonConvert.SerializeObject(trackingInfo)); }
private static async Task <string[]> QueryMLModel(TraceWriter log, QueueNotificationImageUploaded notificationImageUploaded) { string[] matchingImages = { }; try { HttpClient httpClient = new HttpClient(); string mlServiceEndpoint = ConfigurationManager.AppSettings["MLQueryServiceEndpoint"]; /* Only if the ML Service Endpoint is defined */ if (!String.IsNullOrEmpty(mlServiceEndpoint)) { HttpRequestMessage httpRequestMessage = new HttpRequestMessage { // Uri: http://52.178.119.58/api/v1/service/irisapp/score RequestUri = new Uri(ConfigurationManager.AppSettings["MLQueryServiceEndpoint"]), Method = HttpMethod.Post }; if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["MLQueryServiceAuthHeader"])) { httpRequestMessage.Headers.Add("Authorization", ConfigurationManager.AppSettings["MLQueryServiceAuthHeader"]); //httpRequestMessage.Headers.Add("Authorization", "Bearer 44a392fb3a6a4c30bfa5bc668c10508e"); } string httpBody = JsonConvert.SerializeObject(notificationImageUploaded.ImageFile); httpBody = "{\"input_df\": [{\"petal length\": 1.3, \"sepal length\": 3.0, \"sepal width\": 3.6, \"petal width\": 0.25}]}"; httpRequestMessage.Content = new StringContent(httpBody, null, "application/json"); httpRequestMessage.Content.Headers.Remove("Content-Type"); httpRequestMessage.Content.Headers.Add("Content-Type", "application/json"); HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage); string response = await httpResponseMessage.Content.ReadAsStringAsync(); } } catch (Exception ex) { log.Error("Error occured communicating with the ML Model Service.", ex); } return(matchingImages); }
private static async Task ResizeImage(QueueNotificationImageUploaded notificationImageUploaded, int configuredWidth, int configureHeight) { var instructions = new Instructions { Width = configuredWidth, Height = configureHeight, Mode = FitMode.Crop, Scale = ScaleMode.Both }; CloudStorageAccount inputStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["UploadedImageStorageConnectionString"]); CloudBlobClient inputBlobClient = inputStorageAccount.CreateCloudBlobClient(); CloudBlobContainer inputContainer = inputBlobClient.GetContainerReference(ConfigurationManager.AppSettings["UploadedImageContainer"]); CloudBlob inputBlob = inputContainer.GetBlobReference(notificationImageUploaded.ImageFile); if (inputBlob != null) { using (var inputBlobStream = inputBlob.OpenRead()) { CloudStorageAccount outputBlobStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["ResizedImageStorageConnectionString"]); CloudBlobClient outputBlobClient = outputBlobStorageAccount.CreateCloudBlobClient(); CloudBlobContainer outputContainer = outputBlobClient.GetContainerReference(ConfigurationManager.AppSettings["ResizedImageContainer"]); CloudBlockBlob outputBlockBlob = outputContainer.GetBlockBlobReference("rs_" + notificationImageUploaded.ImageFile); using (MemoryStream myStream = new MemoryStream()) { // Resize the image with the given instructions into the stream. ImageBuilder.Current.Build(new ImageJob(inputBlobStream, myStream, instructions)); // Reset the stream's position to the beginning. myStream.Position = 0; // Write the stream to the new blob. await outputBlockBlob.UploadFromStreamAsync(myStream); } } } }
public async static Task Run([QueueTrigger("%UploadedImagesQueueName%", Connection = "UploadedImagesQueueStorageConnectionString")] string myQueueItem, [Queue("%ResizedImageQueueName%", Connection = "ResizedImageQueueStorageConnectionString")] ICollector <QueueNotificationImageUploaded> outputQueue, TraceWriter log) { log.Info($"C# Queue trigger function processed: {myQueueItem}"); QueueNotificationImageUploaded notificationImageUploaded = JsonConvert.DeserializeObject <QueueNotificationImageUploaded>(myQueueItem); string trackingDbConnectionString = ConfigurationManager.AppSettings["TrackingDbConnectionString"]; int configuredWidth = 1571; int configureHeight = 2000; configuredWidth = int.TryParse(ConfigurationManager.AppSettings["PreferredWidth"], out configuredWidth) ? configuredWidth : 1571; configureHeight = int.TryParse(ConfigurationManager.AppSettings["PreferredHeight"], out configureHeight) ? configureHeight : 2000; await ResizeImage(notificationImageUploaded, configuredWidth, configureHeight); UpdateOutboundQueue(outputQueue, notificationImageUploaded); await UpdateTrackingDatabase(notificationImageUploaded, trackingDbConnectionString); }
public static async Task <bool> QueueMessageForProcessingImage(QueueNotificationImageUploaded notificationMessage, AzureStorageConfig queueStorageConfig) { // Create storagecredentials object by reading the values from the configuration (appsettings.json) StorageCredentials storageCredentials = new StorageCredentials(queueStorageConfig.AccountName, queueStorageConfig.AccountKey); // Create cloudstorage account by passing the storagecredentials CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); // Create the queue client. CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); // Retrieve a reference to a container. CloudQueue queue = queueClient.GetQueueReference(queueStorageConfig.QueueName); // Create the queue if it doesn't already exist await queue.CreateIfNotExistsAsync(); // Create a message and add it to the queue. CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject(notificationMessage)); await queue.AddMessageAsync(message); return(await Task.FromResult(true)); }
private static void UpdateOutboundQueue(ICollector <QueueNotificationImageUploaded> outputQueue, QueueNotificationImageUploaded notificationImageUploaded) { //update with resized filename notificationImageUploaded.ImageFile = "rs_" + notificationImageUploaded.ImageFile; outputQueue.Add(notificationImageUploaded); }