diff --git a/src/SopHelper.Avalonia/Training/CameraFlowTest.cs b/src/SopHelper.Avalonia/Training/CameraFlowTest.cs index 7b52c76..fc5296f 100644 --- a/src/SopHelper.Avalonia/Training/CameraFlowTest.cs +++ b/src/SopHelper.Avalonia/Training/CameraFlowTest.cs @@ -56,6 +56,8 @@ public sealed partial class CameraViewModel if (IsRecording || IsTestingFlow) return; await Playback.StopAsync(); EndFlowTest("开始新的测试"); + // 回测按现场实时时钟推进,不沿用步骤编辑的半速设置。 + Playback.HalfSpeed = false; FlowTestDetails = ""; FlowTestDecisionImage = null; FlowTestDecisionLabel = ""; diff --git a/src/SopHelper.Avalonia/Training/CameraViewModel.cs b/src/SopHelper.Avalonia/Training/CameraViewModel.cs index ce3f883..dda221c 100644 --- a/src/SopHelper.Avalonia/Training/CameraViewModel.cs +++ b/src/SopHelper.Avalonia/Training/CameraViewModel.cs @@ -75,6 +75,9 @@ public sealed partial class CameraViewModel : BindableBase public DelegateCommand PlayVideoCommand { get; } public DelegateCommand PauseVideoCommand { get; } public DelegateCommand ToggleVideoPlaybackCommand { get; } + /// 步骤编辑逐帧定位,执行后保持暂停以便获取关键帧。 + public DelegateCommand PreviousVideoFrameCommand { get; } + public DelegateCommand NextVideoFrameCommand { get; } public DelegateCommand StopVideoCommand { get; } public DelegateCommand ImportVideoCommand { get; } public event Action? RecordingLimitReached; @@ -110,11 +113,14 @@ public sealed partial class CameraViewModel : BindableBase ImportVideoCommand = Command(ImportVideoAsync, () => _sopStandard && !IsBusy && !_closing && !IsTestingFlow && !IsRecording); TestFlowCommand = Command(StartFlowTestAsync, () => _sopStandard && !IsBusy && !_closing && !IsTestingFlow && !IsRecording && HasStandardVideo && FlowTestConfigProvider is not null); StopVideoCommand = Command(async () => { await Playback.StopAsync(); EndFlowTest("测试已停止"); Status = "视频播放已停止"; }, () => !IsBusy && !_closing && Playback.IsLoaded); + PreviousVideoFrameCommand = Command(() => Playback.StepFrameAsync(-1), () => CanSeekVideo && !_closing && !Playback.IsSeeking); + NextVideoFrameCommand = Command(() => Playback.StepFrameAsync(1), () => CanSeekVideo && !_closing && !Playback.IsSeeking); Playback.PropertyChanged += (_, e) => { if (e.PropertyName is nameof(Playback.Frame) or nameof(Playback.IsLoaded)) RaisePropertyChanged(nameof(DisplayImage)); if (e.PropertyName is nameof(Playback.IsPlaying) or nameof(Playback.IsLoaded) or nameof(Playback.IsAtEnd)) { RaisePropertyChanged(nameof(PlayVideoButtonText)); UpdateCommands(); } + if (e.PropertyName == nameof(Playback.IsSeeking)) UpdateCommands(); if (e.PropertyName == nameof(Playback.IsLoaded)) { RaisePropertyChanged(nameof(CanSeekVideo)); RaisePropertyChanged(nameof(ShowFlowTestOverlay)); diff --git a/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs b/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs index 67bd8b2..8f8681e 100644 --- a/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs +++ b/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs @@ -22,7 +22,9 @@ public sealed class RecordedVideoPlayback : BindableBase private int _generation; private string _path = ""; private bool _seeking, _reportingProgress; - private double? _pendingSeek; + private (long Frame, bool AtEnd)? _pendingSeek; + private long _frameCount; + private bool _halfSpeed; private Task _seekTask = Task.CompletedTask; public BitmapSource? Frame { get => _frame; private set { var old = _frame; SetProperty(ref _frame, value); old?.Dispose(); } } @@ -40,6 +42,19 @@ public sealed class RecordedVideoPlayback : BindableBase _ = SeekAsync(value); } } + /// 普通播放可切换半速;切换时重设时钟基准,避免播放位置突跳。 + public bool HalfSpeed + { + get => _halfSpeed; + set + { + if (_halfSpeed == value || FrameProcessor is not null || FrameAnalyzer is not null) return; + _startPosition = _position; + _clock.Restart(); + SetProperty(ref _halfSpeed, value); + } + } + public string TimeText => $"{TimeSpan.FromSeconds(_position):mm\\:ss} / {TimeSpan.FromSeconds(_duration):mm\\:ss}"; public event Action? Failed; /// 在解码线程处理即将显示的原始帧;Mat 仅在回调期间有效。 @@ -80,7 +95,8 @@ public sealed class RecordedVideoPlayback : BindableBase { _fps = capture.Fps; if (!double.IsFinite(_fps) || _fps <= 0) _fps = 30; - _duration = capture.FrameCount / _fps; + _frameCount = capture.FrameCount; + _duration = _frameCount / _fps; if (!double.IsFinite(_duration) || _duration <= 0) throw new IOException("视频中没有可播放画面"); _position = 0; IsLoaded = true; @@ -118,7 +134,24 @@ public sealed class RecordedVideoPlayback : BindableBase public Task SeekAsync(double percent) { if (!IsLoaded || FrameProcessor is not null || FrameAnalyzer is not null || !double.IsFinite(percent)) return Task.CompletedTask; - _pendingSeek = Math.Clamp(percent, 0, 100); + percent = Math.Clamp(percent, 0, 100); + return SeekFrameAsync((long)Math.Floor(percent / 100 * _frameCount), percent == 100); + } + + /// 按当前显示帧前后移动一帧并暂停;首尾钳位,流程分析期间禁止切帧。 + public async Task StepFrameAsync(int direction) + { + if (!IsLoaded || FrameProcessor is not null || FrameAnalyzer is not null || direction == 0) return; + Pause(); + await _seekTask; + if (!IsLoaded) return; + var frame = (long)Math.Round(DisplayedSeconds * _fps); + await SeekFrameAsync(frame + Math.Sign(direction), false); + } + + private Task SeekFrameAsync(long frame, bool atEnd) + { + _pendingSeek = (Math.Clamp(frame, 0, Math.Max(0, _frameCount - 1)), atEnd); if (!_seeking) _seekTask = SeekPendingAsync(); return _seekTask; } @@ -132,7 +165,7 @@ public sealed class RecordedVideoPlayback : BindableBase { await WaitForReadAsync(); _readTask = null; _reading = false; - while (_pendingSeek is { } percent && generation == _generation) + while (_pendingSeek is { } seek && generation == _generation) { _pendingSeek = null; var old = _capture; _capture = null; @@ -144,7 +177,7 @@ public sealed class RecordedVideoPlayback : BindableBase try { if (!capture.Open(_path)) throw new IOException("无法打开录制的视频"); - var target = Math.Clamp((long)Math.Floor(percent / 100 * capture.FrameCount), 0, Math.Max(0, capture.FrameCount - 1)); + var target = seek.Frame; if (target > 0) capture.Set(VideoCaptureProperties.PosFrames, target); var decoded = Decode(capture, (long)target); if (decoded.Image is null) throw new IOException("无法读取指定位置的视频画面"); @@ -162,8 +195,8 @@ public sealed class RecordedVideoPlayback : BindableBase Frame = opened.Frame.Image; DisplayedSeconds = opened.Frame.Seconds; Analysis = opened.Frame.Analysis; - _position = percent == 100 ? _duration : opened.Frame.Seconds; - IsAtEnd = percent == 100; + _position = seek.AtEnd ? _duration : opened.Frame.Seconds; + IsAtEnd = seek.AtEnd; if (IsAtEnd) Pause(); NotifyTime(); } @@ -201,7 +234,10 @@ public sealed class RecordedVideoPlayback : BindableBase var capture = _capture; var generation = _generation; // 测试与现场一样按实时时钟推进,AI 在后台判定,不阻塞后续画面。 - var target = (long)Math.Floor((_startPosition + _clock.Elapsed.TotalSeconds) * _fps); + var rate = HalfSpeed && FrameProcessor is null && FrameAnalyzer is null ? 0.5 : 1; + var target = (long)Math.Floor((_startPosition + _clock.Elapsed.TotalSeconds * rate) * _fps); + // 半速及高频定时器下,同一目标帧尚未到期时不提前读取下一帧。 + if (capture.PosFrames > target) { _reading = false; return; } var read = _readTask = Task.Run(() => Decode(capture, target)); try { diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml index 0fdf8c3..2562448 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml @@ -290,8 +290,12 @@