From c34be54f18572dca3025e08b8c215875ad11537f Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Tue, 10 Nov 2026 10:26:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4SOP=E5=BD=95=E5=83=8F?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E9=A1=BA=E5=BA=8F=E5=B9=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8B=96=E5=8A=A8=E6=92=AD=E6=94=BE=E8=BF=9B=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../Training/RecordedVideoPlayback.cs | 98 ++++++++++++++++++- .../VVMs/RecipeEditor/RecipeEditor.axaml | 10 +- tests/RecordingLimit.Smoke/Program.cs | 31 +++++- 4 files changed, 130 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index abf310c..5dfa475 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Windows Python 环境使用 `tools/Setup-Python.ps1 -Sam2`;Linux 环境和部 - 检测模型的类别表只读;维护零件列表不会更改已训练模型的类别映射。模型输出类别仍以训练版本为准。 - “SOP流程视频”页录制当前配方的完整标准操作,固定保存到 `recipes/配方名/sop_stand/sop.avi`,与零件训练录像分开存放。新配方先保存,再开启预览并开始录像。普通录像与SOP录像统一使用运行设置中选中的相机,录制界面只显示相机名称,不再选择或刷新设备。 - 支持暂停、继续和停止保存;暂停时间不计入视频,累计有效录像最长 5 分钟,到时自动保存并弹窗。关闭编辑窗口也会停止并保存录像。 - - 同页可播放已保存视频,支持暂停、继续、停止及播完重播,并显示播放进度和时间。播放不需要连接相机;开始播放时关闭相机预览,重新开启预览或关闭窗口时释放播放器。 + - 同页可播放已保存视频,支持暂停、继续、停止及播完重播,拖动进度条可前后跳转,并保持原来的播放或暂停状态。播放不需要连接相机;开始播放时关闭相机预览,重新开启预览或关闭窗口时释放播放器。 - 每份配方保留一段标准视频;“重新录制”先弹窗确认,确认后删除旧视频并重新开始,取消会保留原视频。录制中发起重新录制时会先暂停,取消后恢复原先的录制状态。 3. 零件标注与训练包含相机录像、视频导入、画框、SAM2 传播、复核、数据集导出、YOLO 训练和 ONNX 导出。 - 每段相机录像最长 5 分钟,达到上限后自动停止、保存文件并弹窗提示保存位置;相机预览继续运行,可再次开始录像。 diff --git a/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs b/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs index 0b8738a..3fd2230 100644 --- a/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs +++ b/src/SopHelper.Avalonia/Training/RecordedVideoPlayback.cs @@ -17,12 +17,25 @@ public sealed class RecordedVideoPlayback : BindableBase private bool _playing, _loaded, _atEnd, _reading; private double _fps, _duration, _position, _startPosition; private int _generation; + private string _path = ""; + private bool _seeking, _reportingProgress; + private double? _pendingSeek; + private Task _seekTask = Task.CompletedTask; public BitmapSource? Frame { get => _frame; private set { var old = _frame; SetProperty(ref _frame, value); old?.Dispose(); } } public bool IsPlaying { get => _playing; private set => SetProperty(ref _playing, value); } public bool IsLoaded { get => _loaded; private set => SetProperty(ref _loaded, value); } public bool IsAtEnd { get => _atEnd; private set => SetProperty(ref _atEnd, value); } - public double Progress => _duration > 0 ? Math.Clamp(_position / _duration * 100, 0, 100) : 0; + public bool IsSeeking => _seeking; + public double Progress + { + get => _duration > 0 ? Math.Clamp(_position / _duration * 100, 0, 100) : 0; + set + { + if (_reportingProgress || (!_seeking && Math.Abs(value - Progress) < .001)) return; + _ = SeekAsync(value); + } + } public string TimeText => $"{TimeSpan.FromSeconds(_position):mm\\:ss} / {TimeSpan.FromSeconds(_duration):mm\\:ss}"; public event Action? Failed; @@ -34,6 +47,8 @@ public sealed class RecordedVideoPlayback : BindableBase public async Task PlayAsync(string path) { + await _seekTask; + _path = path; // Reopen at EOF: some AVI backends cannot seek after their last Read failed. if (IsAtEnd) await StopAsync(); if (_capture is null) @@ -75,6 +90,9 @@ public sealed class RecordedVideoPlayback : BindableBase public async Task StopAsync() { Pause(); _generation++; + _pendingSeek = null; + await _seekTask; + _pendingSeek = null; await WaitForReadAsync(); _readTask = null; _reading = false; var capture = _capture; _capture = null; @@ -83,6 +101,75 @@ public sealed class RecordedVideoPlayback : BindableBase _position = 0; _duration = 0; NotifyTime(); } + public Task SeekAsync(double percent) + { + if (!IsLoaded || !double.IsFinite(percent)) return Task.CompletedTask; + _pendingSeek = Math.Clamp(percent, 0, 100); + if (!_seeking) _seekTask = SeekPendingAsync(); + return _seekTask; + } + + private async Task SeekPendingAsync() + { + _seeking = true; RaisePropertyChanged(nameof(IsSeeking)); _timer.Stop(); _clock.Stop(); + var generation = ++_generation; + try + { + await WaitForReadAsync(); + _readTask = null; _reading = false; + while (_pendingSeek is { } percent && generation == _generation) + { + _pendingSeek = null; + var old = _capture; _capture = null; + if (old is not null) await Task.Run(old.Dispose); + // Reopen for reliable backward seeking, including after EOF on AVI backends. + var opened = await Task.Run(() => + { + var capture = new VideoCapture(); + 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)); + if (target > 0) capture.Set(VideoCaptureProperties.PosFrames, target); + var decoded = Decode(capture, (long)target); + if (decoded.Image is null) throw new IOException("无法读取指定位置的视频画面"); + return (Capture: capture, Frame: decoded); + } + catch { capture.Dispose(); throw; } + }); + if (generation != _generation) + { + opened.Frame.Image?.Dispose(); + await Task.Run(opened.Capture.Dispose); + return; + } + _capture = opened.Capture; + Frame = opened.Frame.Image; + _position = percent == 100 ? _duration : opened.Frame.Seconds; + IsAtEnd = percent == 100; + if (IsAtEnd) Pause(); + NotifyTime(); + } + } + catch (Exception error) + { + if (generation == _generation) + { + Pause(); + Failed?.Invoke("视频跳转失败:" + error.Message); + } + } + finally + { + _seeking = false; + RaisePropertyChanged(nameof(IsSeeking)); + if (generation == _generation && IsPlaying && _capture is not null) + { + _startPosition = _position; _clock.Restart(); _timer.Start(); + } + } + } + private async Task WaitForReadAsync() { if (_readTask is null) return; @@ -91,7 +178,7 @@ public sealed class RecordedVideoPlayback : BindableBase private async Task ReadNextAsync() { - if (!IsPlaying || _capture is null || _reading) return; + if (!IsPlaying || _capture is null || _reading || _seeking) return; _reading = true; var capture = _capture; var generation = _generation; @@ -133,6 +220,11 @@ public sealed class RecordedVideoPlayback : BindableBase return new(image, Math.Max(0, (capture.PosFrames - 1) / _fps)); } - private void NotifyTime() { RaisePropertyChanged(nameof(Progress)); RaisePropertyChanged(nameof(TimeText)); } + private void NotifyTime() + { + _reportingProgress = true; + try { RaisePropertyChanged(nameof(Progress)); RaisePropertyChanged(nameof(TimeText)); } + finally { _reportingProgress = false; } + } private sealed record DecodedFrame(BitmapSource? Image, double Seconds); } diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml index 8697e8d..85c9500 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml @@ -444,10 +444,7 @@ - - -