diff --git a/README.md b/README.md index 8bd2ee0..54504aa 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,14 @@ Windows Python 环境使用 `tools/Setup-Python.ps1 -Sam2`;Linux 环境和部 ## 操作流程 1. 接好 UVC 或海康相机,选择设备和配方,应用后开始检测。 -2. 配方编辑器保留步骤、区域、全局设置、检测模型四个标签页;区域坐标使用原图像素。 +2. 配方编辑器包含步骤、区域、全局设置、零件列表、检测模型五个标签页;区域坐标使用原图像素。 + - 零件名称只在“零件列表”维护,随配方保存到 `sop.parts`。标注类别、步骤目标类别、必留物品和完成必需物品均从该列表选择。 + - 旧配方首次编辑时汇总已有训练类别、模型配置、步骤和标注项目名称;保存后列表成为唯一来源。重名、空名以及删除仍被步骤或标注项目引用的名称会阻止保存。 + - 检测模型的类别表只读;维护零件列表不会更改已训练模型的类别映射。模型输出类别仍以训练版本为准。 3. 零件标注与训练包含相机录像、视频导入、画框、SAM2 传播、复核、数据集导出、YOLO 训练和 ONNX 导出。 - - 在标注页选择所属配方,每个视频录入一个零件;录像和导入原视频保存在 `recipes/配方名/recordings`,标注数据保存在同配方的 `parts`。 + - 在标注页选择所属配方及已有零件类别,每个视频录入一个零件;录像和导入原视频保存在 `recipes/配方名/recordings`,标注数据保存在同配方的 `parts`。 - 点击“训练并更新配方模型”,自动累计导出该配方所有已加入视频的复核数据,并使用上一版配方模型开始新训练;首次使用 `yolo11n.pt`。无需手选权重。 - - 同名零件的多个视频共用类别,新增零件自动分配类别编号。每个视频均需完成足够的训练段和末尾验证段复核。 + - 同名零件的多个视频共用类别,训练导出按零件列表顺序分配类别编号,并拒绝列表以外的项目类别。每个视频均需完成足够的训练段和末尾验证段复核。 - 训练成功后自动将 `配方名.pt` 和 `配方名.onnx` 发布到同配方的 `models/训练版本/`;训练曲线和日志保存在 `runs`,数据集保存在 `datasets`。`latest-model.json` 指向最新完整版本,重新应用配方即可加载。 - “保存参数”写入所选配方的 `training-settings.json`,切换配方时恢复各自参数。打开外部标注项目会将其复制到所属配方目录。 4. 模型、相机和 Python 依赖的实际状态以日志和诊断结果为准。 @@ -78,6 +81,8 @@ Windows Python 环境使用 `tools/Setup-Python.ps1 -Sam2`;Linux 环境和部 ```powershell dotnet run --project tests/Avalonia.Smoke -c Release +dotnet run --project tests/PartCatalog.Smoke -c Release +python/python.exe tests/Training.Smoke/test_recipe_training.py dotnet run --project tests/OpenCv.Smoke -c Release dotnet run --project tests/HandednessSmoke -c Release dotnet run --project tests/Training.Smoke -c Release -- python/python.exe diff --git a/src/SopHelper.Avalonia/Training/MainViewModel.cs b/src/SopHelper.Avalonia/Training/MainViewModel.cs index 6dcbf0e..5794ede 100644 --- a/src/SopHelper.Avalonia/Training/MainViewModel.cs +++ b/src/SopHelper.Avalonia/Training/MainViewModel.cs @@ -7,6 +7,7 @@ using SopHelper.Presentation.Threading; using SopHelper.Presentation; using SopHelper.Presentation.Imaging; using SopHelper.Engine.Training; +using SopHelper.Engine.Sop; using Prism.Commands; using Prism.Mvvm; @@ -88,6 +89,7 @@ public sealed class MainViewModel : BindableBase Status="已切换配方,请导入视频或打开该配方的标注项目"; } LoadTrainingResult(); + RefreshPartNames(); RaisePropertyChanged(nameof(RecipeFolder)); } } @@ -98,7 +100,26 @@ public sealed class MainViewModel : BindableBase throw new ArgumentException("请先选择有效的配方"); return DesktopPlatform.Recipes.DirectoryFor(name); } - public string Label { get; set; } = "零件"; + public ObservableCollection PartNames { get; } = []; + private string _label = ""; + public string Label { get => _label; set => SetProperty(ref _label, value); } + public void RefreshPartNames() + { + var selected = Label; + PartNames.Clear(); + if (!string.IsNullOrWhiteSpace(RecipeName)) + { + var path = DesktopPlatform.Recipes.ConfigPath(RecipeName); + if (File.Exists(path)) + foreach (var name in PartCatalog.Read(SopConfigLoader.Load(path), RecipeDirectoryFor(RecipeName))) PartNames.Add(name); + } + Label = PartNames.Contains(selected) ? selected : ""; + } + private void ValidatePartSelection() + { + RefreshPartNames(); + if (!PartNames.Contains(Label)) throw new InvalidOperationException("请先在配方编辑的“零件列表”中添加零件,再选择零件类别"); + } public int ExtractStride { get; set; } = 1; public int MaxFrames { get; set; } = 600; public string Checkpoint { get; set; } = ""; @@ -209,7 +230,7 @@ public sealed class MainViewModel : BindableBase private void SaveLabel() { var project = RequireProject(); - if (string.IsNullOrWhiteSpace(Label)) throw new ArgumentException("请填写零件类别名称"); + ValidatePartSelection(); if (string.IsNullOrWhiteSpace(project.RecipeDirectory)) { project.RecipeDirectory = RecipeDirectoryFor(RecipeName); @@ -227,7 +248,7 @@ public sealed class MainViewModel : BindableBase catch (Exception e) { Append("录像目录配置读取失败:" + e.Message); } if (await video.ShowDialogAsync() != true) return; var recipeDirectory = RecipeDirectoryFor(RecipeName); - if (string.IsNullOrWhiteSpace(Label)) throw new ArgumentException("请填写零件类别名称"); + ValidatePartSelection(); 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")); @@ -246,6 +267,7 @@ public sealed class MainViewModel : BindableBase RecipeName = _project.RecipeName; } Label = _project.Name; RaisePropertyChanged(nameof(Label)); + RefreshPartNames(); _selected = null; Frames.Clear(); foreach (var frame in _project.Frames) Frames.Add(frame); SelectedFrame = Frames[Math.Clamp(index, 0, Frames.Count - 1)]; diff --git a/src/SopHelper.Avalonia/Training/TrainingPage.axaml b/src/SopHelper.Avalonia/Training/TrainingPage.axaml index 1f66796..551be77 100644 --- a/src/SopHelper.Avalonia/Training/TrainingPage.axaml +++ b/src/SopHelper.Avalonia/Training/TrainingPage.axaml @@ -74,10 +74,10 @@ - - + + - + diff --git a/src/SopHelper.Avalonia/Training/TrainingPage.axaml.cs b/src/SopHelper.Avalonia/Training/TrainingPage.axaml.cs index 9d0b368..3c3fcd1 100644 --- a/src/SopHelper.Avalonia/Training/TrainingPage.axaml.cs +++ b/src/SopHelper.Avalonia/Training/TrainingPage.axaml.cs @@ -28,6 +28,7 @@ public partial class TrainingPage : UserControl foreach (var recipe in sop.Recipes.Select(System.IO.Path.GetFileNameWithoutExtension).OfType()) if (!model.Recipes.Contains(recipe)) model.Recipes.Add(recipe); if (string.IsNullOrWhiteSpace(model.RecipeName)) model.RecipeName = System.IO.Path.GetFileNameWithoutExtension(sop.SelectedRecipe) ?? ""; + model.RefreshPartNames(); } if(_host!=null || TopLevel.GetTopLevel(this) is not Window host)return; _host=host;host.Closing+=HostClosing; diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs index a1b8a3c..b483291 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs @@ -120,6 +120,24 @@ public class RecipeNode : EditorNode RemoveStepCommand = new DelegateCommand(node => { RemoveStep(node); Changed?.Invoke(); }, node => node is not null); MoveStepUpCommand = new DelegateCommand(node => { MoveStepUp(node); Changed?.Invoke(); }, node => node is not null && Steps.Any(s => s.Id < node.Id)); MoveStepDownCommand = new DelegateCommand(node => { MoveStepDown(node); Changed?.Invoke(); }, node => node is not null && Steps.Any(s => s.Id > node.Id)); + Parts.Items.CollectionChanged += (_, e) => + { + if (e.OldItems is not null) foreach (TextItem item in e.OldItems) item.PropertyChanged -= PartChanged; + if (e.NewItems is not null) foreach (TextItem item in e.NewItems) item.PropertyChanged += PartChanged; + RefreshPartNames(); + }; + } + + public EditableList Parts { get; } = new() { Title = "零件列表" }; + public ObservableCollection PartNames { get; } = [""]; + private void PartChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) => RefreshPartNames(); + private void RefreshPartNames() + { + // 替换选项时 SelectionConverter 保留当前引用,删除已引用名称由保存校验拦截。 + PartNames.Clear(); + PartNames.Add(""); + foreach (var name in Parts.Items.Select(p => p.Text).Where(n => !string.IsNullOrWhiteSpace(n)).Distinct()) PartNames.Add(name); + Changed?.Invoke(); } /// 加区域 @@ -385,6 +403,8 @@ public class RecipeNode : EditorNode }; node.Settings.CopyFrom(config.Sop.Settings); + foreach (var name in SopHelper.Engine.Training.PartCatalog.Read(config, null)) + node.Parts.Items.Add(new TextItem { Text = name, RemoveCommand = node.Parts.RemoveCommand }); foreach (var (key, def) in config.Sop.Regions) { @@ -437,6 +457,7 @@ public class RecipeNode : EditorNode config.Sop.Name = Name; config.Sop.Version = Version; config.Sop.Description = Description; + config.Sop.Parts = Parts.Items.Select(p => p.Text).ToList(); Settings.CopyTo(config.Sop.Settings); diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml index 03ec233..9e21dd4 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml @@ -6,7 +6,7 @@ - +