Ejemplo n.º 1
0
        private async Task LoadProjectsFromFile(CustomVisionModelData preselectedProject = null)
        {
            try
            {
                this.Projects.Clear();
                List <CustomVisionModelData> prebuiltModelList = CustomVisionDataLoader.GetBuiltInModelData(CustomVisionProjectType.Classification) ?? new List <CustomVisionModelData>();
                foreach (CustomVisionModelData prebuiltModel in prebuiltModelList)
                {
                    this.Projects.Add(prebuiltModel);
                }

                List <CustomVisionModelData> customVisionModelList = await CustomVisionDataLoader.GetCustomVisionModelDataAsync(CustomVisionProjectType.Classification) ?? new List <CustomVisionModelData>();

                foreach (CustomVisionModelData customModel in customVisionModelList)
                {
                    this.Projects.Add(customModel);
                }

                CustomVisionModelData defaultProject = preselectedProject != null?this.Projects.FirstOrDefault(x => x.Id.Equals(preselectedProject.Id)) : null;

                if (defaultProject != null)
                {
                    this.projectsComboBox.SelectedValue = defaultProject;
                }
                else
                {
                    this.projectsComboBox.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                await Util.GenericApiCallExceptionHandler(ex, "Failure loading projects");
            }
        }
Ejemplo n.º 2
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);
                },