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 (string.IsNullOrEmpty(latestTrainedIteraction.PublishName)) { await trainingApi.PublishIterationAsync(modelId, latestTrainedIteraction.Id, publishName : latestTrainedIteraction.Id.ToString(), predictionId : SettingsHelper.Instance.CustomVisionPredictionResourceId); latestTrainedIteraction = await trainingApi.GetIterationAsync(modelId, latestTrainedIteraction.Id); } if (CurrentInputProductImage?.ImageUrl != null) { result = await CustomVisionServiceHelper.ClassifyImageUrlWithRetryAsync(predictionApi, modelId, new ImageUrl(CurrentInputProductImage.ImageUrl), latestTrainedIteraction.PublishName); } else if (CurrentInputProductImage?.GetImageStreamCallback != null) { result = await CustomVisionServiceHelper.ClassifyImageWithRetryAsync(predictionApi, modelId, CurrentInputProductImage.GetImageStreamCallback, latestTrainedIteraction.PublishName); } } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Custom Vision error analyzing product image"); } 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; CustomVisionTrainingClient trainingApi = this.userProvidedTrainingApi; CustomVisionPredictionClient predictionApi = this.userProvidedPredictionApi; try { IList <Iteration> iteractions = await trainingApi.GetIterationsAsync(currentProject.Id); Iteration 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 (string.IsNullOrEmpty(latestTrainedIteraction.PublishName)) { await trainingApi.PublishIterationAsync(currentProject.Id, latestTrainedIteraction.Id, latestTrainedIteraction.Id.ToString(), SettingsHelper.Instance.CustomVisionPredictionResourceId); latestTrainedIteraction = await trainingApi.GetIterationAsync(currentProject.Id, latestTrainedIteraction.Id); } if (img.ImageUrl != null) { result = await CustomVisionServiceHelper.ClassifyImageUrlWithRetryAsync(predictionApi, currentProject.Id, new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl(img.ImageUrl), latestTrainedIteraction.PublishName); } else { result = await CustomVisionServiceHelper.ClassifyImageWithRetryAsync(predictionApi, currentProject.Id, img.GetImageStreamCallback, latestTrainedIteraction.PublishName); } } 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; } }