87 lines
6.1 KiB
C#
87 lines
6.1 KiB
C#
using OpenCvSharp;
|
|
using SopHelper.Engine.Common;
|
|
using SopHelper.Engine.Sop;
|
|
|
|
internal static class DynamicRegionChecks
|
|
{
|
|
public static async Task RunAsync()
|
|
{
|
|
static void Check(bool value, string message) { if (!value) throw new Exception(message); }
|
|
var options = new AiConfirmation { ImageMode = VisionImageMode.Hand, HandPadding = 20, HandMinSize = 100, HandMemoryMs = 3000 };
|
|
var step = new StepDef { Id = 1, Description = "零件已放好", AiConfirmation = options,
|
|
Detection = new() { Method = ConditionMethod.HandLeaveRegion, Region = "work", Hand = "right", StableFrames = 2, Tolerance = 1 } };
|
|
var regions = new Dictionary<string, DetRect> { ["work"] = new(0, 0, 300, 300) };
|
|
FrameResult Frame(params Hand[] hands) => new() { Size = (640, 480), Hands = [.. hands] };
|
|
Hand Right(float x, float y) => new() { HandType = HandType.Right, Rect = new(x, y, 20, 20) };
|
|
var clock = new ProbeClock(); var tracker = new HandImageRegionTracker(clock);
|
|
var empty = Frame();
|
|
tracker.Observe(step, empty, regions);
|
|
Check(tracker.Resolve(options, empty) is null, "从未见过手时不能发送整图代替跟手裁剪");
|
|
var first = Frame(Right(100, 100)); tracker.Observe(step, first, regions);
|
|
Check(tracker.Resolve(options, first) is { X1: 60, Y1: 60, X2: 160, Y2: 160 }, "手部框应按配置外扩并满足最小范围");
|
|
var moved = Frame(Right(200, 200)); tracker.Observe(step, moved, regions);
|
|
Check(tracker.Resolve(options, moved) is { X1: 160, Y1: 160 }, "裁剪应随手移动,而非固定在旧坐标");
|
|
var otherHand = Frame(new Hand { HandType = HandType.Left, Rect = new(10, 10, 20, 20) });
|
|
tracker.Observe(step, otherHand, regions);
|
|
Check(tracker.Resolve(options, otherHand) is { X1: 160, Y1: 160 }, "左手不能覆盖指定右手的位置");
|
|
var departed = Frame(Right(500, 400)); tracker.Observe(step, departed, regions);
|
|
Check(tracker.Resolve(options, departed) is { X1: 160, Y1: 160 }, "离开区域后应保留接触区域前的位置,不追逐已离开的手");
|
|
clock.Advance(3001);
|
|
Check(tracker.Resolve(options, empty) is null, "过期手部位置必须失效");
|
|
var corner = Frame(Right(0, 0)); tracker.Observe(step, corner, regions);
|
|
Check(tracker.Resolve(options, corner) is { X1: 0, Y1: 0, X2: 60, Y2: 60 }, "靠近画面边缘时应裁到有效边界");
|
|
step.Id = 2; tracker.Observe(step, empty, regions);
|
|
Check(tracker.Resolve(options, empty) is null, "下一步骤不能复用上一步的手部位置");
|
|
tracker.Observe(step, first, regions); tracker.Reset();
|
|
Check(tracker.Resolve(options, first) is null, "暂停复位等状态变更应清空位置");
|
|
tracker.Observe(step, first, regions);
|
|
var resized = new FrameResult { Size = (800, 600) }; tracker.Observe(step, resized, regions);
|
|
Check(tracker.Resolve(options, resized) is null, "相机尺寸改变后不得复用旧坐标");
|
|
Check(VisionImageMode.Resolve(new() { ImageRegion = "work" }) == VisionImageMode.FixedRegion
|
|
&& VisionImageMode.Resolve(new()) == VisionImageMode.FullFrame, "旧配方的固定区域和整图语义必须兼容");
|
|
Check(!VisionImageMode.IsValid(new() { ImageMode = "invalid" })
|
|
&& !VisionImageMode.IsValid(new() { ImageMode = "hand", HandPadding = -1 }), "未知取图方式或无效范围不能退回整图");
|
|
step.Detection!.Hand = "any";
|
|
var both = Frame(Right(50,50), new Hand { HandType=HandType.Left, Rect=new(200,200,20,20) });
|
|
tracker.Observe(step, both, regions);
|
|
Check(tracker.Resolve(options, both) is { X1: 30, Y1: 30, X2: 240, Y2: 240 }, "任意手模式应覆盖触发区域内的双手");
|
|
step.Detection.Hand = "right";
|
|
|
|
step.Id = 1;
|
|
var config = new SopConfig(); config.Sop.Name = "跟手送检"; config.Sop.Settings.AutoResetOnComplete = false;
|
|
config.Sop.Regions["work"] = new() { X1 = 0, Y1 = 0, X2 = 300, Y2 = 300 };
|
|
config.Sop.Steps.Add(step);
|
|
var parsed = SopConfigLoader.Parse(SopConfigLoader.ToYaml(config));
|
|
Check(parsed.Warnings.Count == 0 && parsed.Sop.Steps[0].AiConfirmation is { ImageMode: "hand", HandPadding: 20, HandMinSize: 100, HandMemoryMs: 3000 }, "跟手配置应正确往返保存");
|
|
var fake = new FakeDecision(); var engine = new SopEngine { VisionDecisionClient = fake };
|
|
engine.Load(config); engine.Start();
|
|
using var currentImage = new Mat(480, 640, MatType.CV_8UC3, new Scalar(0, 255, 0));
|
|
int captures = 0;
|
|
byte[] Capture(RegionDef? region)
|
|
{
|
|
Check(region is { X1: 60, Y1: 60, X2: 160, Y2: 160 }, "引擎应使用离开前的手部位置");
|
|
captures++; return VisionDecisionImage.Encode(currentImage, region);
|
|
}
|
|
engine.ProcessFrame(first, captureDecisionImage: Capture);
|
|
Check(captures == 0, "手仍在区域内时不能提前拍照");
|
|
engine.ProcessFrame(empty, captureDecisionImage: Capture); engine.ProcessFrame(empty, captureDecisionImage: Capture);
|
|
await fake.Called.Task.WaitAsync(TimeSpan.FromSeconds(5));
|
|
using (var sent = Cv2.ImDecode(fake.Request!.Jpeg, ImreadModes.Color))
|
|
Check(sent.Width == 100 && sent.Height == 100 && sent.At<Vec3b>(0, 0).Item1 > 240, "必须按历史位置裁剪当前新帧,不能发送历史图片");
|
|
engine.TogglePause(); engine.TogglePause();
|
|
engine.ProcessFrame(empty, captureDecisionImage: Capture);
|
|
engine.ProcessFrame(empty, captureDecisionImage: Capture);
|
|
Check(captures == 1, "暂停恢复不能沿用旧手部位置再次拍照");
|
|
engine.Stop();
|
|
Console.WriteLine("PASS: 跟手动态裁剪、当前帧送检、离开前定位、左右手过滤、过期/跨步骤/暂停/尺寸失效、旧配方兼容");
|
|
}
|
|
|
|
private sealed class ProbeClock : TimeProvider
|
|
{
|
|
private long _ticks;
|
|
public override long TimestampFrequency => 1000;
|
|
public override long GetTimestamp() => _ticks;
|
|
public void Advance(long milliseconds) => _ticks += milliseconds;
|
|
}
|
|
}
|