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"); } }
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 LoadCurrentModelAsync(CustomVisionModelData currentProject) { try { this.deleteBtn.Visibility = currentProject.IsPrebuiltModel ? Visibility.Collapsed : Visibility.Visible; LoadSupportedClasses(currentProject); StorageFile modelFile = await GetModelFileAsync(currentProject); this.customVisionModel = await CustomVisionModel.CreateONNXModel(modelFile); } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Failure loading current project"); } }
private async Task LoadCurrentModelAsync(CustomVisionModelData currentProject) { try { this.deleteBtn.Visibility = currentProject.IsPrebuiltModel ? Visibility.Collapsed : Visibility.Visible; LoadSupportedClasses(currentProject); StorageFile modelFile = await GetModelFileAsync(currentProject); this.objectDetectionModel = new ObjectDetection(this.allModelObjects, probabilityThreshold: MinProbabilityValue); await this.objectDetectionModel.Init(modelFile); } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Failure loading current project"); } }
private void LoadSupportedClasses(CustomVisionModelData currentProject) { string modelLabelsPrefix = "Supported classes: "; this.modelObjects.Text = string.Empty; this.moreModelObjects.Text = string.Empty; this.allModelObjects = currentProject?.ClassLabels?.OrderBy(x => x).ToArray() ?? new string[] { }; if (this.allModelObjects.Length > 2) { this.modelObjects.Text = $"{modelLabelsPrefix}{string.Join(", ", this.allModelObjects.Take(2))}"; this.moreModelObjects.Text = $"and {allModelObjects.Length - 2} more";; } else { this.modelObjects.Text = modelLabelsPrefix + string.Join(", ", allModelObjects); } }
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); },