67 lines
3.0 KiB
C#
67 lines
3.0 KiB
C#
using SopHelper.Engine.Common;
|
|
using SopHelper.Engine.Sop;
|
|
|
|
/// <summary>验证检测需求、原图坐标、稳定帧以及 AI 送检之间的衔接。</summary>
|
|
internal static class ObjectZoneChecks
|
|
{
|
|
public static async Task RunAsync()
|
|
{
|
|
var config = SopConfigLoader.Parse("""
|
|
sop:
|
|
name: 物品触发测试
|
|
model: { path: yolo_models/parts.onnx, classes: [box] }
|
|
regions:
|
|
work: { x1: 0, y1: 0, x2: 100, y2: 100 }
|
|
steps:
|
|
- id: 1
|
|
completion_criteria: 检查盒中物料
|
|
detection: { method: object_in_zone, region: work, target_object: Box, min_confidence: 0.8, stable_frames: 2, tolerance: 0 }
|
|
""");
|
|
var saved = SopConfigLoader.Parse(SopConfigLoader.ToYaml(config));
|
|
Check(saved.Warnings.Count == 0 && saved.Sop.Model?.Classes.Single() == "box"
|
|
&& saved.Sop.Steps[0].Detection?.TargetObject == "Box", "配方往返保留模型、目标类别及触发参数");
|
|
|
|
var fake = new FakeDecision();
|
|
var engine = new SopEngine { VisionDecisionClient = fake };
|
|
engine.Load(saved);
|
|
Check(!engine.RequiresObjectDetection, "空闲时不请求 YOLO");
|
|
engine.Start();
|
|
Check(engine.RequiresObjectDetection && !engine.RequiresHandDetection, "物品触发仅请求 YOLO");
|
|
long frameId = 0;
|
|
void Send(float x, float confidence = .9f, string label = "box", bool detected = true)
|
|
{
|
|
engine.ProcessFrame(new FrameResult
|
|
{
|
|
FrameId = ++frameId,
|
|
Timestamp = DateTime.Now,
|
|
ObjectDetectionRan = detected,
|
|
Detections = [new Detection { Label = label, Confidence = confidence, Rect = new(x, 10, 20, 20) }],
|
|
}, () => [1, 2]);
|
|
}
|
|
|
|
// 边缘相交但中心在区域外、低置信度、不同类别及未推理的帧都会中断连续计数。
|
|
Send(10); Send(95); Send(10); Send(10, .79f); Send(10); Send(10, label: "other");
|
|
Send(10); Send(10, detected: false); Send(10);
|
|
Check(!fake.Called.Task.IsCompleted, "需要两个连续有效命中帧");
|
|
Send(10);
|
|
await fake.Called.Task.WaitAsync(TimeSpan.FromSeconds(2));
|
|
Check(fake.Request?.FrameId == frameId && engine.State == EngineState.Running
|
|
&& !engine.RequiresObjectDetection, "命中帧用于送检,单步等待 AI 时停止物品推理");
|
|
engine.Stop();
|
|
Check(!engine.RequiresObjectDetection, "停止后不请求 YOLO");
|
|
|
|
saved.Sop.Steps[0].Detection!.Method = ConditionMethod.TimeElapsed;
|
|
engine.Load(saved);
|
|
engine.Start();
|
|
Check(!engine.RequiresObjectDetection, "保留模型配置的计时配方不会请求 YOLO");
|
|
engine.Stop();
|
|
|
|
Console.WriteLine("PASS: YOLO 需求门控、物品区域、稳定帧、配方往返及 AI 送检");
|
|
}
|
|
|
|
private static void Check(bool ok, string message)
|
|
{
|
|
if (!ok) throw new Exception(message);
|
|
}
|
|
}
|