Exemple #1
0
        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);
        }
Exemple #2
0
        public async Task <VisualAlertScenarioData> ExportOnnxProject(Project project)
        {
            // get latest iteration
            IList <Iteration> iterations = await trainingApi.GetIterationsAsync(project.Id);

            Iteration latestTrainedIteration = iterations.Where(i => i.Status == "Completed").OrderByDescending(i => i.TrainedAt.Value).FirstOrDefault();

            // export iteration - get download url
            Export exportProject = null;

            if (latestTrainedIteration != null && latestTrainedIteration.Exportable)
            {
                // get project's download Url for the particular platform Windows (ONNX) model
                exportProject = await CustomVisionServiceHelper.ExportIteration(trainingApi, project.Id, latestTrainedIteration.Id);
            }

            if (string.IsNullOrEmpty(exportProject?.DownloadUri))
            {
                throw new ArgumentNullException("Download Uri");
            }

            // download onnx model
            Guid          newModelId            = Guid.NewGuid();
            StorageFolder onnxProjectDataFolder = await VisualAlertDataLoader.GetOnnxModelStorageFolderAsync();

            StorageFile file = await onnxProjectDataFolder.CreateFileAsync($"{newModelId}.onnx", CreationCollisionOption.ReplaceExisting);

            bool success = await Util.UnzipModelFileAsync(exportProject.DownloadUri, file);

            if (!success)
            {
                await file.DeleteAsync();

                return(null);
            }

            return(new VisualAlertScenarioData
            {
                Id = newModelId,
                Name = project.Name,
                ExportDate = DateTime.UtcNow,
                FileName = file.Name,
                FilePath = file.Path
            });
        }
Exemple #3
0
        private async Task ExportProject(Platform currentPlatform)
        {
            CustomVisionProjectType customVisionProjectType = CustomVisionServiceHelper.ObjectDetectionDomainGuidList.Contains(this.CurrentProject.Settings.DomainId)
                    ? CustomVisionProjectType.ObjectDetection : CustomVisionProjectType.Classification;

            bool success = false;

            try
            {
                this.shareStatusTextBlock.Text = "Exporting model...";
                this.shareStatusPanelDescription.Visibility = Visibility.Collapsed;
                this.closeFlyoutBtn.Visibility         = Visibility.Visible;
                this.projectShareProgressRing.IsActive = true;

                // get latest iteration of the project
                if (this.LatestTrainedIteration == null)
                {
                    await this.LoadLatestIterationInCurrentProject();
                }

                if (LatestTrainedIteration != null && LatestTrainedIteration.Exportable)
                {
                    // get project's download Url for the particular platform
                    // Windows (ONNX) model: export latest version of the ONNX model (1.2)
                    Export exportProject = await CustomVisionServiceHelper.ExportIteration(trainingApi, this.CurrentProject.Id, LatestTrainedIteration.Id);

                    success = await ExportOnnxProject(exportProject, customVisionProjectType);
                }
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "We couldn't export the model at this time.");
            }
            finally
            {
                this.projectShareProgressRing.IsActive = false;
                this.closeFlyoutBtn.Visibility         = Visibility.Collapsed;
                this.shareStatusTextBlock.Text         = success
                        ? "The project was exported successfully."
                        : "Something went wrong and we couldn't export the model. Sorry :(";
            }
        }
Exemple #4
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;
            }
        }
        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;
            }
        }