diff --git a/README.md b/README.md index 92afe03..bd1d09a 100644 --- a/README.md +++ b/README.md @@ -44,10 +44,13 @@ Windows Python 环境使用 `tools/Setup-Python.ps1 -Sam2`;Linux 环境和部 ## 操作流程 1. 接好 UVC 或海康相机,选择设备和配方,应用后开始检测。 -2. 配方编辑器包含步骤、区域、全局设置、零件列表、检测模型五个标签页;区域坐标使用原图像素。 +2. 配方编辑器包含步骤、区域、全局设置、零件列表、检测模型、SOP流程视频六个标签页;区域坐标使用原图像素。 - 零件名称只在“零件列表”维护,随配方保存到 `sop.parts`。标注类别、步骤目标类别、必留物品和完成必需物品均从该列表选择。 - 旧配方首次编辑时汇总已有训练类别、模型配置、步骤和标注项目名称;保存后列表成为唯一来源。重名、空名以及删除仍被步骤或标注项目引用的名称会阻止保存。 - 检测模型的类别表只读;维护零件列表不会更改已训练模型的类别映射。模型输出类别仍以训练版本为准。 + - “SOP流程视频”页录制当前配方的完整标准操作,固定保存到 `recipes/配方名/sop_stand/sop.avi`,与零件训练录像分开存放。新配方先保存,再选择相机、开启预览并开始录像。 + - 支持暂停、继续和停止保存;暂停时间不计入视频,累计有效录像最长 5 分钟,到时自动保存并弹窗。关闭编辑窗口也会停止并保存录像。 + - 每份配方保留一段标准视频;“重新录制”先弹窗确认,确认后删除旧视频并重新开始,取消会保留原视频。录制中发起重新录制时会先暂停,取消后恢复原先的录制状态。 3. 零件标注与训练包含相机录像、视频导入、画框、SAM2 传播、复核、数据集导出、YOLO 训练和 ONNX 导出。 - 每段相机录像最长 5 分钟,达到上限后自动停止、保存文件并弹窗提示保存位置;相机预览继续运行,可再次开始录像。 - 在标注页选择所属配方及已有零件类别,每个视频录入一个零件;录像和导入原视频保存在 `recipes/配方名/recordings`,标注数据保存在同配方的 `parts`。 diff --git a/src/SopHelper.Avalonia/Training/CameraViewModel.cs b/src/SopHelper.Avalonia/Training/CameraViewModel.cs index 731475f..a09d1f0 100644 --- a/src/SopHelper.Avalonia/Training/CameraViewModel.cs +++ b/src/SopHelper.Avalonia/Training/CameraViewModel.cs @@ -23,6 +23,7 @@ public sealed class CameraViewModel : BindableBase private readonly DispatcherTimer _timer; private readonly string _settingsPath; private readonly string? _recipeRecordingDirectory; + private readonly bool _sopStandard; private readonly Stopwatch _previewClock = Stopwatch.StartNew(); private Mat? _latest; private int _previewPending; @@ -30,6 +31,7 @@ public sealed class CameraViewModel : BindableBase private long _lastFrameTick; private volatile bool _closing; private bool _busy, _previewing, _recording; + private bool _paused; private string _status = "刷新设备列表,选择相机后开启预览"; private string _directory; private string _lastRecording = ""; @@ -47,6 +49,11 @@ public sealed class CameraViewModel : BindableBase public bool IsBusy { get => _busy; private set { SetProperty(ref _busy, value); StateChanged(); } } public bool IsPreviewing { get => _previewing; private set { SetProperty(ref _previewing, value); StateChanged(); } } public bool IsRecording { get => _recording; private set { SetProperty(ref _recording, value); StateChanged(); } } + public bool IsPaused { get => _paused; private set { SetProperty(ref _paused, value); StateChanged(); } } + public string PauseButtonText => IsPaused ? "继续录制" : "暂停录制"; + public string StandardVideoPath => Path.Combine(SaveDirectory, "sop.avi"); + public bool HasStandardVideo => _sopStandard && File.Exists(StandardVideoPath); + public Func>? ConfirmRestartAsync { get; set; } public bool CanChooseCamera => !IsBusy && !IsPreviewing && !_closing; public bool CanChangeDirectory => _recipeRecordingDirectory is null && !IsBusy && !IsRecording && !_closing; public string PreviewButtonText => IsPreviewing ? "关闭相机" : "开启相机预览"; @@ -60,16 +67,20 @@ public sealed class CameraViewModel : BindableBase public DelegateCommand RecordCommand { get; } public DelegateCommand BrowseCommand { get; } public DelegateCommand RevealCommand { get; } + public DelegateCommand PauseCommand { get; } + public DelegateCommand RestartCommand { get; } public event Action? RecordingLimitReached; - public CameraViewModel(ICameraService camera, string root, string? recipeName = null, TimeProvider? timeProvider = null) + public CameraViewModel(ICameraService camera, string root, string? recipeName = null, TimeProvider? timeProvider = null, bool sopStandard = false) { _recorder = new VideoRecorder(timeProvider); _camera = camera; _dispatcher = Dispatcher.CurrentDispatcher; var recipeDirectory = string.IsNullOrWhiteSpace(recipeName) ? null : new SopHelper.Engine.Training.RecipeWorkspace(root).DirectoryFor(recipeName); - _recipeRecordingDirectory = recipeDirectory is null ? null : Path.Combine(recipeDirectory, "recordings"); + _sopStandard = sopStandard; + if (sopStandard && recipeDirectory is null) throw new ArgumentException("SOP录像必须属于已保存的配方"); + _recipeRecordingDirectory = recipeDirectory is null ? null : Path.Combine(recipeDirectory, sopStandard ? "sop_stand" : "recordings"); _directory = _recipeRecordingDirectory ?? Path.Combine(root, "workspace", "recordings"); - _settingsPath = recipeDirectory is null ? Path.Combine(root, "workspace", "camera-settings.json") : Path.Combine(recipeDirectory, "camera-settings.json"); + _settingsPath = recipeDirectory is null ? Path.Combine(root, "workspace", "camera-settings.json") : Path.Combine(recipeDirectory, sopStandard ? "sop-camera-settings.json" : "camera-settings.json"); try { SaveDirectory = _recipeRecordingDirectory ?? ReadSaveDirectory(root); @@ -77,7 +88,9 @@ public sealed class CameraViewModel : BindableBase catch (Exception e) { Status = "录像目录配置读取失败:" + e.Message; } RefreshCommand = Command(RefreshAsync, () => CanChooseCamera); PreviewCommand = Command(TogglePreviewAsync, () => !IsBusy && !_closing && (IsPreviewing || SelectedCamera != null)); - RecordCommand = Command(ToggleRecordingAsync, () => !IsBusy && !_closing && (IsRecording || (IsPreviewing && Preview != null && HasRecentFrame))); + RecordCommand = Command(ToggleRecordingAsync, () => !IsBusy && !_closing && (IsRecording || (!HasStandardVideo && IsPreviewing && Preview != null && HasRecentFrame))); + PauseCommand = Command(() => { TogglePause(); return Task.CompletedTask; }, () => !IsBusy && !_closing && IsRecording); + RestartCommand = Command(RestartRecordingAsync, () => _sopStandard && !IsBusy && !_closing && HasStandardVideo && IsPreviewing && Preview != null && HasRecentFrame); BrowseCommand = Command(async () => { var picker = new OpenFolderDialog { Title = "选择录像保存目录" }; @@ -103,6 +116,7 @@ public sealed class CameraViewModel : BindableBase _camera.Error += OnError; _timer = new DispatcherTimer(TimeSpan.FromMilliseconds(250), DispatcherPriority.Background, (_, _) => Tick(), _dispatcher); _timer.Start(); + if (HasStandardVideo) { LastRecording = StandardVideoPath; RecordingInfo = "已有SOP流程视频"; Status = "已有标准操作录像;如需替换,请开启相机后点击“重新录制”"; } } private DelegateCommand Command(Func action, Func canExecute) @@ -123,6 +137,7 @@ public sealed class CameraViewModel : BindableBase { RaisePropertyChanged(nameof(CanChooseCamera)); RaisePropertyChanged(nameof(CanChangeDirectory)); RaisePropertyChanged(nameof(PreviewButtonText)); RaisePropertyChanged(nameof(RecordButtonText)); UpdateCommands(); + RaisePropertyChanged(nameof(PauseButtonText)); RaisePropertyChanged(nameof(HasStandardVideo)); } private async Task RefreshAsync() { @@ -156,6 +171,7 @@ public sealed class CameraViewModel : BindableBase private Task ToggleRecordingAsync() { if (IsRecording) { StopRecording(); return Task.CompletedTask; } + if (HasStandardVideo) throw new InvalidOperationException("已有SOP流程视频,请使用“重新录制”并确认删除旧视频"); if (string.IsNullOrWhiteSpace(SaveDirectory)) throw new ArgumentException("请选择录像保存目录"); var directory = Path.GetFullPath(SaveDirectory.Trim()); SaveDirectory = directory; SaveSettings(); @@ -163,21 +179,53 @@ public sealed class CameraViewModel : BindableBase { if (_latest == null || !_camera.IsGrabbing) throw new InvalidOperationException("尚未收到相机画面,请先开启预览"); if (Stopwatch.GetElapsedTime(_lastFrameTick).TotalSeconds > 2) throw new IOException("超过2秒未收到相机画面,请重新连接后再录像"); - LastRecording = _recorder.Start(directory, _latest, RecordingFps); + LastRecording = _recorder.Start(directory, _latest, RecordingFps, _sopStandard ? "sop.avi" : null); } IsRecording = true; Status = "正在录像,最长5分钟;到时自动停止并保存"; Tick(); return Task.CompletedTask; } + private void TogglePause() + { + lock (_frames) + { + if (IsPaused) _recorder.Resume(); else _recorder.Pause(); + IsPaused = _recorder.IsPaused; + } + Status = IsPaused ? "录制已暂停,暂停期间不计入视频时长;点击“继续录制”接着录制" : "已继续录制"; + Tick(); + } + private async Task RestartRecordingAsync() + { + if (ConfirmRestartAsync is null) throw new InvalidOperationException("请先确认删除旧SOP流程视频"); + var resumeOnCancel = IsRecording && !IsPaused; + if (resumeOnCancel) TogglePause(); + var confirmed = false; + try { confirmed = await ConfirmRestartAsync(); } + finally + { + if (!confirmed && resumeOnCancel && IsRecording && IsPaused) TogglePause(); + } + if (!confirmed) { Status = "已取消重新录制,原视频已保留"; return; } + // Do not delete until the camera is ready and the user has explicitly confirmed. + if (_closing || !_camera.IsGrabbing || !HasRecentFrame) throw new IOException("相机画面已中断,原视频保留,请重新连接后再试"); + StopRecording(); + if (!SopHelper.Engine.Training.RecipeWorkspace.IsWithin(StandardVideoPath, _recipeRecordingDirectory!)) + throw new IOException("SOP录像路径不在当前配方目录中"); + File.Delete(StandardVideoPath); + LastRecording = ""; + StateChanged(); + await ToggleRecordingAsync(); + } private void StopRecording() { RecordingResult? result; try { lock (_frames) result = _recorder.Stop(); } - finally { IsRecording = false; } + finally { IsRecording = false; IsPaused = false; } if (result != null) { LastRecording = result.Path; RecordingInfo = $"已保存 · {result.Duration.TotalSeconds:F1} 秒 · {result.Frames} 帧"; - Status = "录像已保存,可在主窗口导入视频继续标注"; + Status = _sopStandard ? "SOP流程视频已保存到当前配方的 sop_stand 目录" : "录像已保存,可在主窗口导入视频继续标注"; } } private void OnFrame(CameraFrame frame) @@ -208,7 +256,7 @@ public sealed class CameraViewModel : BindableBase if (recordingError != null) { var error = recordingError; - Post(() => { IsRecording = false; Status = error; RecordingInfo = "录像中断,请检查日志提示"; }); + Post(() => { IsRecording = false; IsPaused = false; Status = error; RecordingInfo = "录像中断,请检查日志提示"; }); } if (preview != null) { @@ -263,7 +311,7 @@ public sealed class CameraViewModel : BindableBase StopRecording(); Status = "超过2秒未收到相机画面,录像已结束,请检查相机连接"; } else if (!_recorder.IsRecording) IsRecording = false; - else RecordingInfo = $"● 正在录像 {_recorder.Elapsed:hh\\:mm\\:ss} / 00:05:00 · {RecordingFps:F0} fps"; + else RecordingInfo = $"{(IsPaused ? "Ⅱ 已暂停" : "● 正在录像")} {_recorder.Elapsed:hh\\:mm\\:ss} / 00:05:00 · {RecordingFps:F0} fps"; } if (IsPreviewing && !_camera.IsGrabbing && !IsBusy) { diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml index 9e21dd4..1e62349 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml @@ -440,6 +440,37 @@ + + + + + + + + +