diff --git a/README.md b/README.md index 8902346..8ce1058 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,11 @@ Windows Python 环境使用 `tools/Setup-Python.ps1 -Sam2`;Linux 环境和部 1. 接好 UVC 或海康相机,选择设备和配方,应用后开始检测。 2. 配方编辑器保留步骤、区域、全局设置、检测模型四个标签页;区域坐标使用原图像素。 3. 零件标注与训练包含相机录像、视频导入、画框、SAM2 传播、复核、数据集导出、YOLO 训练和 ONNX 导出。 + - 在标注页选择所属配方,每个视频录入一个零件;导入数据自动保存在 `workspace/training/配方名/parts`。 + - 点击“训练并更新配方模型”,自动累计导出该配方所有已加入视频的复核数据,并使用上一版配方模型开始新训练;首次使用 `yolo11n.pt`。无需手选权重。 + - 同名零件的多个视频共用类别,新增零件自动分配类别编号。每个视频均需完成足够的训练段和末尾验证段复核。 + - 模型保存为各次训练结果目录内的 `配方名.pt`,配方目录的 `latest-model.json` 指向最新成功版本;旧版本保留。ONNX 导出使用此最新配方模型,配方编辑器仍需选择导出的 ONNX 用于检测。 + - 旧版独立标注项目打开后,选择所属配方并执行导出或训练即可加入;原视频帧目录需保留。 4. 模型、相机和 Python 依赖的实际状态以日志和诊断结果为准。 模型文件:`palm_detection_full_Nx3x192x192_post.onnx`、`hand_landmark_sparse_Nx3x224x224.onnx`; diff --git a/src/SopHelper.Avalonia/Training/MainViewModel.cs b/src/SopHelper.Avalonia/Training/MainViewModel.cs index abb492a..a7b7e6f 100644 --- a/src/SopHelper.Avalonia/Training/MainViewModel.cs +++ b/src/SopHelper.Avalonia/Training/MainViewModel.cs @@ -71,6 +71,15 @@ public sealed class MainViewModel : BindableBase } // Configuration is saved locally, except the API key which is passed only through the worker environment. private static string PythonPath => DesktopPlatform.PythonPath; + public ObservableCollection Recipes { get; } = []; + private string _recipeName = ""; + public string RecipeName { get => _recipeName; set => SetProperty(ref _recipeName, value); } + private string RecipeDirectoryFor(string name) + { + if (string.IsNullOrWhiteSpace(name) || name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || name is "." or "..") + throw new ArgumentException("请先选择有效的配方"); + return Path.Combine(Root, "workspace", "training", name); + } public string Label { get; set; } = "零件"; public int ExtractStride { get; set; } = 1; public int MaxFrames { get; set; } = 600; @@ -140,7 +149,7 @@ public sealed class MainViewModel : BindableBase if (!Directory.Exists(directory)) throw new DirectoryNotFoundException("尚无训练结果目录,请先完成一次训练"); DesktopPlatform.Reveal(directory); }); - OnnxCommand = Command(async () => await Run(Request("onnx"))); + OnnxCommand = Command(async () => { SaveLabel(); await Run(Request("onnx")); }); SuggestCommand = Command(async () => { SaveLabel(); @@ -173,13 +182,19 @@ public sealed class MainViewModel : BindableBase } // Async operations catch their own exceptions inside Run. Import does synchronous validation before awaiting. private object Request(string action) => new { action, project = RequireProject().FilePath, every = ExportEvery, - valRatio = 0.2, epochs = Epochs, imageSize = ImageSize, batch = Batch, model = Model, device = Device, + valRatio = 0.2, epochs = Epochs, imageSize = ImageSize, batch = Batch, model = "yolo11n.pt", device = Device, outputDirectory = TrainingOutputDirectory }; private TrainingProject RequireProject() => _project ?? throw new InvalidOperationException("请先导入视频或打开项目"); private void SaveLabel() { var project = RequireProject(); if (string.IsNullOrWhiteSpace(Label)) throw new ArgumentException("请填写零件类别名称"); + if (string.IsNullOrWhiteSpace(project.RecipeDirectory)) + { + project.RecipeDirectory = RecipeDirectoryFor(RecipeName); + project.RecipeName = RecipeName; + } + if (project.RecipeName != RecipeName) throw new InvalidOperationException("当前视频属于配方“" + project.RecipeName + "”,请切回该配方;新配方请导入新视频"); project.Name = Label.Trim(); project.Save(); } private async void Import() @@ -190,11 +205,10 @@ public sealed class MainViewModel : BindableBase try { video.InitialDirectory = VideoImportDirectory ?? CameraViewModel.ReadSaveDirectory(Root); } catch (Exception e) { Append("录像目录配置读取失败:" + e.Message); } if (await video.ShowDialogAsync() != true) return; - var folder = new OpenFolderDialog { Title = "选择项目保存的父文件夹(会自动新建子文件夹)" }; - if (await folder.ShowDialogAsync() != true) return; + var recipeDirectory = RecipeDirectoryFor(RecipeName); if (string.IsNullOrWhiteSpace(Label)) throw new ArgumentException("请填写零件类别名称"); - var output = Path.Combine(folder.FolderName, "part-" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + "-" + Guid.NewGuid().ToString("N")[..4]); - if (await Run(new { action = "extract", video = video.FileName, output, name = Label.Trim(), stride = ExtractStride, maxFrames = MaxFrames }, reload: false)) + var output = Path.Combine(recipeDirectory, "parts", "part-" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + "-" + Guid.NewGuid().ToString("N")[..4]); + if (await Run(new { action = "extract", video = video.FileName, output, name = Label.Trim(), recipeName = RecipeName, recipeDirectory, stride = ExtractStride, maxFrames = MaxFrames }, reload: false)) Load(Path.Combine(output, "project.json")); } catch (Exception e) { Status = e.Message; Append(e.Message); } @@ -202,6 +216,11 @@ public sealed class MainViewModel : BindableBase public void Load(string path, int index = 0) { _project = TrainingProject.Load(path); + if (!string.IsNullOrWhiteSpace(_project.RecipeName)) + { + if (!Recipes.Contains(_project.RecipeName)) Recipes.Add(_project.RecipeName); + RecipeName = _project.RecipeName; + } Label = _project.Name; RaisePropertyChanged(nameof(Label)); _selected = null; Frames.Clear(); foreach (var frame in _project.Frames) Frames.Add(frame); @@ -334,7 +353,7 @@ public sealed class MainViewModel : BindableBase { if (IsTraining) return; LatestWeights = ""; TrainingRunDirectory = ""; TrainingMetrics = "尚无训练指标"; - var file = Path.Combine(RequireProject().DirectoryPath, "latest-model.json"); + var file = Path.Combine(string.IsNullOrWhiteSpace(RequireProject().RecipeDirectory) ? RequireProject().DirectoryPath : RequireProject().RecipeDirectory, "latest-model.json"); if (!File.Exists(file)) return; try { diff --git a/src/SopHelper.Avalonia/Training/TrainingPage.axaml b/src/SopHelper.Avalonia/Training/TrainingPage.axaml index 7f9c9e4..9878b21 100644 --- a/src/SopHelper.Avalonia/Training/TrainingPage.axaml +++ b/src/SopHelper.Avalonia/Training/TrainingPage.axaml @@ -71,6 +71,9 @@ + + + @@ -94,8 +97,9 @@ - - + + + @@ -103,9 +107,11 @@