private async Task <StorageFile> GetModelFileAsync(CustomVisionModelData customVisionModelData) { if (customVisionModelData.IsPrebuiltModel) { string modelPath = $"Assets\\{customVisionModelData.FileName}"; return(await Package.Current.InstalledLocation.GetFileAsync(modelPath)); } else { StorageFolder onnxProjectDataFolder = await CustomVisionDataLoader.GetOnnxModelStorageFolderAsync(CustomVisionProjectType.Classification); return(await onnxProjectDataFolder.GetFileAsync(customVisionModelData.FileName)); } }
private async Task DeleteProjectAsync() { CustomVisionModelData currentProject = this.projectsComboBox.SelectedValue as CustomVisionModelData; if (currentProject != null && !currentProject.IsPrebuiltModel) { // delete ONNX model file StorageFolder onnxProjectDataFolder = await CustomVisionDataLoader.GetOnnxModelStorageFolderAsync(CustomVisionProjectType.Classification); StorageFile modelFile = await onnxProjectDataFolder.GetFileAsync(currentProject.FileName); if (modelFile != null) { await modelFile.DeleteAsync(); } // update local file with custom models this.Projects.Remove(currentProject); List <CustomVisionModelData> updatedCustomModelList = this.Projects.Where(x => !x.IsPrebuiltModel).ToList(); await CustomVisionDataLoader.SaveCustomVisionModelDataAsync(updatedCustomModelList, CustomVisionProjectType.Classification); this.projectsComboBox.SelectedIndex = 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); },