aisop/src/SopHelper.Avalonia/Training/CameraFlowTest.cs

110 lines
4.4 KiB
C#

using Prism.Commands;
using SopHelper.Engine.Sop;
using OpenCvSharp;
using SopHelper.Presentation.Imaging;
namespace SopHelper.Training;
public sealed partial class CameraViewModel
{
private SopVideoFlowTest? _flowTest;
private Func<SopConfig>? _flowTestConfigProvider;
private bool _testingFlow;
private string _flowTestStatus = "", _flowTestDetails = "";
private BitmapSource? _flowTestDecisionImage;
private string _flowTestDecisionLabel = "";
public BitmapSource? FlowTestDecisionImage
{
get => _flowTestDecisionImage;
private set { var old = _flowTestDecisionImage; SetProperty(ref _flowTestDecisionImage, value); old?.Dispose(); }
}
public string FlowTestDecisionLabel { get => _flowTestDecisionLabel; private set => SetProperty(ref _flowTestDecisionLabel, value); }
public Func<SopConfig>? FlowTestConfigProvider
{
get => _flowTestConfigProvider;
set { _flowTestConfigProvider = value; UpdateCommands(); }
}
public DelegateCommand TestFlowCommand { get; }
public bool IsTestingFlow
{
get => _testingFlow;
private set { SetProperty(ref _testingFlow, value); StateChanged(); }
}
public bool CanSeekVideo => Playback.IsLoaded && !IsTestingFlow && !IsBusy;
public string FlowTestStatus { get => _flowTestStatus; private set => SetProperty(ref _flowTestStatus, value); }
public string FlowTestDetails { get => _flowTestDetails; private set => SetProperty(ref _flowTestDetails, value); }
private async Task StartFlowTestAsync()
{
if (IsRecording || IsTestingFlow) return;
await Playback.StopAsync();
EndFlowTest("开始新的测试");
FlowTestDetails = "";
FlowTestDecisionImage = null;
FlowTestDecisionLabel = "";
FlowTestStatus = "正在准备流程测试…";
try
{
var test = new SopVideoFlowTest(FlowTestConfigProvider!());
_flowTest = test;
IsTestingFlow = true;
if (IsPreviewing)
{
await Task.Run(_camera.Disconnect);
ClearPreview(); IsPreviewing = false;
}
test.Changed += (status, details) => _dispatcher.BeginInvoke(() =>
{
if (!ReferenceEquals(_flowTest, test) || _closing) return;
FlowTestStatus = status; FlowTestDetails = details;
});
test.DecisionImageCaptured += (jpeg, frameId) => _dispatcher.BeginInvoke(() =>
{
if (!ReferenceEquals(_flowTest, test) || _closing) return;
using var image = Cv2.ImDecode(jpeg, ImreadModes.Color);
var stride = checked((int)image.Step());
FlowTestDecisionImage = BitmapSource.Create(image.Width, image.Height, 96, 96, PixelFormats.Bgr24,
null, image.Data, checked(stride * image.Height), stride);
FlowTestDecisionLabel = $"AI 实际送检图 · 视频帧 {frameId}";
});
await Task.Run(test.LoadModels);
test.Start();
FlowTestDetails = test.Details;
FlowTestStatus = "正在播放标准视频并检测流程(使用当前编辑配置)";
Playback.FrameProcessor = test.ProcessFrame;
await Playback.PlayAsync(StandardVideoPath);
if (IsTestingFlow) Status = "正在测试流程;点击“停止播放”可停止测试";
}
catch (Exception error)
{
await Playback.StopAsync();
EndFlowTest("测试失败");
FlowTestStatus = "测试失败:" + error.Message;
throw;
}
}
// 仅在解码任务结束后调用,避免释放正在推理的模型。
private async Task CompleteFlowTestAsync()
{
var test = _flowTest;
if (test is null) return;
FlowTestStatus = "标准视频播放结束,正在确认已缓存的连续操作证据…";
await test.FinishPendingDecisionAsync();
if (ReferenceEquals(_flowTest, test)) EndFlowTest("标准视频播放结束");
}
private void EndFlowTest(string reason)
{
Playback.FrameProcessor = null;
if (_flowTest is { } test)
{
FlowTestStatus = test.Finish(reason);
FlowTestDetails = test.Details;
_flowTest = null;
test.Dispose();
}
IsTestingFlow = false;
}
}