From 25341c7fd285eedfae964223533f8b8f39061aa9 Mon Sep 17 00:00:00 2001
From: tfarcraw <123211521@163.com>
Date: Sat, 21 Nov 2026 11:05:56 +0800
Subject: [PATCH] =?UTF-8?q?=E6=AD=A5=E9=AA=A4=E7=BC=96=E8=BE=91=E6=94=AF?=
=?UTF-8?q?=E6=8C=81=E5=8D=8A=E9=80=9F=E6=92=AD=E6=94=BE=E5=92=8C=E9=80=90?=
=?UTF-8?q?=E5=B8=A7=E5=AE=9A=E4=BD=8D=E5=B9=B6=E8=B0=83=E6=95=B4=E5=85=B3?=
=?UTF-8?q?=E9=94=AE=E5=B8=A7=E6=8C=89=E9=92=AE=E9=A1=BA=E5=BA=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Training/CameraFlowTest.cs | 2 +
.../Training/CameraViewModel.cs | 6 ++
.../Training/RecordedVideoPlayback.cs | 52 ++++++++++++++---
.../VVMs/RecipeEditor/RecipeEditor.axaml | 8 ++-
tests/VideoFlowTest.Smoke/Program.cs | 57 +++++++++++++++++--
5 files changed, 110 insertions(+), 15 deletions(-)
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 @@
-
-
+
+
+
+
+
+
diff --git a/tests/VideoFlowTest.Smoke/Program.cs b/tests/VideoFlowTest.Smoke/Program.cs
index cbdccf6..5d85574 100644
--- a/tests/VideoFlowTest.Smoke/Program.cs
+++ b/tests/VideoFlowTest.Smoke/Program.cs
@@ -1,4 +1,4 @@
-using Avalonia;
+using Avalonia;
using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Markup.Xaml.Styling;
@@ -161,7 +161,7 @@ internal static class Program
var runtime = runtimeStore.Read() ?? new();
var originalTimeout = runtime.DjevTimeoutMs;
runtime.DjevTimeoutMs = 1000; runtimeStore.Save(runtime);
- try { CheckStridedHandPlayback(model.StandardVideoPath); CheckRealtimePlayback(model.StandardVideoPath); CheckContinuousPlayback(model.StandardVideoPath); }
+ try { CheckStridedHandPlayback(model.StandardVideoPath); CheckRealtimePlayback(model.StandardVideoPath); CheckContinuousPlayback(model.StandardVideoPath); CheckEditorPlayback(model.StandardVideoPath); }
finally { runtime.DjevTimeoutMs = originalTimeout; runtimeStore.Save(runtime); }
vm.Recipe.AddStepCommand.Execute();
var selected = vm.Recipe.SelectedStep!;
@@ -204,6 +204,8 @@ internal static class Program
Check(regionNavigation.TranslatePoint(default, window)!.Value.Y >= stepVideo.TranslatePoint(default, window)!.Value.Y + stepVideo.Bounds.Height
&& regionParameters.TranslatePoint(default, window)!.Value.Y >= stepVideo.TranslatePoint(default, window)!.Value.Y + stepVideo.Bounds.Height,
"Region navigation and parameters sit below the step video player");
+ // 前面的保存检查已停止播放器;先加载并暂停,确保界面绑定与逐帧命令使用真实视频画面。
+ Pump(model.Playback.PlayAsync(model.StandardVideoPath)); model.Playback.Pause();
Pump(model.Playback.SeekAsync(50)); window.UpdateLayout(); Dispatcher.UIThread.RunJobs();
var regionCanvas = view.FindControl("StepVideoRegions")!;
Check(ReferenceEquals(regionCanvas.Image, model.Playback.Frame) && ReferenceEquals(regionCanvas.Regions, vm.Recipe.Regions),
@@ -212,9 +214,21 @@ internal static class Program
Check(ReferenceEquals(regionCanvas.SelectedRegion, vm.Recipe.SelectedRegion)
&& ReferenceEquals(regionNavigation.SelectedItem, vm.Recipe.SelectedRegion),
"Region navigation, parameters and canvas share the selected editable region");
- var playbackButtons = ((Panel)view.FindControl