private static PredictionResponse invokePrediction(byte[] imageBytes, ModuleClient moduleClient) { PredictionResponse predictionResponse = null; //used for latency calcualtion DateTime startTime = DateTime.UtcNow; ByteArrayContent imageContent = new ByteArrayContent(imageBytes); imageContent.Headers.Add("Content-Type", "application/octet-stream"); HttpResponseMessage httpResponse = null; try { httpResponse = httpClient.PostAsync(IMAGE_PROCESSING_ENDPOINT, imageContent).Result; Console.WriteLine("Image Classification endpoint called."); } catch (Exception ex) { Console.WriteLine($"Error calling IMAGE_PROCESSING_ENDPOINT: {IMAGE_PROCESSING_ENDPOINT}\n{ex.ToString()}"); return(null); } try { if (httpResponse.IsSuccessStatusCode) { Console.WriteLine($"HTTP call IsSuccessStatusCode: {httpResponse.StatusCode}"); string predictionResponseString = httpResponse.Content.ReadAsStringAsync().Result; //parse as JSON predictionResponse = JsonConvert.DeserializeObject <PredictionResponse>(predictionResponseString); } else { Console.WriteLine($"HTTP return code is NOT Success Status Code: {httpResponse.StatusCode}"); } } catch (Exception ex) { Console.WriteLine($"Error evaluating HTTP result\n{ex.ToString()}"); return(null); } TimeSpan endToEndDuration = DateTime.UtcNow - startTime; if (predictionResponse != null && reportingLoop) { reportLatency("INVOKE_PREDICTOR", endToEndDuration, moduleClient); } return(predictionResponse); }
private static void publishPredictionResponse(PredictionResponse predictionResponse, ModuleClient moduleClient) { string predictionString = JsonConvert.SerializeObject(predictionResponse); byte[] messageContent = System.Text.Encoding.UTF8.GetBytes(predictionString); Message message = new Message(messageContent); message.ContentType = "application/json"; message.Properties.Add("MSG", "PredictionResponse"); Console.WriteLine("Publishing Prediction Result to Module Output: predictions..."); moduleClient.SendEventAsync("predictions", message).Wait(); Console.WriteLine("Publishing Prediction Result to Module Output done"); }
private static void mainThreadBody(object userContext) { Console.WriteLine("Entering main thread"); while (true) { if (loopNumber == reportingLoopInterval) { Console.WriteLine("In this loop performance will be reported."); reportingLoop = true; loopNumber = 0; } else { reportingLoop = false; } var moduleClient = userContext as ModuleClient; if (moduleClient == null) { Console.WriteLine("Module Client is NULL."); throw new InvalidOperationException("UserContext doesn't contain " + "expected values"); } //used for polling calcualtion DateTime startTime = DateTime.UtcNow; //get the image byte[] imageBytes = getImage(moduleClient); if (imageBytes != null) { Console.WriteLine("Image Bytes not null... good job."); if (MODE == OperatingMode.ImageClassification) { Console.WriteLine("Calling Image Classification"); //call the Custom Vision Module PredictionResponse predictionResponse = invokePrediction(imageBytes, moduleClient); if (predictionResponse != null) { //publish the response message as output publishPredictionResponse(predictionResponse, moduleClient); TimeSpan endToEndDuration = DateTime.UtcNow - startTime; if (reportingLoop) { reportLatency("END_TO_END", endToEndDuration, moduleClient); } loopNumber++; } } if (CUSTOM_VISION_TRAINING.IsValid()) { Console.WriteLine("CUSTOM_VISION_TRAINING.IsValid()"); } else { Console.WriteLine("NOT CUSTOM_VISION_TRAINING.IsValid()"); } if ((MODE == OperatingMode.TrainingToCloud) && CUSTOM_VISION_TRAINING.IsValid()) { Console.WriteLine("OperatingMode TrainingToCloud and Custom Vision training data is provided"); if (customVisionTrainingClient == null) { Console.WriteLine("Creating CustomVisionTrainingClient."); customVisionTrainingClient = new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.CustomVisionTrainingClient() { ApiKey = CUSTOM_VISION_TRAINING.ApiKey, Endpoint = CUSTOM_VISION_TRAINING.EndPoint }; } //ok now just send an image to Custom Vision for training purpose MemoryStream imageMemoryStream = new MemoryStream(imageBytes); Console.WriteLine("Uploading the image into Custom Vision "); Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.ImageCreateSummary imageCreateSummary = customVisionTrainingClient.CreateImagesFromData(CUSTOM_VISION_TRAINING.ProjectId, imageMemoryStream); Console.WriteLine($"Success: {imageCreateSummary.IsBatchSuccessful}"); } } Console.WriteLine("Calculating time to next run"); TimeSpan loopDuration = DateTime.UtcNow - startTime; TimeSpan timeToNextRun = IMAGE_POLLING_INTERVAL - loopDuration; Console.WriteLine($"Time to next run: {timeToNextRun.ToString()}"); if (timeToNextRun > TimeSpan.FromSeconds(0.0)) { Console.WriteLine("Delay"); Task.Delay((int)timeToNextRun.TotalMilliseconds).Wait(); } } }