仅在当前步骤需要时执行手部检测
This commit is contained in:
parent
46225993dc
commit
0596727b74
@ -91,6 +91,7 @@ public class SopPageViewModel : ViewModelBase
|
||||
_camera = camera;
|
||||
_pipeline = pipeline;
|
||||
_engine = engine;
|
||||
_pipeline.ShouldDetectHands = () => _engine.RequiresHandDetection;
|
||||
_dialogs = dialogs;
|
||||
_dispatcher = Dispatcher.CurrentDispatcher;
|
||||
|
||||
@ -736,6 +737,7 @@ public class SopPageViewModel : ViewModelBase
|
||||
LoadTrainedRecipeModel(config);
|
||||
_engine.Load(config);
|
||||
|
||||
var needsHands = config.Sop.Steps.Any(step => ConditionMethod.RequiresHandDetection(step.Detection?.Method));
|
||||
// 仅使用本配方训练发布的 ONNX。
|
||||
var options = new PipelineOptions
|
||||
{
|
||||
@ -743,8 +745,8 @@ public class SopPageViewModel : ViewModelBase
|
||||
YoloClasses = config.Sop.Model?.Classes ?? [],
|
||||
YoloInputSize = config.Sop.Model?.InputSize ?? 640,
|
||||
// 文件名必须和 models/ 下实际一致,否则 ResolveModel 返回 null、手部链路静默不跑
|
||||
PalmModelPath = ResolveModel("models/palm_detection_full_Nx3x192x192_post.onnx"),
|
||||
LandmarkModelPath = ResolveModel("models/hand_landmark_sparse_Nx3x224x224.onnx"),
|
||||
PalmModelPath = needsHands ? ResolveModel("models/palm_detection_full_Nx3x192x192_post.onnx") : null,
|
||||
LandmarkModelPath = needsHands ? ResolveModel("models/hand_landmark_sparse_Nx3x224x224.onnx") : null,
|
||||
FrameStride = Math.Max(1, config.Sop.Settings.FrameStride),
|
||||
RequireGpu = OperatingSystem.IsLinux(),
|
||||
};
|
||||
@ -773,7 +775,7 @@ public class SopPageViewModel : ViewModelBase
|
||||
foreach (var warning in _pipeline.Warnings)
|
||||
Log("warn", warning);
|
||||
|
||||
if (options.PalmModelPath is null || options.LandmarkModelPath is null)
|
||||
if (needsHands && (options.PalmModelPath is null || options.LandmarkModelPath is null))
|
||||
Log("warn", "手部模型缺失,手部检测及画面标记不可用,请检查 models 目录中的 palm / landmark ONNX 文件");
|
||||
}
|
||||
|
||||
|
||||
@ -72,6 +72,9 @@ public class DetectionPipeline : IDisposable
|
||||
public string HandBackend => _hand.Backend;
|
||||
public string YoloBackend => _yolo.Backend;
|
||||
|
||||
/// <summary>每帧查询当前步骤是否需要手部;未设置时保持独立管线的默认检测行为。</summary>
|
||||
public Func<bool>? ShouldDetectHands { get; set; }
|
||||
|
||||
/// <summary>结果帧(在管线线程上触发)</summary>
|
||||
public event Action<FrameResult>? ResultArrived;
|
||||
|
||||
@ -254,7 +257,7 @@ public class DetectionPipeline : IDisposable
|
||||
try { result.Detections = _yolo.Detect(frame.Bgr); }
|
||||
catch (Exception ex) { ReportInferenceWarning("YOLO 推理异常:" + ex.Message); }
|
||||
}
|
||||
if (_hand.IsReady)
|
||||
if (_hand.IsReady && (ShouldDetectHands?.Invoke() ?? true))
|
||||
{
|
||||
try { result.Hands = _hand.Detect(frame.Bgr); }
|
||||
catch (Exception ex) { ReportInferenceWarning("手部推理异常:" + ex.Message); }
|
||||
|
||||
@ -201,6 +201,10 @@ public class StepCondition
|
||||
/// </summary>
|
||||
public static class ConditionMethod
|
||||
{
|
||||
/// <summary>该判定方法是否需要手部推理。</summary>
|
||||
public static bool RequiresHandDetection(string? method) => method?.ToLowerInvariant() is
|
||||
HandInRegion or HandMove or HandNearObject;
|
||||
|
||||
/// <summary>目标出现在画面(1 次)</summary>
|
||||
public const string ObjectPresent = "object_present";
|
||||
|
||||
|
||||
@ -57,6 +57,10 @@ public class SopEngine
|
||||
/// <summary>当前步骤(未完成的那一步)</summary>
|
||||
public StepDef? CurrentStep => State is EngineState.Idle or EngineState.Completed ? null : StepAt(_index);
|
||||
|
||||
/// <summary>仅运行中的当前步骤需要手部条件时启用手部推理。</summary>
|
||||
public bool RequiresHandDetection => State == EngineState.Running
|
||||
&& ConditionMethod.RequiresHandDetection(CurrentStep?.Detection?.Method);
|
||||
|
||||
/// <summary>配方(UI 显示步骤卡用)</summary>
|
||||
public SopConfig Config => _config;
|
||||
|
||||
|
||||
@ -2,6 +2,39 @@ using System.Collections.Concurrent;
|
||||
using OpenCvSharp;
|
||||
using SopHelper.Engine.Camera;
|
||||
using SopHelper.Engine.Pipeline;
|
||||
using SopHelper.Engine.Sop;
|
||||
using SopHelper.Engine.Common;
|
||||
|
||||
// The first step must not enable hands merely because a later step uses them.
|
||||
var engine = new SopEngine();
|
||||
var config = new SopConfig();
|
||||
config.Sop.Settings.AutoResetOnComplete = false;
|
||||
config.Sop.Regions["work"] = new RegionDef { X1=0, Y1=0, X2=100, Y2=100 };
|
||||
config.Sop.Steps = [
|
||||
new StepDef { Id=1, Detection=new StepCondition { Method=ConditionMethod.ObjectPresent, TargetObject="part", StableFrames=1 } },
|
||||
new StepDef { Id=2, Detection=new StepCondition { Method=ConditionMethod.HandInRegion, Region="work", StableFrames=1 } },
|
||||
new StepDef { Id=3, Detection=new StepCondition { Method=ConditionMethod.ObjectPresent, TargetObject="part", StableFrames=1 } }
|
||||
];
|
||||
engine.Load(config);
|
||||
if(engine.RequiresHandDetection)throw new Exception("Idle engine enabled hands");
|
||||
engine.Start();
|
||||
if(engine.RequiresHandDetection)throw new Exception("Future hand step enabled hands early");
|
||||
var partFrame = new FrameResult { Detections=[new Detection { Label="part", Confidence=1 }] };
|
||||
engine.ProcessFrame(partFrame);
|
||||
if(!engine.RequiresHandDetection)throw new Exception("Entering hand step did not enable hands");
|
||||
engine.TogglePause();
|
||||
if(engine.RequiresHandDetection)throw new Exception("Paused step enabled hands");
|
||||
engine.TogglePause();
|
||||
if(!engine.RequiresHandDetection)throw new Exception("Resuming hand step did not enable hands");
|
||||
engine.ProcessFrame(new FrameResult { Hands=[new Hand { Rect=new DetRect(10,10,20,20) }] });
|
||||
if(engine.RequiresHandDetection)throw new Exception("Leaving hand step did not disable hands");
|
||||
engine.ProcessFrame(partFrame);
|
||||
if(engine.State!=EngineState.Completed||engine.RequiresHandDetection)throw new Exception("Completed engine enabled hands");
|
||||
engine.Reset();
|
||||
if(engine.RequiresHandDetection)throw new Exception("Reset kept previous hand requirement");
|
||||
foreach(var method in new[]{ConditionMethod.HandInRegion,ConditionMethod.HandMove,ConditionMethod.HandNearObject})
|
||||
if(!ConditionMethod.RequiresHandDetection(method.ToUpperInvariant()))throw new Exception("Hand method not recognized: "+method);
|
||||
Console.WriteLine("PASS: Hand inference follows only the current running step across transitions, pause, completion and reset");
|
||||
|
||||
using var camera = new TestCamera();
|
||||
using var pipeline = new DetectionPipeline(camera);
|
||||
@ -70,6 +103,21 @@ if (args.Length >= 3)
|
||||
var frame = await completion.Task.WaitAsync(TimeSpan.FromSeconds(10));pipeline.Stop();
|
||||
if (frame.Hands.Count == 0 || pipeline.Warnings.Count != 0) throw new Exception("Combined YOLO/hand inference failed");
|
||||
Console.WriteLine("PASS: Real YOLO and hand models process the same frame despite an outdated configured input size");
|
||||
var expectedDetections = frame.Detections.Select(d => (d.Label,d.Rect,d.Confidence)).ToArray();
|
||||
var detectHands = false;
|
||||
pipeline.ShouldDetectHands = () => detectHands;
|
||||
foreach (var enabled in new[]{false,true,false})
|
||||
{
|
||||
detectHands = enabled;
|
||||
completion = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
await pipeline.StartAsync();camera.Emit(200,image);
|
||||
frame = await completion.Task.WaitAsync(TimeSpan.FromSeconds(10));pipeline.Stop();
|
||||
if((frame.Hands.Count>0)!=enabled)throw new Exception("Hand inference did not follow the current requirement");
|
||||
if(!frame.Detections.Select(d => (d.Label,d.Rect,d.Confidence)).SequenceEqual(expectedDetections))
|
||||
throw new Exception("Disabling hands affected object detection");
|
||||
}
|
||||
pipeline.ShouldDetectHands = null;
|
||||
Console.WriteLine("PASS: Real hand detection stops and resumes without stale hands while YOLO continues");
|
||||
// Reproduce the original YOLO dimension exception after loading, to verify fault isolation.
|
||||
var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
|
||||
var yolo = typeof(DetectionPipeline).GetField("_yolo", flags)!.GetValue(pipeline)!;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user