diff --git a/README.md b/README.md index 96fe17a..00b29c4 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ dotnet run --project tests/ShutdownSmoke/ShutdownSmoke.csproj -- --hardware ## 零件标注与训练 -侧栏「零件标注与训练」包含原 PartTrainer 的完整工作流:相机录像、导入视频与抽帧、手工画框、SAM2 双向传播、逐帧或一键复核、负样本与排除帧、YOLO 数据集导出、训练与 ONNX 导出、视频模型测试,以及可选视觉 API 建议框。切换到 SOP 页再返回会保留当前项目及任务。 +侧栏「零件标注与训练」包含以下工作流:相机录像、导入视频与抽帧、手工画框、SAM2 双向传播、逐帧或一键复核、负样本与排除帧、YOLO 数据集导出、训练与 ONNX 导出,以及可选视觉 API 建议框。切换到 SOP 页再返回会保留当前项目及任务。 首次使用 Python 功能,在 SopHelper 根目录执行: diff --git a/src/SopHelper.App/Training/MainViewModel.cs b/src/SopHelper.App/Training/MainViewModel.cs index c7a8cc0..59e129d 100644 --- a/src/SopHelper.App/Training/MainViewModel.cs +++ b/src/SopHelper.App/Training/MainViewModel.cs @@ -35,7 +35,6 @@ public sealed class MainViewModel : BindableBase public string TrainingRunDirectory { get => _trainingRunDirectory; private set => SetProperty(ref _trainingRunDirectory, value); } public string TrainingOutputDirectory { get; set; } = ""; public string Root { get; } - internal WorkerClient Worker => _worker; public TrainingProject? Project => _project; public ObservableCollection Frames { get; } = []; public bool IsBusy { get => _busy; private set { SetProperty(ref _busy, value); RaisePropertyChanged(nameof(IsIdle)); Commands.ForEach(c => c.RaiseCanExecuteChanged()); } } diff --git a/src/SopHelper.App/Training/ModelTestViewModel.cs b/src/SopHelper.App/Training/ModelTestViewModel.cs deleted file mode 100644 index 1d041be..0000000 --- a/src/SopHelper.App/Training/ModelTestViewModel.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System.IO; -using System.Text.Json; -using System.Windows; -using System.Windows.Media.Imaging; -using Microsoft.Win32; -using SopHelper.Engine.Training; -using Prism.Commands; -using Prism.Mvvm; - -namespace SopHelper.Training; - -public sealed class ModelTestViewModel : BindableBase -{ - private readonly string _root; - private readonly string _python; - private readonly WorkerClient _worker; - private CancellationTokenSource? _stop; - private Task _running = Task.CompletedTask; - private bool _busy; - private string _video = "", _model = "", _status = "选择视频和检测模型,点击开始"; - private string _detections = "尚无检测结果", _position = "", _logs = ""; - private BitmapImage? _preview; - private double _progress; - public bool IsBusy { get => _busy; private set { SetProperty(ref _busy, value); RaisePropertyChanged(nameof(IsIdle)); UpdateCommands(); } } - public bool IsIdle => !IsBusy; - public string VideoPath { get => _video; set { SetProperty(ref _video, value); UpdateCommands(); } } - public string ModelPath { get => _model; set { SetProperty(ref _model, value); UpdateCommands(); } } - public double Confidence { get; set; } = 0.25; - public string Device { get; set; } = "auto"; - public int ImageSize { get; set; } = 640; - public string Status { get => _status; private set => SetProperty(ref _status, value); } - public string Detections { get => _detections; private set => SetProperty(ref _detections, value); } - public string Position { get => _position; private set => SetProperty(ref _position, value); } - public string Logs { get => _logs; private set => SetProperty(ref _logs, value); } - public BitmapImage? Preview { get => _preview; private set => SetProperty(ref _preview, value); } - public double Progress { get => _progress; private set => SetProperty(ref _progress, value); } - public DelegateCommand ChooseVideoCommand { get; } - public DelegateCommand ChooseModelCommand { get; } - public DelegateCommand StartCommand { get; } - public DelegateCommand StopCommand { get; } - - public ModelTestViewModel(string root, string python, WorkerClient? worker = null) - { - _root = root; _python = python; _worker = worker ?? new WorkerClient(); - ChooseVideoCommand = new DelegateCommand(() => - { - var picker = new OpenFileDialog { Filter = "视频|*.mp4;*.avi;*.mov;*.mkv;*.wmv|所有文件|*.*" }; - if (picker.ShowDialog() == true) VideoPath = picker.FileName; - }, () => IsIdle); - ChooseModelCommand = new DelegateCommand(() => - { - var picker = new OpenFileDialog { Filter = "YOLO检测模型|*.pt;*.onnx" }; - if (picker.ShowDialog() == true) ModelPath = picker.FileName; - }, () => IsIdle); - StartCommand = new DelegateCommand(() => _running = PlayAsync(), () => IsIdle && File.Exists(VideoPath) && File.Exists(ModelPath)); - StopCommand = new DelegateCommand(() => _stop?.Cancel(), () => IsBusy); - } - private void UpdateCommands() - { - ChooseVideoCommand?.RaiseCanExecuteChanged(); ChooseModelCommand?.RaiseCanExecuteChanged(); - StartCommand?.RaiseCanExecuteChanged(); StopCommand?.RaiseCanExecuteChanged(); - } - private async Task PlayAsync() - { - if (IsBusy) return; - IsBusy = true; Preview = null; Progress = 0; Position = ""; Detections = "等待检测画面"; - Status = "正在加载模型…"; Logs = ""; - using var stop = new CancellationTokenSource(); _stop = stop; - string? lastError = null; - try - { - await _worker.RunAsync(_python, Path.Combine(_root,"tools","worker.py"), new - { action = "test_video", video = VideoPath, model = ModelPath, confidence = Confidence, device = Device, imageSize = ImageSize }, line => - { - using var json = Parse(line); - var item = json?.RootElement; - Application.Current.Dispatcher.Invoke(() => - { - if (item is { ValueKind: JsonValueKind.Object } data && data.TryGetProperty("event", out var kind) && kind.GetString() == "test_frame") - { - try - { - using var stream = new MemoryStream(Convert.FromBase64String(data.GetProperty("image").GetString()!)); - var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; - bitmap.StreamSource = stream; bitmap.EndInit(); bitmap.Freeze(); Preview = bitmap; - var frame = data.GetProperty("frame").GetInt32(); - var total = data.GetProperty("totalFrames").GetInt32(); - var seconds = data.GetProperty("seconds").GetDouble(); - Position = $"{seconds:F1} / {data.GetProperty("duration").GetDouble():F1} 秒 · 帧 {frame}/{total} · {data.GetProperty("processingFps").GetDouble():F1} fps"; - Progress = total > 0 ? Math.Clamp(frame * 100.0 / total,0,100) : 0; - var targets = data.GetProperty("detections").EnumerateArray().ToList(); - Detections = targets.Count == 0 ? "当前帧未检测到目标" : string.Join(Environment.NewLine, targets.Select(d => $"{d.GetProperty("label").GetString()} {d.GetProperty("confidence").GetDouble():P1}")); - Status = $"正在播放检测 · {targets.Count} 个目标"; - } - catch (Exception e) { lastError = "检测画面读取失败:" + e.Message; stop.Cancel(); } - return; - } - var message = item is { ValueKind: JsonValueKind.Object } obj && obj.TryGetProperty("message",out var m) ? m.GetString() ?? line : line; - if (item is { ValueKind: JsonValueKind.Object } error && error.TryGetProperty("error",out var flag) && flag.ValueKind == JsonValueKind.True) lastError = message; - Status = message; Logs += message + Environment.NewLine; - if (Logs.Length > 15000) Logs = Logs[^12000..]; - }); - }, stop.Token); - } - catch (OperationCanceledException) { Status = lastError ?? "已停止播放检测"; } - catch (Exception e) { Status = lastError ?? e.Message; Logs += Status + Environment.NewLine; } - finally { _stop = null; IsBusy = false; } - } - private static JsonDocument? Parse(string line) - { - try { return JsonDocument.Parse(line); } catch (JsonException) { return null; } - } - public async Task StopAsync() { _stop?.Cancel(); await _running; } -} diff --git a/src/SopHelper.App/Training/ModelTestWindow.xaml b/src/SopHelper.App/Training/ModelTestWindow.xaml deleted file mode 100644 index cbe9c37..0000000 --- a/src/SopHelper.App/Training/ModelTestWindow.xaml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - -