private async Task <ImagePrediction> AnalyzeProductImageAsync(Guid modelId) { ImagePrediction result = null; try { var iteractions = await trainingApi.GetIterationsAsync(modelId); 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."); } if (CurrentInputProductImage?.ImageUrl != null) { result = await CustomVisionServiceHelper.PredictImageUrlWithRetryAsync(predictionApi, modelId, new ImageUrl(CurrentInputProductImage.ImageUrl), latestTrainedIteraction.Id); } else if (CurrentInputProductImage?.GetImageStreamCallback != null) { result = await CustomVisionServiceHelper.PredictImageWithRetryAsync(predictionApi, modelId, CurrentInputProductImage.GetImageStreamCallback, latestTrainedIteraction.Id); } } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Custom Vision error analyzing product image"); } return(result); }
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; } }
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; } }