Example #1
0
        private async void UpdateResults(ImageAnalyzer img)
        {
            this.searchErrorTextBlock.Visibility = Visibility.Collapsed;

            Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImagePrediction 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 Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.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.TagName, 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.TagName,
                    HasTag  = Math.Round(t.Probability * 100) > 0
                }));
            }
            else
            {
                this.activeLearningButton.Opacity = 0;
            }
        }
Example #2
0
        public Result GetClothingPrediction(string predictionImagePath)
        {
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = ClothingModelPredictionKey
            };
            MemoryStream predictionImage = new MemoryStream(File.ReadAllBytes(predictionImagePath));

            Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImagePrediction predictionResult = endpoint.PredictImage(ClothingModelProjectID, predictionImage);

            Result result = new Result
            {
                // First result in list is highest probability
                Name        = predictionResult.Predictions[0].TagName,
                Probability = predictionResult.Predictions[0].Probability.ToString()
            };

            return(result);
        }
Example #3
0
        public Result GetClassification(string predictionImagePath)
        {
            PredictionEndpoint endpoint = new PredictionEndpoint()
            {
                ApiKey = ClassifierModelPredictionKey
            };
            MemoryStream predictionImage = new MemoryStream(File.ReadAllBytes(predictionImagePath));

            Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImagePrediction predictionResult = endpoint.PredictImage(ClassifierModelProjectID, predictionImage);

            Result result = new Result
            {
                // First result in list is highest probability
                Name        = predictionResult.Predictions[0].TagName,
                Probability = predictionResult.Predictions[0].Probability.ToString()
            };

            if (result.Name.Equals("Barcode"))
            {
                result.Classification = Classifiers.Barcode;
            }
            else if (result.Name.Equals("Book"))
            {
                result.Classification = Classifiers.Book;
            }
            else if (result.Name.Equals("Clothing"))
            {
                result.Classification = Classifiers.Clothing;
            }
            else
            {
                result.Classification = Classifiers.Other;
            }

            return(result);
        }
        private async void UpdateResults(ImageAnalyzer img)
        {
            Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImagePrediction 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 Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.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;

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

            if (!currentProjectViewModel.IsObjectDetection)
            {
                //show image classification matches
                OverlayPresenter.MatchInfo = new MatchOverlayInfo(matches);
            }
            else
            {
                //show detected objects
                OverlayPresenter.ObjectInfo = matches.Where(m => m.Probability >= 0.6).Select(i => new PredictedObjectOverlayInfo(i)).ToList();
            }

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

                this.PredictionDataForRetraining.Clear();
                this.PredictionDataForRetraining.AddRange(result.Predictions.Select(t => new ActiveLearningTagViewModel
                {
                    PredictionResultId = result.Id,
                    TagId   = t.TagId,
                    TagName = t.TagName,
                    HasTag  = Math.Round(t.Probability * 100) > 0
                }));
            }
            else
            {
                this.activeLearningButton.Opacity = 0;
            }
        }