Exemplo n.º 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
            {
                if (!currentProjectViewModel.IsObjectDetection)
                {
                    this.resultsGridView.ItemsSource = matches.Select(t => new { Tag = t.TagName, Probability = string.Format("{0}%", Math.Round(t.Probability * 100)) });
                }
                else
                {
                    this.resultsDetails.Visibility = Visibility.Collapsed;
                    this.currentDetectedObjects    = matches.Where(m => m.Probability >= 0.6);
                    ShowObjectDetectionBoxes(this.currentDetectedObjects);
                }
            }

            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;
            }
        }
Exemplo n.º 2
0
        public IList <Result> predictingImages(String imgUrl)
        {
            IList <Result> predictionResult       = new List <Result>();
            CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
            {
                ApiKey   = predictionKey,
                Endpoint = SouthCentralUsEndpoint
            };

            projectID = new Guid(ConfigurationManager.AppSettings["CustomVisionProjectID"]);
            Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl        testImage = new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl(imgUrl);
            Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImagePrediction result1   = endpoint.ClassifyImageUrl(projectID, "Veg Classifier", testImage);
            foreach (var output in result1.Predictions)
            {
                Result finalresult = new Result();
                finalresult.probability = output.Probability * 100;
                finalresult.tagName     = output.TagName;
                predictionResult.Add(finalresult);
            }
            return(predictionResult);
        }