调整SOP录像按钮顺序并支持拖动播放进度
This commit is contained in:
parent
823027e7e6
commit
c34be54f18
@ -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 分钟,达到上限后自动停止、保存文件并弹窗提示保存位置;相机预览继续运行,可再次开始录像。
|
||||
|
||||
@ -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<string>? 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);
|
||||
}
|
||||
|
||||
@ -444,10 +444,7 @@
|
||||
<Grid Margin="10" RowDefinitions="Auto,*">
|
||||
<TextBlock Text="录制完整标准操作流程,视频单独保存在当前配方的 sop_stand 目录。暂停期间不计时,累计录制最长5分钟。新配方请先保存。" TextWrapping="Wrap" Margin="0,0,0,10" />
|
||||
<Grid Grid.Row="1" DataContext="{Binding SopRecording}" IsVisible="{Binding DataContext.HasSopRecording, RelativeSource={RelativeSource AncestorType=UserControl}}" RowDefinitions="Auto,*,Auto,Auto,Auto">
|
||||
<Grid ColumnDefinitions="*,Auto" Margin="0,0,0,10">
|
||||
<TextBlock Text="{Binding SelectedCameraName}" VerticalAlignment="Center" TextWrapping="Wrap" Margin="0,0,8,0" />
|
||||
<Button Grid.Column="1" Content="{Binding PreviewButtonText}" Command="{Binding PreviewCommand}" />
|
||||
</Grid>
|
||||
<TextBlock Text="{Binding SelectedCameraName}" TextWrapping="Wrap" Margin="0,0,0,10" />
|
||||
<Border Grid.Row="1" Background="#080E14" CornerRadius="6" MinHeight="180">
|
||||
<Grid>
|
||||
<TextBlock Text="使用运行设置中选中的相机,开启预览后录制标准操作流程" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource FgDim}" />
|
||||
@ -456,15 +453,16 @@
|
||||
</Border>
|
||||
<StackPanel Grid.Row="2" Spacing="8" Margin="0,12,0,8">
|
||||
<StackPanel Orientation="Horizontal" Spacing="8">
|
||||
<Button Content="{Binding PreviewButtonText}" Command="{Binding PreviewCommand}" />
|
||||
<Button Content="{Binding RecordButtonText}" Command="{Binding RecordCommand}" />
|
||||
<Button Content="{Binding PauseButtonText}" Command="{Binding PauseCommand}" />
|
||||
<Button Content="重新录制" Command="{Binding RestartCommand}" />
|
||||
<Button Content="打开保存位置" Command="{Binding RevealCommand}" />
|
||||
<Button Content="{Binding PlayVideoButtonText}" Command="{Binding PlayVideoCommand}" />
|
||||
<Button Content="停止播放" Command="{Binding StopVideoCommand}" />
|
||||
<Button Content="打开保存位置" Command="{Binding RevealCommand}" />
|
||||
</StackPanel>
|
||||
<Grid ColumnDefinitions="*,Auto">
|
||||
<ProgressBar Minimum="0" Maximum="100" Value="{Binding Playback.Progress}" Height="6" VerticalAlignment="Center" />
|
||||
<Slider x:Name="VideoSeekSlider" Minimum="0" Maximum="100" Value="{Binding Playback.Progress, Mode=TwoWay}" IsEnabled="{Binding Playback.IsLoaded}" SmallChange="1" LargeChange="10" VerticalAlignment="Center" ToolTip.Tip="拖动进度条跳转播放位置" />
|
||||
<TextBlock Grid.Column="1" Text="{Binding Playback.TimeText}" Margin="12,0,0,0" />
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
|
||||
@ -13,7 +13,10 @@ using SopHelper.Engine.Sop;
|
||||
using SopHelper.Engine.Training;
|
||||
using SopHelper.VVMs.RecipeEditor;
|
||||
using Prism.Dialogs;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Window = Avalonia.Controls.Window;
|
||||
using Point = Avalonia.Point;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
@ -98,9 +101,13 @@ internal static class Program
|
||||
// Substitute the camera at the presentation boundary; never open real test hardware.
|
||||
typeof(RecipeEditorViewModel).GetProperty(nameof(vm.SopRecording))!.SetValue(vm, model);
|
||||
var editor = new RecipeEditor { DataContext = vm };
|
||||
var window = new Window { Content = editor, Width = 1180, Height = 820 }; window.Show(); window.UpdateLayout();
|
||||
var window = new Window { Content = editor, Width = 1040, Height = 760 }; window.Show(); window.UpdateLayout();
|
||||
var tabs = editor.GetVisualDescendants().OfType<TabControl>().Single(); tabs.SelectedIndex = 5; window.UpdateLayout();
|
||||
Check(tabs.ItemCount == 6 && ((TabItem)tabs.Items[5]!).Header?.ToString() == "SOP流程视频", "Recipe editor contains the SOP recording page");
|
||||
var previewButton = editor.GetVisualDescendants().OfType<Button>().Single(b => ReferenceEquals(b.Command, model.PreviewCommand));
|
||||
var videoButtons = ((StackPanel)previewButton.Parent!).Children.OfType<Button>().ToArray();
|
||||
Check(ReferenceEquals(videoButtons[0].Command, model.PreviewCommand) && ReferenceEquals(videoButtons[1].Command, model.RecordCommand) && ReferenceEquals(videoButtons[^1].Command, model.RevealCommand), "Preview precedes recording and saved location is last");
|
||||
Check(videoButtons[^1].TranslatePoint(new Point(videoButtons[^1].Bounds.Width, 0), editor)!.Value.X <= editor.Bounds.Width, "Video controls fit the minimum editor width");
|
||||
Check(!editor.GetVisualDescendants().OfType<Button>().Any(b => b.Content?.ToString() == "刷新相机"), "SOP page has no independent camera refresh");
|
||||
model.PreviewCommand.Execute(); Thread.Sleep(75); camera.Emit(); Dispatcher.UIThread.RunJobs();
|
||||
Check(ReferenceEquals(camera.ConnectedCamera, selected) && camera.EnumerationCount == 0, "SOP recording also uses the global camera directly");
|
||||
@ -124,7 +131,27 @@ internal static class Program
|
||||
model.PlayVideoCommand.Execute(); PumpIdle(model); PumpFor(100);
|
||||
var pausedProgress = model.Playback.Progress; PumpFor(100);
|
||||
Check(!model.Playback.IsPlaying && model.Playback.Progress == pausedProgress && model.DisplayImage is not null, "Playback pause holds the frame and progress");
|
||||
Pump(model.Playback.SeekAsync(80));
|
||||
Check(!model.Playback.IsPlaying && Math.Abs(model.Playback.Progress - 80) < 2, "Paused video seeks forward without starting playback");
|
||||
Pump(model.Playback.SeekAsync(20));
|
||||
Check(!model.Playback.IsPlaying && Math.Abs(model.Playback.Progress - 20) < 2, "Backward seek updates the displayed frame");
|
||||
var seekSlider = editor.FindControl<Slider>("VideoSeekSlider")!; window.UpdateLayout();
|
||||
var thumb = seekSlider.GetVisualDescendants().OfType<Thumb>().First();
|
||||
var start = thumb.TranslatePoint(new Point(thumb.Bounds.Width / 2, thumb.Bounds.Height / 2), window)!.Value;
|
||||
var end = seekSlider.TranslatePoint(new Point(seekSlider.Bounds.Width * .6, seekSlider.Bounds.Height / 2), window)!.Value;
|
||||
window.MouseDown(start, MouseButton.Left); window.MouseMove(end); window.MouseUp(end, MouseButton.Left);
|
||||
var seekWait = System.Diagnostics.Stopwatch.StartNew();
|
||||
while (model.Playback.IsSeeking && seekWait.Elapsed < TimeSpan.FromSeconds(5)) { Dispatcher.UIThread.RunJobs(); Thread.Sleep(1); }
|
||||
Check(!model.Playback.IsPlaying && model.Playback.Progress is > 50 and < 70, "Dragging the actual slider seeks the paused video");
|
||||
Pump(model.Playback.SeekAsync(100));
|
||||
Check(model.Playback.IsAtEnd && model.Playback.Progress == 100 && model.DisplayImage is not null, "Seeking to the end shows the last frame");
|
||||
Pump(model.Playback.SeekAsync(0));
|
||||
Check(!model.Playback.IsAtEnd && model.Playback.Progress == 0 && !model.Playback.IsPlaying, "Seeking from EOF back to the beginning preserves pause");
|
||||
model.PlayVideoCommand.Execute(); PumpIdle(model);
|
||||
Pump(model.Playback.SeekAsync(80));
|
||||
Check(model.Playback.IsPlaying && Math.Abs(model.Playback.Progress - 80) < 2, "Seeking while playing resumes at the requested position");
|
||||
_ = model.Playback.SeekAsync(10); _ = model.Playback.SeekAsync(90); Pump(model.Playback.SeekAsync(30));
|
||||
Check(model.Playback.IsPlaying && Math.Abs(model.Playback.Progress - 30) < 2, "Rapid seeks use the final requested position");
|
||||
var playbackWait = System.Diagnostics.Stopwatch.StartNew();
|
||||
while (model.Playback.IsPlaying && playbackWait.Elapsed < TimeSpan.FromSeconds(4)) PumpFor(20);
|
||||
Check(model.Playback.IsAtEnd && model.Playback.Progress == 100 && model.PlayVideoButtonText == "重新播放", "Playback stops at the end and offers replay");
|
||||
@ -160,7 +187,9 @@ internal static class Program
|
||||
Check(reopened.PlayVideoCommand.CanExecute() && !reopened.PreviewCommand.CanExecute(), "Existing videos can play without a selected or connected camera");
|
||||
reopened.PlayVideoCommand.Execute(); PumpIdle(reopened);
|
||||
Check(reopened.Playback.IsPlaying && reopened.DisplayImage is not null, "Reopened recipe plays its saved video");
|
||||
var pendingSeek = reopened.Playback.SeekAsync(70);
|
||||
Pump(reopened.ShutdownAsync());
|
||||
Pump(pendingSeek);
|
||||
Check(!reopened.Playback.IsLoaded, "Closing playback disposes the video reader");
|
||||
using (var exclusive = new FileStream(expectedPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
||||
Check(exclusive.Length > 0, "Video file is unlocked after player shutdown");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user