1012 lines
30 KiB
C#
1012 lines
30 KiB
C#
using System.Collections.ObjectModel;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using Prism.Commands;
|
||
using SopHelper.Core.Prism;
|
||
using SopHelper.Engine.Sop;
|
||
|
||
namespace SopHelper.VVMs.RecipeEditor;
|
||
|
||
/// <summary>
|
||
/// 编辑器自己的可观察节点。配方模型(SopHelper.Engine)是纯数据类、不带 INPC,
|
||
/// 引擎层不该为 UI 引这套接口,所以绑定层在这一侧,保存时整体映射回 SopConfig。
|
||
/// </summary>
|
||
public abstract class EditorNode : BindableBase
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 可被"可编辑列表"删除的行
|
||
/// </summary>
|
||
public interface IRemovable
|
||
{
|
||
/// <summary>删除自己(由所属列表注入)</summary>
|
||
ICommand? RemoveCommand { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 字符串列表项(YAML 的 classes / must_keep / required_objects)。
|
||
/// 不能直接绑 string——string 不实现 INPC,TwoWay 写不回集合项。
|
||
/// </summary>
|
||
public class TextItem : EditorNode, IRemovable
|
||
{
|
||
private string _text = "";
|
||
|
||
/// <summary>文本</summary>
|
||
public string Text
|
||
{
|
||
get { return _text; }
|
||
set { SetProperty(ref _text, value); }
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public ICommand? RemoveCommand { get; set; }
|
||
|
||
/// <inheritdoc />
|
||
public override string ToString() => Text;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 可增删的行列表(类别名、必在物料、违规规则都用它)
|
||
/// </summary>
|
||
public class EditableList<T> : EditorNode where T : EditorNode, IRemovable, new()
|
||
{
|
||
/// <summary>列表标题</summary>
|
||
public string Title { get; init; } = "";
|
||
|
||
/// <summary>行</summary>
|
||
public ObservableCollection<T> Items { get; } = [];
|
||
|
||
/// <summary>加一行</summary>
|
||
public DelegateCommand AddCommand { get; }
|
||
|
||
/// <summary>删一行</summary>
|
||
public DelegateCommand<T> RemoveCommand { get; }
|
||
|
||
/// <summary>
|
||
/// 可增删的行列表
|
||
/// </summary>
|
||
public EditableList()
|
||
{
|
||
AddCommand = new DelegateCommand(() =>
|
||
{
|
||
var item = new T { RemoveCommand = RemoveCommand };
|
||
Items.Add(item);
|
||
});
|
||
|
||
RemoveCommand = new DelegateCommand<T>(item =>
|
||
{
|
||
if (item is not null) Items.Remove(item);
|
||
});
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// sop 根节点
|
||
/// </summary>
|
||
public class RecipeNode : EditorNode
|
||
{
|
||
private string _fileName = "";
|
||
private bool _isNew;
|
||
private string _name = "";
|
||
private string _version = "1.0";
|
||
private string _description = "";
|
||
private bool _hasModel;
|
||
private RecipeModelNode? _model;
|
||
private RegionNode? _selectedRegion;
|
||
private StepNode? _selectedStep;
|
||
|
||
/// <summary>
|
||
/// 配方根节点
|
||
/// </summary>
|
||
public RecipeNode()
|
||
{
|
||
Methods =
|
||
[
|
||
"",
|
||
ConditionMethod.TimeElapsed,
|
||
ConditionMethod.HandInRegion,
|
||
ConditionMethod.HandMove,
|
||
ConditionMethod.HandAction,
|
||
ConditionMethod.HandNearObject,
|
||
ConditionMethod.ObjectPresent,
|
||
ConditionMethod.ObjectInZone,
|
||
ConditionMethod.PersonPresent,
|
||
];
|
||
|
||
Hands = ["any", "left", "right"];
|
||
Actions = ["", "pickup", "putdown"];
|
||
ViolationTypes = [ViolationType.Timeout, ViolationType.WrongOrder, ViolationType.ObjectRemoved];
|
||
|
||
AddRegionCommand = new DelegateCommand(() => { AddRegion(); Changed?.Invoke(); });
|
||
RemoveRegionCommand = new DelegateCommand<RegionNode>(node => { RemoveRegion(node); Changed?.Invoke(); }, node => node is not null);
|
||
|
||
AddStepCommand = new DelegateCommand(() => { AddStep(); Changed?.Invoke(); });
|
||
RemoveStepCommand = new DelegateCommand<StepNode>(node => { RemoveStep(node); Changed?.Invoke(); }, node => node is not null);
|
||
MoveStepUpCommand = new DelegateCommand<StepNode>(node => { MoveStepUp(node); Changed?.Invoke(); }, node => node is not null && Steps.Any(s => s.Id < node.Id));
|
||
MoveStepDownCommand = new DelegateCommand<StepNode>(node => { MoveStepDown(node); Changed?.Invoke(); }, node => node is not null && Steps.Any(s => s.Id > node.Id));
|
||
}
|
||
|
||
/// <summary>加区域</summary>
|
||
public DelegateCommand AddRegionCommand { get; }
|
||
|
||
/// <summary>删区域</summary>
|
||
public DelegateCommand<RegionNode> RemoveRegionCommand { get; }
|
||
|
||
/// <summary>加步骤</summary>
|
||
public DelegateCommand AddStepCommand { get; }
|
||
|
||
/// <summary>删步骤</summary>
|
||
public DelegateCommand<StepNode> RemoveStepCommand { get; }
|
||
|
||
/// <summary>步骤上移</summary>
|
||
public DelegateCommand<StepNode> MoveStepUpCommand { get; }
|
||
|
||
/// <summary>步骤下移</summary>
|
||
public DelegateCommand<StepNode> MoveStepDownCommand { get; }
|
||
|
||
/// <summary>列表结构变了(ViewModel 借此重跑校验)</summary>
|
||
public event Action? Changed;
|
||
|
||
/// <summary>文件名(不含扩展名;新建时可填,之后按它落盘)</summary>
|
||
public string FileName
|
||
{
|
||
get { return _fileName; }
|
||
set { SetProperty(ref _fileName, value); }
|
||
}
|
||
|
||
/// <summary>新建(尚未落盘)</summary>
|
||
public bool IsNew
|
||
{
|
||
get { return _isNew; }
|
||
private set
|
||
{
|
||
if (!SetProperty(ref _isNew, value)) return;
|
||
RaisePropertyChanged(nameof(CanEditFileName));
|
||
}
|
||
}
|
||
|
||
/// <summary>文件名是否可编辑(只有新建能改——SOP 页按文件名选配方,已存在的改名等于换个配方)</summary>
|
||
public bool CanEditFileName
|
||
{
|
||
get { return IsNew; }
|
||
}
|
||
|
||
/// <summary>配方名(sop.name)</summary>
|
||
public string Name
|
||
{
|
||
get { return _name; }
|
||
set { SetProperty(ref _name, value); }
|
||
}
|
||
|
||
/// <summary>版本(sop.version)</summary>
|
||
public string Version
|
||
{
|
||
get { return _version; }
|
||
set { SetProperty(ref _version, value); }
|
||
}
|
||
|
||
/// <summary>说明(sop.description)</summary>
|
||
public string Description
|
||
{
|
||
get { return _description; }
|
||
set { SetProperty(ref _description, value); }
|
||
}
|
||
|
||
/// <summary>全局设置(sop.settings)</summary>
|
||
public RecipeSettingsNode Settings { get; } = new();
|
||
|
||
/// <summary>区域(sop.regions,键 = 配方里引用的区域名)</summary>
|
||
public ObservableCollection<RegionNode> Regions { get; } = [];
|
||
|
||
/// <summary>步骤(sop.steps)</summary>
|
||
public ObservableCollection<StepNode> Steps { get; } = [];
|
||
|
||
/// <summary>是否配置检测模型(不勾就不写 sop.model 节点)</summary>
|
||
public bool HasModel
|
||
{
|
||
get { return _hasModel; }
|
||
set
|
||
{
|
||
if (!SetProperty(ref _hasModel, value)) return;
|
||
|
||
// 勾上给一个可编辑的模型节点,取消勾选就丢弃
|
||
_model = value ? _model ?? new RecipeModelNode() : null;
|
||
RaisePropertyChanged(nameof(Model));
|
||
}
|
||
}
|
||
|
||
/// <summary>模型定义(HasModel=false 时为 null)</summary>
|
||
public RecipeModelNode? Model
|
||
{
|
||
get { return _model; }
|
||
private set { SetProperty(ref _model, value); }
|
||
}
|
||
|
||
/// <summary>当前编辑的区域</summary>
|
||
public RegionNode? SelectedRegion
|
||
{
|
||
get { return _selectedRegion; }
|
||
set { SetProperty(ref _selectedRegion, value); }
|
||
}
|
||
|
||
/// <summary>当前编辑的步骤</summary>
|
||
public StepNode? SelectedStep
|
||
{
|
||
get { return _selectedStep; }
|
||
set { SetProperty(ref _selectedStep, value); }
|
||
}
|
||
|
||
/// <summary>method 下拉候选(空串 = 本步不写判定条件)</summary>
|
||
public IReadOnlyList<string> Methods { get; }
|
||
|
||
/// <summary>hand 下拉候选</summary>
|
||
public IReadOnlyList<string> Hands { get; }
|
||
|
||
/// <summary>action 下拉候选(hand_action 用)</summary>
|
||
public IReadOnlyList<string> Actions { get; }
|
||
|
||
/// <summary>违规类型下拉候选</summary>
|
||
public IReadOnlyList<string> ViolationTypes { get; }
|
||
|
||
/// <summary>区域名下拉候选(含空项,表示该字段不引用区域)</summary>
|
||
public IReadOnlyList<string> RegionNames
|
||
{
|
||
get
|
||
{
|
||
var names = Regions.Select(r => r.Key).Where(k => !string.IsNullOrWhiteSpace(k)).ToList();
|
||
names.Insert(0, "");
|
||
return names;
|
||
}
|
||
}
|
||
|
||
/// <summary>加区域</summary>
|
||
public RegionNode AddRegion()
|
||
{
|
||
var node = new RegionNode
|
||
{
|
||
Key = $"region{Regions.Count + 1}",
|
||
Name = "新区域",
|
||
X1 = 100,
|
||
Y1 = 100,
|
||
X2 = 400,
|
||
Y2 = 400,
|
||
};
|
||
|
||
node.Renamed += OnRegionRenamed;
|
||
Regions.Add(node);
|
||
SelectedRegion = node;
|
||
|
||
return node;
|
||
}
|
||
|
||
/// <summary>删区域(条件里引用了它的步骤会清掉引用,避免留下悬空引用)</summary>
|
||
public void RemoveRegion(RegionNode? node)
|
||
{
|
||
if (node is null) return;
|
||
|
||
Regions.Remove(node);
|
||
node.Renamed -= OnRegionRenamed;
|
||
|
||
foreach (var step in Steps.Where(s => s.Detection is not null && References(s.Detection, node.Key)))
|
||
ClearRegionReferences(step.Detection!, node.Key);
|
||
|
||
SelectedRegion = Regions.FirstOrDefault();
|
||
}
|
||
|
||
/// <summary>加步骤(Id 取当前最大 +1,执行顺序按 Id 升序)</summary>
|
||
public StepNode AddStep()
|
||
{
|
||
var node = new StepNode
|
||
{
|
||
Id = Steps.Count == 0 ? 1 : Steps.Max(s => s.Id) + 1,
|
||
Name = "新步骤",
|
||
Detection = new ConditionNode(),
|
||
};
|
||
|
||
Steps.Add(node);
|
||
SelectedStep = node;
|
||
|
||
return node;
|
||
}
|
||
|
||
/// <summary>删步骤</summary>
|
||
public void RemoveStep(StepNode? node)
|
||
{
|
||
if (node is null) return;
|
||
|
||
Steps.Remove(node);
|
||
SelectedStep = Steps.FirstOrDefault();
|
||
}
|
||
|
||
/// <summary>上移(与上一个交换 Id——执行顺序就是 Id 升序)</summary>
|
||
public void MoveStepUp(StepNode? node)
|
||
{
|
||
if (node is null) return;
|
||
|
||
var prev = Steps.Where(s => s.Id < node.Id).OrderByDescending(s => s.Id).FirstOrDefault();
|
||
if (prev is null) return;
|
||
|
||
(node.Id, prev.Id) = (prev.Id, node.Id);
|
||
ResortSteps();
|
||
}
|
||
|
||
/// <summary>下移</summary>
|
||
public void MoveStepDown(StepNode? node)
|
||
{
|
||
if (node is null) return;
|
||
|
||
var next = Steps.Where(s => s.Id > node.Id).OrderBy(s => s.Id).FirstOrDefault();
|
||
if (next is null) return;
|
||
|
||
(node.Id, next.Id) = (next.Id, node.Id);
|
||
ResortSteps();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 区域键名变了 → 同步条件里的引用。不做这一步,改个区域名就得把每个引用它的步骤手改一遍。
|
||
/// </summary>
|
||
private void OnRegionRenamed(string from, string to)
|
||
{
|
||
if (string.Equals(from, to, StringComparison.OrdinalIgnoreCase)) return;
|
||
|
||
foreach (var step in Steps)
|
||
{
|
||
var condition = step.Detection;
|
||
if (condition is null) continue;
|
||
|
||
if (string.Equals(condition.Region, from, StringComparison.OrdinalIgnoreCase)) condition.Region = to;
|
||
if (string.Equals(condition.FromRegion, from, StringComparison.OrdinalIgnoreCase)) condition.FromRegion = to;
|
||
if (string.Equals(condition.ToRegion, from, StringComparison.OrdinalIgnoreCase)) condition.ToRegion = to;
|
||
}
|
||
|
||
RaisePropertyChanged(nameof(RegionNames));
|
||
}
|
||
|
||
/// <summary>按 Id 重排(保持选中项)</summary>
|
||
private void ResortSteps()
|
||
{
|
||
var selected = SelectedStep;
|
||
|
||
var ordered = Steps.OrderBy(s => s.Id).ToList();
|
||
Steps.Clear();
|
||
foreach (var step in ordered) Steps.Add(step);
|
||
|
||
SelectedStep = selected;
|
||
}
|
||
|
||
private static bool References(ConditionNode condition, string key)
|
||
=> new[] { condition.Region, condition.FromRegion, condition.ToRegion }
|
||
.Any(n => string.Equals(n, key, StringComparison.OrdinalIgnoreCase));
|
||
|
||
private static void ClearRegionReferences(ConditionNode condition, string key)
|
||
{
|
||
if (string.Equals(condition.Region, key, StringComparison.OrdinalIgnoreCase)) condition.Region = "";
|
||
if (string.Equals(condition.FromRegion, key, StringComparison.OrdinalIgnoreCase)) condition.FromRegion = "";
|
||
if (string.Equals(condition.ToRegion, key, StringComparison.OrdinalIgnoreCase)) condition.ToRegion = "";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 配方 → 编辑器节点
|
||
/// </summary>
|
||
public static RecipeNode From(SopConfig config, string fileName, bool isNew)
|
||
{
|
||
var node = new RecipeNode
|
||
{
|
||
FileName = System.IO.Path.GetFileNameWithoutExtension(fileName),
|
||
IsNew = isNew,
|
||
Name = config.Sop.Name,
|
||
Version = config.Sop.Version,
|
||
Description = config.Sop.Description,
|
||
};
|
||
|
||
node.Settings.CopyFrom(config.Sop.Settings);
|
||
|
||
foreach (var (key, def) in config.Sop.Regions)
|
||
{
|
||
var region = new RegionNode { Key = key };
|
||
region.CopyFrom(def);
|
||
region.Renamed += node.OnRegionRenamed;
|
||
node.Regions.Add(region);
|
||
}
|
||
|
||
foreach (var step in config.Sop.Steps.OrderBy(s => s.Id))
|
||
{
|
||
var stepNode = new StepNode
|
||
{
|
||
Id = step.Id,
|
||
Name = step.Name,
|
||
Description = step.Description,
|
||
Timeout = step.Timeout,
|
||
StandardDurationMs = step.StandardDurationMs,
|
||
};
|
||
|
||
foreach (var item in step.MustKeep) stepNode.MustKeep.Items.Add(new TextItem { Text = item, RemoveCommand = stepNode.MustKeep.RemoveCommand });
|
||
foreach (var item in step.RequiredObjects) stepNode.RequiredObjects.Items.Add(new TextItem { Text = item, RemoveCommand = stepNode.RequiredObjects.RemoveCommand });
|
||
|
||
if (step.Detection is not null) stepNode.Detection = ConditionNode.From(step.Detection);
|
||
|
||
foreach (var rule in step.ViolationRules)
|
||
stepNode.ViolationRules.Items.Add(ViolationRuleNode.From(rule, stepNode.ViolationRules.RemoveCommand));
|
||
|
||
node.Steps.Add(stepNode);
|
||
}
|
||
|
||
if (config.Sop.Model is not null)
|
||
{
|
||
node.HasModel = true;
|
||
node.Model?.CopyFrom(config.Sop.Model);
|
||
}
|
||
|
||
node.SelectedStep = node.Steps.FirstOrDefault();
|
||
node.SelectedRegion = node.Regions.FirstOrDefault();
|
||
|
||
return node;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 编辑器节点 → 配方(全新构造一份,不动原对象)
|
||
/// </summary>
|
||
public SopConfig ToConfig()
|
||
{
|
||
var config = new SopConfig();
|
||
|
||
config.Sop.Name = Name;
|
||
config.Sop.Version = Version;
|
||
config.Sop.Description = Description;
|
||
|
||
Settings.CopyTo(config.Sop.Settings);
|
||
|
||
foreach (var region in Regions.Where(r => !string.IsNullOrWhiteSpace(r.Key)))
|
||
{
|
||
var def = new RegionDef { Name = region.Name, Description = region.Description, Color = region.Color };
|
||
region.CopyTo(def);
|
||
config.Sop.Regions[region.Key.Trim()] = def;
|
||
}
|
||
|
||
foreach (var step in Steps.OrderBy(s => s.Id))
|
||
{
|
||
var def = new StepDef
|
||
{
|
||
Id = step.Id,
|
||
Name = step.Name,
|
||
Description = step.Description,
|
||
Timeout = step.Timeout,
|
||
StandardDurationMs = step.StandardDurationMs,
|
||
};
|
||
|
||
def.MustKeep.AddRange(step.MustKeep.Items.Select(i => i.Text).Where(s => !string.IsNullOrWhiteSpace(s)));
|
||
def.RequiredObjects.AddRange(step.RequiredObjects.Items.Select(i => i.Text).Where(s => !string.IsNullOrWhiteSpace(s)));
|
||
|
||
if (step.Detection is not null && !string.IsNullOrWhiteSpace(step.Detection.Method))
|
||
def.Detection = step.Detection.ToCondition();
|
||
|
||
foreach (var rule in step.ViolationRules.Items.Where(r => !string.IsNullOrWhiteSpace(r.Type)))
|
||
def.ViolationRules.Add(rule.ToRule());
|
||
|
||
config.Sop.Steps.Add(def);
|
||
}
|
||
|
||
// 只有勾了模型才写 sop.model:留一个 path 为空的节点会让校验里"需要模型"那几条提醒失灵
|
||
if (HasModel && Model is not null)
|
||
{
|
||
var def = new ModelDef { Path = Model.Path, InputSize = Model.InputSize };
|
||
def.Classes.AddRange(Model.Classes.Items.Select(c => c.Text).Where(c => !string.IsNullOrWhiteSpace(c)));
|
||
config.Sop.Model = def;
|
||
}
|
||
|
||
return config;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// sop.settings
|
||
/// </summary>
|
||
public class RecipeSettingsNode : EditorNode
|
||
{
|
||
private float _confidenceThreshold = 0.6f;
|
||
private int _stableFrames = 5;
|
||
private float _positionTolerance = 20f;
|
||
private bool _enableTimeoutDetection = true;
|
||
private bool _enableSkipDetection = true;
|
||
private bool _autoResetOnComplete = true;
|
||
private int _resetDelaySec = 1;
|
||
private int _frameStride = 1;
|
||
|
||
/// <summary>全局置信度阈值</summary>
|
||
public float ConfidenceThreshold
|
||
{
|
||
get { return _confidenceThreshold; }
|
||
set { SetProperty(ref _confidenceThreshold, value); }
|
||
}
|
||
|
||
/// <summary>默认稳定帧数(条件未写 stable_frames 时用它)</summary>
|
||
public int StableFrames
|
||
{
|
||
get { return _stableFrames; }
|
||
set { SetProperty(ref _stableFrames, value); }
|
||
}
|
||
|
||
/// <summary>区域命中容差(像素)</summary>
|
||
public float PositionTolerance
|
||
{
|
||
get { return _positionTolerance; }
|
||
set { SetProperty(ref _positionTolerance, value); }
|
||
}
|
||
|
||
/// <summary>启用超时违规</summary>
|
||
public bool EnableTimeoutDetection
|
||
{
|
||
get { return _enableTimeoutDetection; }
|
||
set { SetProperty(ref _enableTimeoutDetection, value); }
|
||
}
|
||
|
||
/// <summary>启用跳步/错序违规</summary>
|
||
public bool EnableSkipDetection
|
||
{
|
||
get { return _enableSkipDetection; }
|
||
set { SetProperty(ref _enableSkipDetection, value); }
|
||
}
|
||
|
||
/// <summary>整轮完成后自动复位</summary>
|
||
public bool AutoResetOnComplete
|
||
{
|
||
get { return _autoResetOnComplete; }
|
||
set { SetProperty(ref _autoResetOnComplete, value); }
|
||
}
|
||
|
||
/// <summary>复位前停留秒数</summary>
|
||
public int ResetDelaySec
|
||
{
|
||
get { return _resetDelaySec; }
|
||
set { SetProperty(ref _resetDelaySec, value); }
|
||
}
|
||
|
||
/// <summary>抽帧间隔(1=每帧都判)</summary>
|
||
public int FrameStride
|
||
{
|
||
get { return _frameStride; }
|
||
set { SetProperty(ref _frameStride, value); }
|
||
}
|
||
|
||
/// <summary>读入</summary>
|
||
public void CopyFrom(SopSettings from)
|
||
{
|
||
ConfidenceThreshold = from.ConfidenceThreshold;
|
||
StableFrames = from.StableFrames;
|
||
PositionTolerance = from.PositionTolerance;
|
||
EnableTimeoutDetection = from.EnableTimeoutDetection;
|
||
EnableSkipDetection = from.EnableSkipDetection;
|
||
AutoResetOnComplete = from.AutoResetOnComplete;
|
||
ResetDelaySec = from.ResetDelaySec;
|
||
FrameStride = from.FrameStride;
|
||
}
|
||
|
||
/// <summary>写出</summary>
|
||
public void CopyTo(SopSettings to)
|
||
{
|
||
to.ConfidenceThreshold = ConfidenceThreshold;
|
||
to.StableFrames = StableFrames;
|
||
to.PositionTolerance = PositionTolerance;
|
||
to.EnableTimeoutDetection = EnableTimeoutDetection;
|
||
to.EnableSkipDetection = EnableSkipDetection;
|
||
to.AutoResetOnComplete = AutoResetOnComplete;
|
||
to.ResetDelaySec = ResetDelaySec;
|
||
to.FrameStride = FrameStride;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// sop.model
|
||
/// </summary>
|
||
public class RecipeModelNode : EditorNode
|
||
{
|
||
private string _path = "";
|
||
private int _inputSize = 640;
|
||
|
||
/// <summary>onnx 路径(相对程序目录)</summary>
|
||
public string Path
|
||
{
|
||
get { return _path; }
|
||
set { SetProperty(ref _path, value); }
|
||
}
|
||
|
||
/// <summary>类别名(顺序必须与训练时一致)</summary>
|
||
public EditableList<TextItem> Classes { get; } = new() { Title = "类别名(顺序须与训练一致)" };
|
||
|
||
/// <summary>输入尺寸</summary>
|
||
public int InputSize
|
||
{
|
||
get { return _inputSize; }
|
||
set { SetProperty(ref _inputSize, value); }
|
||
}
|
||
|
||
/// <summary>读入</summary>
|
||
public void CopyFrom(ModelDef from)
|
||
{
|
||
Path = from.Path;
|
||
InputSize = from.InputSize;
|
||
|
||
Classes.Items.Clear();
|
||
foreach (var name in from.Classes) Classes.Items.Add(new TextItem { Text = name, RemoveCommand = Classes.RemoveCommand });
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// regions 里的一项(Key = 配方里引用的区域名,即 YAML 的字典键)
|
||
/// </summary>
|
||
public class RegionNode : EditorNode
|
||
{
|
||
private string _key = "";
|
||
private int _x1;
|
||
private int _y1;
|
||
private int _x2;
|
||
private int _y2;
|
||
private string _name = "";
|
||
private string _description = "";
|
||
private string _color = "#4CAF50";
|
||
|
||
/// <summary>区域键名(YAML 的键,条件里引用的就是它)</summary>
|
||
public string Key
|
||
{
|
||
get { return _key; }
|
||
set
|
||
{
|
||
var old = _key;
|
||
if (!SetProperty(ref _key, value)) return;
|
||
if (!string.IsNullOrWhiteSpace(old)) Renamed?.Invoke(old, value);
|
||
}
|
||
}
|
||
|
||
/// <summary>左上 X</summary>
|
||
public int X1
|
||
{
|
||
get { return _x1; }
|
||
set { SetProperty(ref _x1, value); }
|
||
}
|
||
|
||
/// <summary>左上 Y</summary>
|
||
public int Y1
|
||
{
|
||
get { return _y1; }
|
||
set { SetProperty(ref _y1, value); }
|
||
}
|
||
|
||
/// <summary>右下 X</summary>
|
||
public int X2
|
||
{
|
||
get { return _x2; }
|
||
set { SetProperty(ref _x2, value); }
|
||
}
|
||
|
||
/// <summary>右下 Y</summary>
|
||
public int Y2
|
||
{
|
||
get { return _y2; }
|
||
set { SetProperty(ref _y2, value); }
|
||
}
|
||
|
||
/// <summary>显示名</summary>
|
||
public string Name
|
||
{
|
||
get { return _name; }
|
||
set { SetProperty(ref _name, value); }
|
||
}
|
||
|
||
/// <summary>说明</summary>
|
||
public string Description
|
||
{
|
||
get { return _description; }
|
||
set { SetProperty(ref _description, value); }
|
||
}
|
||
|
||
/// <summary>显示颜色(#RRGGBB)</summary>
|
||
public string Color
|
||
{
|
||
get { return _color; }
|
||
set
|
||
{
|
||
if (!SetProperty(ref _color, value)) return;
|
||
RaisePropertyChanged(nameof(Brush));
|
||
}
|
||
}
|
||
|
||
/// <summary>色块预览(解析失败退回默认绿,别炸界面)</summary>
|
||
public SolidColorBrush Brush
|
||
{
|
||
get
|
||
{
|
||
try
|
||
{
|
||
return new SolidColorBrush((System.Windows.Media.Color)ColorConverter.ConvertFromString(Color));
|
||
}
|
||
catch
|
||
{
|
||
return new SolidColorBrush(System.Windows.Media.Color.FromRgb(0x4C, 0xAF, 0x50));
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>区域键名变了(配方里的引用要跟着改)</summary>
|
||
public event Action<string, string>? Renamed;
|
||
|
||
/// <summary>读入</summary>
|
||
public void CopyFrom(RegionDef from)
|
||
{
|
||
X1 = from.X1;
|
||
Y1 = from.Y1;
|
||
X2 = from.X2;
|
||
Y2 = from.Y2;
|
||
Name = from.Name;
|
||
Description = from.Description;
|
||
Color = from.Color;
|
||
}
|
||
|
||
/// <summary>写出</summary>
|
||
public void CopyTo(RegionDef to)
|
||
{
|
||
to.X1 = X1;
|
||
to.Y1 = Y1;
|
||
to.X2 = X2;
|
||
to.Y2 = Y2;
|
||
to.Name = Name;
|
||
to.Description = Description;
|
||
to.Color = Color;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// steps 里的一项
|
||
/// </summary>
|
||
public class StepNode : EditorNode
|
||
{
|
||
private int _id;
|
||
private string _name = "";
|
||
private string _description = "";
|
||
private int _timeout;
|
||
private int _standardDurationMs;
|
||
private ConditionNode? _detection;
|
||
|
||
/// <summary>步骤号(执行顺序按它升序)</summary>
|
||
public int Id
|
||
{
|
||
get { return _id; }
|
||
set { SetProperty(ref _id, value); }
|
||
}
|
||
|
||
/// <summary>步骤名</summary>
|
||
public string Name
|
||
{
|
||
get { return _name; }
|
||
set { SetProperty(ref _name, value); }
|
||
}
|
||
|
||
/// <summary>说明</summary>
|
||
public string Description
|
||
{
|
||
get { return _description; }
|
||
set { SetProperty(ref _description, value); }
|
||
}
|
||
|
||
/// <summary>本步超时秒数(0=不限制)</summary>
|
||
public int Timeout
|
||
{
|
||
get { return _timeout; }
|
||
set { SetProperty(ref _timeout, value); }
|
||
}
|
||
|
||
/// <summary>标准节拍毫秒(报表用,不参与判定)</summary>
|
||
public int StandardDurationMs
|
||
{
|
||
get { return _standardDurationMs; }
|
||
set { SetProperty(ref _standardDurationMs, value); }
|
||
}
|
||
|
||
/// <summary>判定条件(method 留空 = 本步不写 detection 节点)</summary>
|
||
public ConditionNode? Detection
|
||
{
|
||
get { return _detection; }
|
||
set { SetProperty(ref _detection, value); }
|
||
}
|
||
|
||
/// <summary>本步期间必须常在的物体</summary>
|
||
public EditableList<TextItem> MustKeep { get; } = new() { Title = "本步期间必须在的物体(被拿走即违规)" };
|
||
|
||
/// <summary>本步完成时必须存在的物体</summary>
|
||
public EditableList<TextItem> RequiredObjects { get; } = new() { Title = "本步完成时必须存在的物体(漏放/缺料)" };
|
||
|
||
/// <summary>违规规则</summary>
|
||
public EditableList<ViolationRuleNode> ViolationRules { get; } = new() { Title = "违规规则" };
|
||
|
||
/// <summary>本步是否有判定条件(取消勾选就清掉条件)</summary>
|
||
public bool HasCondition
|
||
{
|
||
get { return Detection is not null; }
|
||
set
|
||
{
|
||
if (value) Detection ??= new ConditionNode();
|
||
else Detection = null;
|
||
|
||
RaisePropertyChanged();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 步骤的判定条件
|
||
/// </summary>
|
||
public class ConditionNode : EditorNode
|
||
{
|
||
private string _method = "";
|
||
private string _targetObject = "";
|
||
private string _region = "";
|
||
private string _fromRegion = "";
|
||
private string _toRegion = "";
|
||
private string _hand = "any";
|
||
private float _tolerance = 60f;
|
||
private float _minConfidence = 0.7f;
|
||
private int _stableFrames = 5;
|
||
private int _durationMs;
|
||
private string _action = "";
|
||
|
||
/// <summary>判定方法</summary>
|
||
public string Method
|
||
{
|
||
get { return _method; }
|
||
set { SetProperty(ref _method, value); }
|
||
}
|
||
|
||
/// <summary>目标物体类别</summary>
|
||
public string TargetObject
|
||
{
|
||
get { return _targetObject; }
|
||
set { SetProperty(ref _targetObject, value); }
|
||
}
|
||
|
||
/// <summary>主区域名</summary>
|
||
public string Region
|
||
{
|
||
get { return _region; }
|
||
set { SetProperty(ref _region, value); }
|
||
}
|
||
|
||
/// <summary>起点区域名(hand_move 的 from)</summary>
|
||
public string FromRegion
|
||
{
|
||
get { return _fromRegion; }
|
||
set { SetProperty(ref _fromRegion, value); }
|
||
}
|
||
|
||
/// <summary>终点区域名(hand_move 的 to)</summary>
|
||
public string ToRegion
|
||
{
|
||
get { return _toRegion; }
|
||
set { SetProperty(ref _toRegion, value); }
|
||
}
|
||
|
||
/// <summary>手:any/left/right</summary>
|
||
public string Hand
|
||
{
|
||
get { return _hand; }
|
||
set { SetProperty(ref _hand, value); }
|
||
}
|
||
|
||
/// <summary>手-物相邻容差(像素)</summary>
|
||
public float Tolerance
|
||
{
|
||
get { return _tolerance; }
|
||
set { SetProperty(ref _tolerance, value); }
|
||
}
|
||
|
||
/// <summary>本条件置信度阈值</summary>
|
||
public float MinConfidence
|
||
{
|
||
get { return _minConfidence; }
|
||
set { SetProperty(ref _minConfidence, value); }
|
||
}
|
||
|
||
/// <summary>稳定帧数</summary>
|
||
public int StableFrames
|
||
{
|
||
get { return _stableFrames; }
|
||
set { SetProperty(ref _stableFrames, value); }
|
||
}
|
||
|
||
/// <summary>计时毫秒(time_elapsed 用)</summary>
|
||
public int DurationMs
|
||
{
|
||
get { return _durationMs; }
|
||
set { SetProperty(ref _durationMs, value); }
|
||
}
|
||
|
||
/// <summary>动作语义(hand_action 用)</summary>
|
||
public string Action
|
||
{
|
||
get { return _action; }
|
||
set { SetProperty(ref _action, value); }
|
||
}
|
||
|
||
/// <summary>读入</summary>
|
||
public static ConditionNode From(StepCondition from) => new()
|
||
{
|
||
Method = from.Method,
|
||
TargetObject = from.TargetObject,
|
||
Region = from.Region,
|
||
FromRegion = from.FromRegion,
|
||
ToRegion = from.ToRegion,
|
||
Hand = from.Hand,
|
||
Tolerance = from.Tolerance,
|
||
MinConfidence = from.MinConfidence,
|
||
StableFrames = from.StableFrames,
|
||
DurationMs = from.DurationMs,
|
||
Action = from.Action,
|
||
};
|
||
|
||
/// <summary>写出</summary>
|
||
public StepCondition ToCondition() => new()
|
||
{
|
||
Method = Method,
|
||
TargetObject = TargetObject,
|
||
Region = Region,
|
||
FromRegion = FromRegion,
|
||
ToRegion = ToRegion,
|
||
Hand = Hand,
|
||
Tolerance = Tolerance,
|
||
MinConfidence = MinConfidence,
|
||
StableFrames = StableFrames,
|
||
DurationMs = DurationMs,
|
||
Action = Action,
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 违规规则
|
||
/// </summary>
|
||
public class ViolationRuleNode : EditorNode, IRemovable
|
||
{
|
||
private string _type = ViolationType.Timeout;
|
||
private string _description = "";
|
||
private int _severity = 1;
|
||
|
||
/// <summary>违规类型</summary>
|
||
public string Type
|
||
{
|
||
get { return _type; }
|
||
set { SetProperty(ref _type, value); }
|
||
}
|
||
|
||
/// <summary>描述(告警横幅文案)</summary>
|
||
public string Description
|
||
{
|
||
get { return _description; }
|
||
set { SetProperty(ref _description, value); }
|
||
}
|
||
|
||
/// <summary>严重度</summary>
|
||
public int Severity
|
||
{
|
||
get { return _severity; }
|
||
set { SetProperty(ref _severity, value); }
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public ICommand? RemoveCommand { get; set; }
|
||
|
||
/// <summary>读入</summary>
|
||
public static ViolationRuleNode From(ViolationRule from, ICommand? removeCommand = null) => new()
|
||
{
|
||
Type = from.Type,
|
||
Description = from.Description,
|
||
Severity = from.Severity,
|
||
RemoveCommand = removeCommand,
|
||
};
|
||
|
||
/// <summary>写出</summary>
|
||
public ViolationRule ToRule() => new()
|
||
{
|
||
Type = Type,
|
||
Description = Description,
|
||
Severity = Severity,
|
||
};
|
||
}
|