private async Task ExportOnnxProject()

in Kiosk/Views/CustomVision/CustomVisionSetup.xaml.cs [689:771]


        private async Task<bool> ExportOnnxProject(Export exportProject, CustomVisionProjectType customVisionProjectType)
        {
            if (string.IsNullOrEmpty(exportProject?.DownloadUri))
            {
                throw new ArgumentNullException("Download Uri");
            }

            var newModelId = Guid.NewGuid();
            this.downloadCancellationTokenSource = new CancellationTokenSource();

            StorageFolder onnxProjectDataFolder = await CustomVisionDataLoader.GetOnnxModelStorageFolderAsync(customVisionProjectType);
            StorageFile file = await onnxProjectDataFolder.CreateFileAsync($"{newModelId}.onnx", CreationCollisionOption.ReplaceExisting);
            bool success = await Util.UnzipModelFileAsync(exportProject.DownloadUri, file, this.downloadCancellationTokenSource.Token);
            
            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);
                    },
                    confirmActionLabel: "Replace",
                    cancelActionLabel: "Keep Both");
            }
            else
            {
                await SaveCustomVisionModelAsync(customVisionModelList, newExportedCustomVisionModel, customVisionProjectType);
                ShowShareStatus(customVisionProjectType);
            }

            return success;
        }