in Kiosk/Views/CustomVision/CustomVisionExplorer.xaml.cs [85:157]
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;
}
}