94 lines
5.9 KiB
C#
94 lines
5.9 KiB
C#
using System.Diagnostics;
|
||
using System.Net.Http;
|
||
using System.Text.Json;
|
||
using OpenCvSharp;
|
||
using SopHelper.Engine.Sop;
|
||
using SopHelper.SopVideo;
|
||
|
||
/// <summary>真实手部模型与生产引擎的时序试验;截断时间仅控制输入视频,绝不作为判定条件。</summary>
|
||
internal static class SequenceTrialChecks
|
||
{
|
||
public static async Task RunAsync(string configPath, string videoPath, string settingsPath, string reportPath,
|
||
double stopSeconds, bool disableThinking)
|
||
{
|
||
Directory.CreateDirectory(reportPath);
|
||
var original = File.ReadAllBytes(configPath);
|
||
using var settings = JsonDocument.Parse(File.ReadAllText(settingsPath));
|
||
var root = settings.RootElement;
|
||
string Text(string key) => root.TryGetProperty(key, out var value) ? value.GetString() ?? "" : "";
|
||
using var http = new HttpClient { Timeout = Timeout.InfiniteTimeSpan };
|
||
var connection = new SequenceVisionConnection(Text("endpoint"), Text("model"), Text("apiKey"), disableThinking ? false : null);
|
||
using var test = new SopVideoFlowTest(SopConfigLoader.Load(configPath),
|
||
new ConfiguredVisionDecisionClient(http, new SequenceVisionDecisionClient(http, connection)));
|
||
var watch = Stopwatch.StartNew();
|
||
var requests = new List<object>(); var verdicts = new List<object>(); var triggers = new List<object>();
|
||
var events = new List<object>();
|
||
var candidates = new List<object>();
|
||
var observed = 0; var latestSeconds = 0d; var sequence = 0;
|
||
test.FrameObserved += (frame, seconds) => { observed++; latestSeconds = seconds; };
|
||
test.LogObserved += entry => events.Add(new { WallSeconds = watch.Elapsed.TotalSeconds, SourceSeconds = latestSeconds, entry.Level, entry.Message });
|
||
test.DecisionCandidatesObserved += (id, frames) =>
|
||
{
|
||
var bank = frames.DistinctBy(f => f.FrameId).ToArray();
|
||
foreach (var frame in bank)
|
||
File.WriteAllBytes(Path.Combine(reportPath, $"step-{id:D2}-frame-{frame.FrameId:D6}.jpg"), frame.Jpeg);
|
||
candidates.Add(new { StepId = id, CurrentVideoSeconds = latestSeconds,
|
||
Frames = bank.Select(f => new { f.FrameId, f.SourceSeconds, ImageFile = $"step-{id:D2}-frame-{f.FrameId:D6}.jpg" }).ToArray() });
|
||
};
|
||
test.TriggerEvaluated += (id, frame, result, phase) =>
|
||
{
|
||
if (result.IsMet) triggers.Add(new { Step = id, frame.FrameId, frame.SourceSeconds, Phase = phase });
|
||
};
|
||
test.DecisionSubmitted += request =>
|
||
{
|
||
var number = ++sequence;
|
||
var collected = request.Frames ?? [new VisionEvidenceFrame(request.FrameId, request.CapturedAt, request.Jpeg, request.SourceSeconds)];
|
||
foreach (var frame in collected)
|
||
{
|
||
var file = Path.Combine(reportPath, $"step-{request.StepId:D2}-frame-{frame.FrameId:D6}.jpg");
|
||
if (!File.Exists(file)) File.WriteAllBytes(file, frame.Jpeg);
|
||
}
|
||
var frames = collected.Select(f => new { f.FrameId, f.SourceSeconds, Bytes = f.Jpeg.Length,
|
||
ImageFile = $"step-{request.StepId:D2}-frame-{f.FrameId:D6}.jpg" }).ToArray();
|
||
requests.Add(new { Number = number, WallSeconds = watch.Elapsed.TotalSeconds, CurrentVideoSeconds = latestSeconds,
|
||
request.StepId, request.EvidenceMode, request.Criteria, request.TriggerObservation, Frames = frames });
|
||
Console.WriteLine($"提交 {number}: 步骤 {request.StepId},视频 {latestSeconds:F2}s,{frames.Length} 张真实画面");
|
||
};
|
||
test.DecisionEvaluated += (request, result, accepted) =>
|
||
{
|
||
verdicts.Add(new { WallSeconds = watch.Elapsed.TotalSeconds, CurrentVideoSeconds = latestSeconds,
|
||
request.StepId, request.FrameId, request.SourceSeconds, Accepted = accepted, Status = result.EffectiveStatus.ToString(),
|
||
ConfirmationFrameId = request.ConfirmationEvidence?.FrameId, ConfirmationSeconds = request.ConfirmationEvidence?.SourceSeconds,
|
||
result.Confidence, result.Reason, result.EvidenceFrameIds, result.CompletionFrameId });
|
||
Console.WriteLine($"返回: {result.EffectiveStatus}, 放行={accepted}, 完成证据={request.SourceSeconds:F2}s, {result.Reason}");
|
||
};
|
||
test.LoadModels(); test.Start();
|
||
using var capture = new VideoCapture(videoPath);
|
||
if (!capture.IsOpened()) throw new IOException("视频无法打开");
|
||
var fps = capture.Fps > 0 ? capture.Fps : 30;
|
||
using var image = new Mat();
|
||
var playback = Stopwatch.StartNew();
|
||
long id = 0;
|
||
while (capture.Read(image) && !image.Empty())
|
||
{
|
||
var seconds = id / fps;
|
||
if (seconds > stopSeconds) break;
|
||
var wait = TimeSpan.FromSeconds(seconds) - playback.Elapsed;
|
||
if (wait > TimeSpan.Zero) await Task.Delay(wait);
|
||
test.ProcessFrame(image, id, seconds); id++;
|
||
}
|
||
using var deadline = new CancellationTokenSource(TimeSpan.FromMinutes(6));
|
||
await test.FinishPendingDecisionAsync(deadline.Token);
|
||
var summary = test.Finish("真实时序试验结束");
|
||
File.WriteAllText(Path.Combine(reportPath, "result.json"), JsonSerializer.Serialize(new
|
||
{
|
||
Config = configPath, Video = videoPath, StopSeconds = double.IsFinite(stopSeconds) ? (double?)stopSeconds : null,
|
||
Completed = test.IsCompleted, Finished = test.IsFinished, Steps = test.StepResults,
|
||
Summary = summary, test.Details, Observed = observed, LatestSeconds = latestSeconds,
|
||
WallSeconds = watch.Elapsed.TotalSeconds, Requests = requests, Verdicts = verdicts, Triggers = triggers, Events = events, Candidates = candidates,
|
||
ConfigUnchanged = original.SequenceEqual(File.ReadAllBytes(configPath))
|
||
}, new JsonSerializerOptions { WriteIndented = true }));
|
||
Console.WriteLine(summary); Console.WriteLine("REPORT " + reportPath);
|
||
}
|
||
}
|