public static List <CustomVisionModelData> GetBuiltInModelData(CustomVisionProjectType customVisionProjectType)
 {
     try
     {
         string fileName = customVisionProjectType == CustomVisionProjectType.Classification ? BuiltInClassificationModelsFileName : BuiltInObjectDetectionModelsFileName;
         string content  = File.ReadAllText(fileName);
         return(JsonConvert.DeserializeObject <List <CustomVisionModelData> >(content));
     }
     catch (Exception)
     {
         return(new List <CustomVisionModelData>());
     }
 }
Exemplo n.º 2
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 :(";
            }
        }
Exemplo n.º 3
0
        private async Task <bool> ExportOnnxProject(Export exportProject, CustomVisionProjectType customVisionProjectType)
        {
            if (string.IsNullOrEmpty(exportProject?.DownloadUri))
            {
                throw new ArgumentNullException("Download Uri");
            }

            bool          success               = false;
            Guid          newModelId            = Guid.NewGuid();
            StorageFolder onnxProjectDataFolder = await CustomVisionDataLoader.GetOnnxModelStorageFolderAsync(customVisionProjectType);

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

            switch (customVisionProjectType)
            {
            case CustomVisionProjectType.Classification:
                success = await DownloadFileAsync(exportProject.DownloadUri, file);

                break;

            case CustomVisionProjectType.ObjectDetection:
                success = await UnzipModelFileAsync(exportProject.DownloadUri, file);

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

                return(false);
            }

            string[] classLabels = this.TagsInCurrentGroup?.Select(x => x.Name)?.ToArray() ?? new string[] { };
            newExportedCustomVisionModel = new CustomVisionModelData
            {
                Id          = newModelId,
                Name        = CurrentProject.Name,
                ClassLabels = classLabels,
                ExportDate  = DateTime.UtcNow,
                FileName    = file.Name,
                FilePath    = file.Path
            };

            List <CustomVisionModelData> customVisionModelList = await CustomVisionDataLoader.GetCustomVisionModelDataAsync(customVisionProjectType);

            CustomVisionModelData customVisionModelWithSameName = customVisionModelList.FirstOrDefault(x => string.Equals(x.Name, CurrentProject.Name));

            if (customVisionModelWithSameName != null)
            {
                string titleMessage = $"There is already a “{CurrentProject.Name}” model in this device. Select “Replace” if you would like to replace it, or “Keep Both” if you would like to keep both.";
                await Util.ConfirmActionAndExecute(titleMessage,
                                                   async() =>
                {
                    // if user select Yes, we replace the model with the same name
                    bool modelEntryRemovedFromFile = customVisionModelList.Remove(customVisionModelWithSameName);
                    StorageFile modelFileToRemove  = await onnxProjectDataFolder.GetFileAsync(customVisionModelWithSameName.FileName);
                    if (modelEntryRemovedFromFile && modelFileToRemove != null)
                    {
                        await modelFileToRemove.DeleteAsync();
                    }
                    await SaveCustomVisionModelAsync(customVisionModelList, newExportedCustomVisionModel, customVisionProjectType);

                    // re-display flyout window
                    FlyoutBase.ShowAttachedFlyout(this.exportButton);
                    ShowShareStatus(customVisionProjectType);
                },
                                                   cancelAction : async() =>
                {
                    int maxNumberOfModelWithSameName = customVisionModelList
                                                       .Where(x => x.Name != null && x.Name.StartsWith(newExportedCustomVisionModel.Name, StringComparison.OrdinalIgnoreCase))
                                                       .Select(x =>
                    {
                        string modelNumberInString = x.Name.Split('_').LastOrDefault();
                        int.TryParse(modelNumberInString, out int number);
                        return(number);
                    })
                                                       .Max();

                    // if user select Cancel we just save the new model with the same name
                    newExportedCustomVisionModel.Name = $"{newExportedCustomVisionModel.Name}_{maxNumberOfModelWithSameName + 1}";
                    await SaveCustomVisionModelAsync(customVisionModelList, newExportedCustomVisionModel, customVisionProjectType);

                    // re-display flyout window
                    FlyoutBase.ShowAttachedFlyout(this.exportButton);
                    ShowShareStatus(customVisionProjectType);
                },
        public static async Task SaveCustomVisionModelDataAsync(List <CustomVisionModelData> customVisionModelData, CustomVisionProjectType customVisionProjectType)
        {
            switch (customVisionProjectType)
            {
            case CustomVisionProjectType.Classification:
                await SaveCustomVisionModelDataToFileAsync(customVisionModelData, RealtimeImageClassificationModelsFileName);

                break;

            case CustomVisionProjectType.ObjectDetection:
                await SaveCustomVisionModelDataToFileAsync(customVisionModelData, RealtimeObjectDetectionModelsFileName);

                break;
            }
        }
        public static async Task <List <CustomVisionModelData> > GetCustomVisionModelDataAsync(CustomVisionProjectType customVisionProjectType)
        {
            switch (customVisionProjectType)
            {
            case CustomVisionProjectType.Classification:
                return(await ReadCustomVisionModelDataFromFileAsync(RealtimeImageClassificationModelsFileName));

            case CustomVisionProjectType.ObjectDetection:
                return(await ReadCustomVisionModelDataFromFileAsync(RealtimeObjectDetectionModelsFileName));

            default:
                return(new List <CustomVisionModelData>());
            }
        }
        public static async Task <StorageFolder> GetOnnxModelStorageFolderAsync(CustomVisionProjectType customVisionProjectType)
        {
            string foldername = customVisionProjectType == CustomVisionProjectType.Classification ? ClassificationOnnxModelStorageName : ObjectDetectionOnnxModelStorageName;

            return(await ApplicationData.Current.LocalFolder.CreateFolderAsync(foldername, CreationCollisionOption.OpenIfExists));
        }
        public async Task <CustomVisionResponse> AnalyzeAsync(string imageUrl, IFormFile file,
                                                              Guid projectId, string iterationPublishedName, CustomVisionProjectType projectType)
        {
            // Custom vision
            if (!string.IsNullOrWhiteSpace(imageUrl))
            {
                var imageAnalysis = CustomVisionAnalyzeImageByUrlAsync(imageUrl, projectId, iterationPublishedName, projectType);
                var imageInfo     = ImageInfoProcessor.GetImageInfoFromImageUrlAsync(imageUrl, _httpClientFactory);

                // Combine
                var task = Task.WhenAll(imageAnalysis, imageInfo);

                try
                {
                    await task;

                    return(new CustomVisionResponse
                    {
                        ImageInfo = imageInfo.Result,
                        Predictions = imageAnalysis.Result.Predictions.ToList()
                    });
                }
                catch (CustomVisionErrorException ex)
                {
                    var exceptionMessage = ex.Response.Content;
                    var parsedJson       = JToken.Parse(exceptionMessage);

                    if (ex.Response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                    {
                        return(new CustomVisionResponse
                        {
                            ApiRequestErrorMessage = $"Bad request thrown by the underlying API from Microsoft:",
                            ApiRequestErrorContent = parsedJson.ToString(Formatting.Indented)
                        });
                    }
                    else
                    {
                        return(new CustomVisionResponse
                        {
                            OtherErrorMessage = $"Error thrown by the underlying API from Microsoft:",
                            OtherErrorContent = parsedJson.ToString(Formatting.Indented)
                        });
                    }
                }
            }
            else
            {
                using (var analyzeStream = new MemoryStream())
                    using (var outputStream = new MemoryStream())
                    {
                        // Get initial value
                        await file.CopyToAsync(analyzeStream);

                        analyzeStream.Seek(0, SeekOrigin.Begin);
                        await analyzeStream.CopyToAsync(outputStream);

                        // Reset the stream for consumption
                        analyzeStream.Seek(0, SeekOrigin.Begin);
                        outputStream.Seek(0, SeekOrigin.Begin);

                        try
                        {
                            var imageAnalysis = await CustomVisionAnalyzeImageByStreamAsync(analyzeStream, projectId, iterationPublishedName, projectType);

                            var imageInfo = ImageInfoProcessor.GetImageInfoFromStream(outputStream, file.ContentType);

                            return(new CustomVisionResponse
                            {
                                ImageInfo = imageInfo,
                                Predictions = imageAnalysis.Predictions.ToList()
                            });
                        }
                        catch (CustomVisionErrorException ex)
                        {
                            var exceptionMessage = ex.Response.Content;
                            var parsedJson       = JToken.Parse(exceptionMessage);

                            if (ex.Response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                            {
                                return(new CustomVisionResponse
                                {
                                    ApiRequestErrorMessage = $"Bad request thrown by the underlying API from Microsoft:",
                                    ApiRequestErrorContent = parsedJson.ToString(Formatting.Indented)
                                });
                            }
                            else
                            {
                                return(new CustomVisionResponse
                                {
                                    OtherErrorMessage = $"Error thrown by the underlying API from Microsoft:",
                                    OtherErrorContent = parsedJson.ToString(Formatting.Indented)
                                });
                            }
                        }
                    }
            }
        }
        private async Task <ImagePrediction> CustomVisionAnalyzeImageByStreamAsync(Stream imageStream, Guid projectId, string publishedName, CustomVisionProjectType projectType)
        {
            ImagePrediction imagePrediction = null;

            switch (projectType)
            {
            case CustomVisionProjectType.Classification:
                imagePrediction = await _customVisionPredictionClient.ClassifyImageWithNoStoreAsync(
                    projectId : projectId,
                    publishedName : publishedName,
                    imageData : imageStream);

                break;

            case CustomVisionProjectType.Object_Detection:
                imagePrediction = await _customVisionPredictionClient.DetectImageWithNoStoreAsync(
                    projectId : projectId,
                    publishedName : publishedName,
                    imageData : imageStream);

                break;
            }

            return(imagePrediction);
        }