Example #1
0
        /// <summary>
        /// Uses Cognitive Services Custom Vision API to determine whether an object was found in an image
        /// with some degree of certainty
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <returns>Boolean</returns>
        private static async Task <bool> ObjectFound(string imageUrl, double minimumScore = 0.5)
        {
            bool objectFound = false;

            try
            {
                // Initialize prediction endpoint
                PredictionEndpoint predictionEndpoint = new PredictionEndpoint()
                {
                    ApiKey = ConfigurationManager.AppSettings["CustomVisionApiKey"]
                };

                // Call custom vision prediction API to predict image
                ImagePredictionResultModel predictionResult = await predictionEndpoint.PredictImageUrlAsync(
                    new Guid(ConfigurationManager.AppSettings["CustomVisionProjectId"]),
                    new ImageUrl(imageUrl));

                // Query for the object tag
                var objectTag = predictionResult.Predictions.Where(x => x.Tag == "Object").FirstOrDefault();

                // Check if the object tag probability surpassed the minimum score
                if (objectTag != null && objectTag.Probability >= minimumScore)
                {
                    objectFound = true;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            // Return result
            return(objectFound);
        }
Example #2
0
 private static void PrintResults(ImagePredictionResultModel result)
 {
     Console.Write(Environment.NewLine);
     foreach (var prediction in result.Predictions)
     {
         Console.WriteLine($"Tag: {prediction.Tag} - Probability: {String.Format("Value: {0:P2}.", prediction.Probability)}");
     }
 }
Example #3
0
        private static string GetMetaDataFromPrediction(ImagePredictionResultModel response)
        {
            var predictionDetail = response.Predictions
                                   .Select(x => new { tag = x.Tag, probability = Math.Round(x.Probability, 4) })
                                   .ToArray();

            return(JsonConvert.SerializeObject(predictionDetail));
        }
Example #4
0
        private static async Task ApplyPredictionToBlob(IStorageService storageService,
                                                        string name,
                                                        ImagePredictionResultModel response)
        {
            var metadata = CreateMetadata(response);

            await WriteMetadataToFile(storageService, name, metadata);
        }
Example #5
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                //trainmodel();
                if (FileUpload1.HasFile)
                {
                    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
                    //Response.Redirect(Request.Url.AbsoluteUri);

                    // Create the Api, passing in the training key
                    //CustomVisionTrainingClient trainingApi = new CustomVisionTrainingClient()
                    //{
                    //    ApiKey = trainingKey,
                    //    Endpoint = SouthCentralUsEndpoint
                    //};

                    //var project = trainingApi.GetProject(projectid);


                    using (var stream = new MemoryStream(File.ReadAllBytes(Server.MapPath("~/Images/") + fileName)))
                    {
                        // Create a prediction endpoint, passing in obtained prediction key
                        CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
                        {
                            ApiKey   = predictionKey,
                            Endpoint = SouthCentralUsEndpoint
                        };

                        var predictions = new ImagePredictionResultModel();

                        // Make a prediction against the new project
                        TextBox1.Text = string.Format("Making a prediction:") + System.Environment.NewLine;
                        var result = endpoint.PredictImage(projectid, stream);



                        // Loop over each prediction and write out the results
                        foreach (var c in result.Predictions)
                        {
                            TextBox1.Text += string.Format($"\t{c.TagName}: {c.Probability:P1}" + System.Environment.NewLine);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                showerror(ex);
                //throw ex;
            }
        }
Example #6
0
 private static IDictionary <string, string> CreateMetadata(ImagePredictionResultModel response)
 {
     return(new Dictionary <string, string>
     {
         {
             Constants.ImageMetadataKeys.ClassificationStatus,
             ImageClassificationStatus.Completed.ToString()
         },
         {
             Constants.ImageMetadataKeys.PredictionDetail,
             GetMetaDataFromPrediction(response)
         }
     });
 }
Example #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="predictionResult"></param>
        /// <returns></returns>
        public static bool ValidateImagePrediction(ImagePredictionResultModel predictionResult)
        {
            var objectTag = predictionResult.Predictions.Where(x => x.Tag == "object").FirstOrDefault();
            var oceanTag  = predictionResult.Predictions.Where(x => x.Tag == "ocean").FirstOrDefault();

            bool objectFound = false;

            if (objectTag != null && objectTag.Probability >= objectMinimumScore)
            {
                objectFound = true;
            }

            return(objectFound);
        }
        static void Main(string[] args)
        {
            MemoryStream testImage = null;
            Guid         projectId;
            string       predictionKey;

            try
            {
                Console.Write("Enter a local image URL: ");
                var imagePath = Console.ReadLine();
                testImage = new MemoryStream(File.ReadAllBytes(imagePath));

                Console.Write("Enter Project Id Guid: ");
                var projectIdGuid = Console.ReadLine();
                projectId = Guid.Parse(projectIdGuid);

                Console.Write("Enter Prediction Key: ");
                predictionKey = Console.ReadLine();

                Console.WriteLine();
                Console.WriteLine();

                // Utilizing the Microsoft.Cognitive.CustomVision NuGet Package to establish Credentials and the Endpoint
                PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
                PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

                ImagePredictionResultModel result = endpoint.PredictImage(projectId, testImage);

                Console.WriteLine("Prediction results for the given image are...");
                foreach (ImageTagPrediction prediction in result.Predictions)
                {
                    Console.WriteLine($"{prediction.Tag}: {prediction.Probability:P1}");
                }

                Console.WriteLine();
                exitInNSeconds(5);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                Console.WriteLine();
                exitInNSeconds(5);
            }
        }
Example #9
0
        static void Main(string[] args)
        {
            Guid   projectId     = Guid.Parse("[Insert project ID]");
            string predictionKey = "[Insert prediction Key]";
            string imagePath     = @"d:\sample.jpg";

            MemoryStream testImage = new MemoryStream(File.ReadAllBytes(imagePath));

            PredictionEndpointCredentials predictionEndpointCredentials = new PredictionEndpointCredentials(predictionKey);
            PredictionEndpoint            endpoint = new PredictionEndpoint(predictionEndpointCredentials);

            ImagePredictionResultModel result = endpoint.PredictImage(projectId, testImage);

            // Loop over each prediction and write out the results
            foreach (ImageTagPrediction prediction in result.Predictions)
            {
                Console.WriteLine($"\t{prediction.Tag}: {prediction.Probability:P1}");
            }

            Console.ReadKey();
        }
        private async void UpdateResults(ImageAnalyzer img)
        {
            this.searchErrorTextBlock.Visibility = Visibility.Collapsed;

            ImagePredictionResultModel result = null;
            var currentProjectViewModel       = (ProjectViewModel)this.projectsComboBox.SelectedValue;
            var currentProject = ((ProjectViewModel)this.projectsComboBox.SelectedValue).Model;

            var trainingApi   = this.userProvidedTrainingApi;
            var predictionApi = this.userProvidedPredictionApi;

            try
            {
                var iteractions = await trainingApi.GetIterationsAsync(currentProject.Id);

                var latestTrainedIteraction = iteractions.Where(i => i.Status == "Completed").OrderByDescending(i => i.TrainedAt.Value).FirstOrDefault();

                if (latestTrainedIteraction == null)
                {
                    throw new Exception("This project doesn't have any trained models yet. Please train it, or wait until training completes if one is in progress.");
                }

                if (img.ImageUrl != null)
                {
                    result = await CustomVisionServiceHelper.PredictImageUrlWithRetryAsync(predictionApi, currentProject.Id, new ImageUrl(img.ImageUrl), latestTrainedIteraction.Id);
                }
                else
                {
                    result = await CustomVisionServiceHelper.PredictImageWithRetryAsync(predictionApi, currentProject.Id, img.GetImageStreamCallback, latestTrainedIteraction.Id);
                }
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Error");
            }

            this.progressRing.IsActive     = false;
            this.resultsDetails.Visibility = Visibility.Visible;

            var matches = result?.Predictions?.Where(r => Math.Round(r.Probability * 100) > 0);

            if (matches == null || !matches.Any())
            {
                this.searchErrorTextBlock.Visibility = Visibility.Visible;
            }
            else
            {
                this.resultsGridView.ItemsSource = matches.Select(t => new { Tag = t.Tag, Probability = string.Format("{0}%", Math.Round(t.Probability * 100)) });
            }

            if (result?.Predictions != null)
            {
                this.activeLearningButton.Opacity = 1;

                this.PredictionDataForRetraining.Clear();
                this.PredictionDataForRetraining.AddRange(result.Predictions.Select(
                                                              t => new ActiveLearningTagViewModel
                {
                    PredictionResultId = result.Id,
                    TagId   = t.TagId,
                    TagName = t.Tag,
                    HasTag  = Math.Round(t.Probability * 100) > 0
                }));
            }
            else
            {
                this.activeLearningButton.Opacity = 0;
            }
        }
        public static ImagePredictionResultModel EvaluateCustomDNN(string imagePath, Function modelFunc)
        {
            DeviceDescriptor device           = DeviceDescriptor.CPUDevice;
            string           workingDirectory = Environment.CurrentDirectory;

            string[] class_labels = new string[] { "空", "牛", "松鼠", "人", "雉鸡", "猕猴", "鼠", "麂", "滇金丝猴", "鸟", "狗",
                                                   "山羊", "黄喉貂", "豹猫", "绵羊", "黄鼬", "黑熊", "野兔", "鬣羚", "马", "豪猪", "其他" };

            // Load the model.
            // This example requires the ResNet20_CIFAR10_CNTK.model.
            // The model can be downloaded from <see cref="https://www.cntk.ai/Models/CNTK_Pretrained/ResNet20_CIFAR10_CNTK.model"/>
            // Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model.
            // string modelFilePath = Path.Combine(domainBaseDirectory, @"CNTK\Models\ResNet20_CIFAR10_CNTK.model");


            // Get input variable. The model has only one single input.
            Variable inputVar = modelFunc.Arguments.Single();

            // Get shape data for the input variable
            NDShape inputShape    = inputVar.Shape;
            int     imageWidth    = inputShape[0];
            int     imageHeight   = inputShape[1];
            int     imageChannels = inputShape[2];
            int     imageSize     = inputShape.TotalSize;

            // Get output variable
            Variable outputVar = modelFunc.Output;

            var inputDataMap  = new Dictionary <Variable, Value>();
            var outputDataMap = new Dictionary <Variable, Value>();

            // Retrieve the image file.
            //Bitmap bmp = new Bitmap(Bitmap.FromFile(imageUrl));
            //System.Net.Http.HttpClient httpClient = new HttpClient();
            //Stream imageStream = await httpClient.GetStreamAsync(imageUrl);
            Bitmap bmp = new Bitmap(imagePath);

            var          resized    = bmp.Resize(imageWidth, imageHeight, true);
            List <float> resizedCHW = resized.ParallelExtractCHW();

            // Create input data map
            var inputVal = Value.CreateBatch(inputVar.Shape, resizedCHW, device);

            inputDataMap.Add(inputVar, inputVal);

            // Create output data map
            outputDataMap.Add(outputVar, null);

            // Start evaluation on the device
            try
            {
                modelFunc.Evaluate(inputDataMap, outputDataMap, device);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            // Get evaluate result as dense output
            var outputVal  = outputDataMap[outputVar];
            var outputData = outputVal.GetDenseData <float>(outputVar);

            float[] softmax_vals = ActivationFunctions.Softmax(outputData[0]);

            // construct a ImagePredictionResultModel.    "class name": prediction of the class.
            ImagePredictionResultModel predictionResult = new ImagePredictionResultModel();

            predictionResult.Id          = "TNC100";
            predictionResult.Project     = "TNCAnimalLabel";
            predictionResult.Iteration   = "1.00";
            predictionResult.Created     = DateTime.Now;
            predictionResult.Predictions = new List <TNCAnimalLabelWebAPI.Models.Prediction>();

            int class_id = 0;

            for (; class_id < (softmax_vals.Length); class_id++)
            {
                TNCAnimalLabelWebAPI.Models.Prediction prediction = new TNCAnimalLabelWebAPI.Models.Prediction();
                prediction.TagId       = class_id.ToString();
                prediction.Tag         = class_labels[class_id];
                prediction.Probability = softmax_vals[class_id];
                predictionResult.Predictions.Add(prediction);
            }

            return(predictionResult);
        }