本仓库的 .cs 都是 UTF-8 with BOM + CRLF。前面几个提交里,编辑工具把 App.xaml.cs、LoginModel.cs、VisionFunction.cs 首行的 BOM 掉了(表现为每个提交都多出一行无关的 -using/+using diff);新建的 TaskTool.cs、WorkerGuard.cs 及三个测试文件则是无 BOM 的 LF。 这里统一补回 BOM、行尾归一为 CRLF。改动只是编码,逐文件 diff 都只有首行一处,无代码变化。
630 lines
24 KiB
C#
630 lines
24 KiB
C#
using OpenCvSharp;
|
||
using PLCSharp.Core.Common;
|
||
using PLCSharp.Core.Prism;
|
||
using PLCSharp.Core.Tools;
|
||
using PLCSharp.Core.UserControls;
|
||
using PLCSharp.Models;
|
||
using PLCSharp.VVMs.Vision.VisionFlowHandler;
|
||
using PLCSharp.VVMs.Vision.VisionFlowHandler.Access;
|
||
using PLCSharp.VVMs.Vision.VisionFlowHandler.Algorithm;
|
||
using PLCSharp.VVMs.Vision.VisionFlowHandler.Processing;
|
||
using Prism.Mvvm;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.Diagnostics;
|
||
using System.Text.Json;
|
||
using System.Threading;
|
||
using System.Windows.Media;
|
||
|
||
namespace PLCSharp.VVMs.Vision
|
||
{
|
||
/// <summary>
|
||
/// 视觉功能的执行单元,包含流程步骤列表(VisionFlows)、参数集合(Params)和运行时图像缓存
|
||
/// </summary>
|
||
public class VisionFunction : BindableBase
|
||
{
|
||
/// <summary>
|
||
/// 唯一标识
|
||
/// </summary>
|
||
[Key]
|
||
public Guid ID { get; set; } = Guid.NewGuid();
|
||
|
||
/// <summary>
|
||
/// 配方标识
|
||
/// </summary>
|
||
public Guid RecipeID { get; set; }
|
||
|
||
private string _Name;
|
||
|
||
/// <summary>
|
||
/// 配置项
|
||
/// </summary>
|
||
public string Name
|
||
{
|
||
get { return _Name; }
|
||
set
|
||
{
|
||
if (_Name != value)
|
||
{
|
||
Prompt = "已修改,请保存";
|
||
}
|
||
SetProperty(ref _Name, value);
|
||
}
|
||
}
|
||
|
||
private string _Prompt;
|
||
|
||
/// <summary>
|
||
/// 提示
|
||
/// </summary>
|
||
[NotMapped]
|
||
public string Prompt
|
||
{
|
||
get { return _Prompt; }
|
||
set { SetProperty(ref _Prompt, value); }
|
||
}
|
||
|
||
private string _Comment;
|
||
|
||
/// <summary>
|
||
/// 备注
|
||
/// </summary>
|
||
public string Comment
|
||
{
|
||
get { return _Comment; }
|
||
set
|
||
{
|
||
if (_Comment != value)
|
||
{
|
||
Prompt = "已修改,请保存";
|
||
}
|
||
SetProperty(ref _Comment, value);
|
||
}
|
||
}
|
||
|
||
private string _ControlName;
|
||
|
||
/// <summary>
|
||
/// 关联的首页图像控件
|
||
/// </summary>
|
||
public string ControlName
|
||
{
|
||
get { return _ControlName; }
|
||
set { SetProperty(ref _ControlName, value); }
|
||
}
|
||
|
||
#region 结果
|
||
|
||
[NotMapped]
|
||
public Pos ResultPos { get; set; } = new();
|
||
|
||
[NotMapped]
|
||
public Rect ResultRect { get; set; } = new();
|
||
|
||
[NotMapped]
|
||
public Circle ResultCircle { get; set; } = new();
|
||
|
||
[NotMapped]
|
||
public Line ResultLine { get; set; } = new();
|
||
|
||
[NotMapped]
|
||
public Barcode ResultBarcode { get; set; } = new();
|
||
|
||
[NotMapped]
|
||
public double ResultDouble { get; set; }
|
||
|
||
[NotMapped]
|
||
public string ResultString { get; set; }
|
||
|
||
[NotMapped]
|
||
public List<Pos> ResultPosList { get; set; } = [];
|
||
|
||
[NotMapped]
|
||
public List<Rect> ResultRectList { get; set; } = [];
|
||
|
||
[NotMapped]
|
||
public List<Circle> ResultCircleList { get; set; } = [];
|
||
|
||
[NotMapped]
|
||
public List<Line> ResultLineList { get; set; } = [];
|
||
|
||
[NotMapped]
|
||
public List<Barcode> ResultBarcodeList { get; set; } = [];
|
||
|
||
[NotMapped]
|
||
public List<double> ResultDoubleList { get; set; } = [];
|
||
|
||
[NotMapped]
|
||
public List<string> ResultStringList { get; set; } = [];
|
||
|
||
#endregion 结果
|
||
|
||
/// <summary>
|
||
/// 编辑时显示的控件
|
||
/// </summary>
|
||
[NotMapped]
|
||
public ImageEdit EditImageEdit { get; set; }
|
||
|
||
private Mat _Src;
|
||
|
||
/// <summary>
|
||
/// 源图像
|
||
/// </summary>
|
||
[NotMapped]
|
||
public Mat Src
|
||
{
|
||
get { return _Src; }
|
||
set
|
||
{
|
||
var old = _Src;
|
||
if (old != value)
|
||
{
|
||
_Src = value;
|
||
old?.Dispose(); // 释放旧 Mat 的非托管资源
|
||
}
|
||
}
|
||
}
|
||
|
||
private ObservableCollection<LocalImageData> _LocalImageDatas = [];
|
||
|
||
/// <summary>
|
||
/// 图像集合
|
||
/// </summary>
|
||
[NotMapped]
|
||
public ObservableCollection<LocalImageData> LocalImageDatas
|
||
{
|
||
get { return _LocalImageDatas; }
|
||
set { SetProperty(ref _LocalImageDatas, value); }
|
||
}
|
||
|
||
private VisionParams _Params = new();
|
||
|
||
// VisionFunction 中添加辅助方法
|
||
|
||
/// <summary>
|
||
/// 参数集合
|
||
/// </summary>
|
||
[NotMapped]
|
||
public VisionParams Params
|
||
{
|
||
get { return _Params; }
|
||
set { SetProperty(ref _Params, value); }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 序列化后的参数 JSON
|
||
/// </summary>
|
||
[Column("VisionParams")]
|
||
public string SerializedVisionParams
|
||
{
|
||
get => JsonSerializer.Serialize(Params, JsonOptions.Default); // 自动序列化
|
||
set
|
||
{
|
||
try
|
||
{
|
||
Params = value != null ? JsonSerializer.Deserialize<VisionParams>(value, JsonOptions.Default) ?? new VisionParams() : new VisionParams(); // 自动反序列化
|
||
CleanInvalidVariableValues(); // 清洗历史残留的 NaN / ±Infinity,避免脏数据污染后续流程
|
||
}
|
||
catch (global::System.Exception)
|
||
{
|
||
Params = new VisionParams();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 清洗局部变量表中非有限的 double 值(NaN / ±Infinity),重置为 0。
|
||
/// </summary>
|
||
private void CleanInvalidVariableValues()
|
||
{
|
||
foreach (var v in Params.Variables)
|
||
{
|
||
if (v.VarType == "Double" && v.RawValue is double d && !double.IsFinite(d))
|
||
{
|
||
v.RawValue = 0.0;
|
||
}
|
||
}
|
||
}
|
||
|
||
private ObservableCollection<VisionFlow> _VisionFlows = [];
|
||
|
||
/// <summary>
|
||
/// 流程步骤列表
|
||
/// </summary>
|
||
[NotMapped]
|
||
public ObservableCollection<VisionFlow> VisionFlows
|
||
{
|
||
get { return _VisionFlows; }
|
||
set
|
||
{
|
||
if (value == null) return;
|
||
|
||
SetProperty(ref _VisionFlows, value);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 序列化后的流程 JSON
|
||
/// </summary>
|
||
[Column("VisionFlows")]
|
||
public string SerializedVisionFlows
|
||
{
|
||
get => JsonSerializer.Serialize(VisionFlows, JsonOptions.Default); // 自动序列化
|
||
set => VisionFlows = JsonSerializer.Deserialize<ObservableCollection<VisionFlow>>(value, JsonOptions.Default) ?? []; // 自动反序列化
|
||
}
|
||
|
||
private int index = 0;
|
||
|
||
/// <summary>
|
||
/// 流程状态模型
|
||
/// </summary>
|
||
[NotMapped]
|
||
public FlowModel Flow { get; set; } = new();
|
||
|
||
[NotMapped]
|
||
public Stopwatch Sw { get; set; } = new();
|
||
|
||
/// <summary>
|
||
/// 运行All
|
||
/// </summary>
|
||
/// <param name="flow">流程状态模型</param>
|
||
/// <returns>返回布尔值</returns>
|
||
public async Task<bool> RunAllAsync()
|
||
{
|
||
Flow.Completed = false;
|
||
while (!Flow.Completed)
|
||
{
|
||
switch (Flow.Step)
|
||
{
|
||
case 0:
|
||
index = 0;
|
||
Flow.Step++;
|
||
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
DrawCommands.Clear();
|
||
EditImageEdit?.Remove("DrawOverlay");
|
||
GlobalModel.GetImageControl(ControlName)?.Remove("DrawOverlay");
|
||
});
|
||
break;
|
||
|
||
case 1:
|
||
if (VisionFlows.Count > index)
|
||
{
|
||
VisionFlows[index].Flow.Reset();
|
||
Flow.Step++;
|
||
}
|
||
else
|
||
{
|
||
var customControl = GlobalModel.GetCustomControl(ControlName);
|
||
if (customControl != null && Src != null)
|
||
{
|
||
customControl.ShowMat(Src);
|
||
}
|
||
Flow.Completed = true;
|
||
Flow.Success = true;
|
||
}
|
||
break;
|
||
|
||
case 2:
|
||
{
|
||
Sw.Restart();
|
||
Flow.Step++;
|
||
}
|
||
break;
|
||
|
||
case 3:
|
||
{
|
||
// 单步超时:取 FlowModel.TimeoutSeconds(<=0 表示不限时)
|
||
var item = VisionFlows[index];
|
||
var timeoutSeconds = Flow.TimeoutSeconds;
|
||
try
|
||
{
|
||
item.Flow.StepTime = DateTime.Now;
|
||
var result = await TaskTool.RunWithTimeoutAsync(() => RunItem(item), timeoutSeconds);
|
||
item.CT = Math.Round(Sw.ElapsedMilliseconds / 1000.0, 3); // 转换为秒
|
||
Sw.Stop();
|
||
if (result)
|
||
{
|
||
Flow.Step = 1; // 回到 case 1 取下一个
|
||
index++;
|
||
}
|
||
else
|
||
{
|
||
item.Flow.FailMessage ??= "未知错误";
|
||
Flow.Success = false;
|
||
Flow.Completed = true;
|
||
}
|
||
}
|
||
catch (TimeoutException)
|
||
{
|
||
// 超时后该步骤被放弃:它可能仍在自己的线程上跑完,其结果一律视为不可信
|
||
Sw.Stop();
|
||
item.CT = Math.Round(Sw.ElapsedMilliseconds / 1000.0, 3); // 转换为秒
|
||
item.Flow.FailMessage = $"执行超时(>{timeoutSeconds:F0}秒),该步骤已被放弃";
|
||
Flow.Success = false;
|
||
Flow.Completed = true;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
return true;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 全局图像列表
|
||
/// </summary>
|
||
[NotMapped]
|
||
public ObservableCollection<ImageData> ImageDatas { get; set; }
|
||
|
||
/// <summary>
|
||
/// 视觉模型
|
||
/// </summary>
|
||
[NotMapped]
|
||
public VisionsModel VisionsModel { get; set; }
|
||
|
||
/// <summary>
|
||
/// 运行项
|
||
/// </summary>
|
||
/// <param name="item">变量项</param>
|
||
/// <returns>返回布尔值</returns>
|
||
public bool RunItem(VisionFlow item)
|
||
{
|
||
if (_handlers.TryGetValue(item.Type, out var handler))
|
||
return handler.Execute(this, item);
|
||
return false;
|
||
}
|
||
|
||
private static readonly Dictionary<VisionFlowType, IVisionFlowHandler> _handlers = new()
|
||
{
|
||
[VisionFlowType.阈值] = new ThresholdHandler(),
|
||
[VisionFlowType.GRAY2BGR] = new Gray2BgrHandler(),
|
||
[VisionFlowType.BGR2GRAY] = new Bgr2GrayHandler(),
|
||
[VisionFlowType.取通道] = new SplitChannelHandler(),
|
||
|
||
[VisionFlowType.各通道最小值] = new MinChannelHandler(),
|
||
[VisionFlowType.各通道最大值] = new MaxChannelHandler(),
|
||
[VisionFlowType.显示图像到主页] = new ImageToHomePageHandler(),
|
||
|
||
[VisionFlowType.从文件获取图片] = new ImageFromFileHandler(),
|
||
|
||
[VisionFlowType.存到文件] = new ImageToFileHandler(),
|
||
[VisionFlowType.从全局图像获取图片] = new ImageFromGlobalHandler(),
|
||
[VisionFlowType.存到全局图像] = new ImageToGlobalHandler(),
|
||
[VisionFlowType.从局部图像获取图片] = new ImageFromLocalHandler(),
|
||
[VisionFlowType.存到局部图像] = new ImageToLocalHandler(),
|
||
[VisionFlowType.拍照] = new ImageFromCameraHandler(),
|
||
[VisionFlowType.卡尺寻边] = new CaliperFindEdgeHandler(),
|
||
[VisionFlowType.ORB匹配] = new ORBMatchHandler(),
|
||
[VisionFlowType.灰度模板匹配] = new GrayTemplateMatchHandler(),
|
||
[VisionFlowType.SIFT匹配] = new SiftMatchHandler(),
|
||
[VisionFlowType.矩阵生成] = new MatrixGenHandler(),
|
||
[VisionFlowType.距离] = new DistanceHandler(),
|
||
[VisionFlowType.卡尺找圆] = new CaliperFindCircleHandler(),
|
||
[VisionFlowType.卡尺找旋转矩形] = new CaliperFindRectHandler(),
|
||
[VisionFlowType.清除绘制] = new ClearDrawHandler(),
|
||
[VisionFlowType.两线交点] = new TwoLineIntersectHandler(),
|
||
[VisionFlowType.坐标转换] = new CoordinateTransformHandler(),
|
||
[VisionFlowType.ROI解码] = new BarcodeDecodeHandler(),
|
||
[VisionFlowType.微信解码] = new WeChatDecodeHandler(),
|
||
[VisionFlowType.画廊分类] = new GalleryClassifierHandler(),
|
||
[VisionFlowType.颜色面积] = new ColorAreaHandler(),
|
||
[VisionFlowType.灰度面积] = new GrayAreaHandler(),
|
||
[VisionFlowType.图像翻转] = new ImageFlipHandler(),
|
||
[VisionFlowType.图像旋转] = new ImageRotateHandler(),
|
||
[VisionFlowType.ROI剪切] = new ROICropHandler(),
|
||
[VisionFlowType.直方图均衡化] = new HistogramEqualizeHandler(),
|
||
[VisionFlowType.图像相减] = new ImageSubtractHandler(),
|
||
[VisionFlowType.图像校正] = new ImageWarpHandler(),
|
||
};
|
||
|
||
[NotMapped]
|
||
public GlobalModel GlobalModel { get; set; }
|
||
|
||
[NotMapped]
|
||
public List<DrawCommand> DrawCommands { get; set; } = [];
|
||
|
||
public async Task RenderDrawAsync()
|
||
{
|
||
await System.Windows.Application.Current.Dispatcher.InvokeAsync(() =>
|
||
{
|
||
if (DrawCommands == null || DrawCommands.Count <= 0) return;
|
||
var commands = DrawCommands.ToList();
|
||
|
||
if (EditImageEdit != null)
|
||
//编辑窗口显示
|
||
{
|
||
foreach (var cmd in commands)
|
||
{
|
||
if (cmd.IsDrawnEdit) continue; // 如果已经绘制过,则跳过
|
||
|
||
switch (cmd.Shape)
|
||
{
|
||
case DrawCommand.Type.Line:
|
||
var line = new System.Windows.Shapes.Line
|
||
{
|
||
X1 = cmd.X1,
|
||
Y1 = cmd.Y1,
|
||
X2 = cmd.X2,
|
||
Y2 = cmd.Y2,
|
||
Stroke = new SolidColorBrush(cmd.Color),
|
||
StrokeThickness = cmd.Thickness,
|
||
Tag = "DrawOverlay"
|
||
};
|
||
EditImageEdit.Draw(line);
|
||
break;
|
||
|
||
case DrawCommand.Type.Circle:
|
||
if (cmd.Filled)
|
||
{
|
||
var dot = new System.Windows.Shapes.Ellipse
|
||
{
|
||
Width = cmd.Radius * 2,
|
||
Height = cmd.Radius * 2,
|
||
Fill = new SolidColorBrush(cmd.Color),
|
||
Tag = "DrawOverlay"
|
||
};
|
||
EditImageEdit.Draw(dot, (int)(cmd.Y1 - cmd.Radius), (int)(cmd.X1 - cmd.Radius));
|
||
}
|
||
else
|
||
{
|
||
var circle = new System.Windows.Shapes.Ellipse
|
||
{
|
||
Width = cmd.Radius * 2,
|
||
Height = cmd.Radius * 2,
|
||
Stroke = new SolidColorBrush(cmd.Color),
|
||
StrokeThickness = cmd.Thickness,
|
||
Tag = "DrawOverlay"
|
||
};
|
||
EditImageEdit.Draw(circle, (int)(cmd.Y1 - cmd.Radius), (int)(cmd.X1 - cmd.Radius));
|
||
}
|
||
break;
|
||
|
||
case DrawCommand.Type.Text:
|
||
var editText = new System.Windows.Controls.TextBlock
|
||
{
|
||
Text = cmd.Text,
|
||
FontSize = cmd.FontSize,
|
||
Foreground = new SolidColorBrush(cmd.Color),
|
||
Tag = "DrawOverlay"
|
||
};
|
||
EditImageEdit.Draw(editText, (int)cmd.Y1, (int)cmd.X1);
|
||
break;
|
||
|
||
case DrawCommand.Type.Polygon:
|
||
if (cmd.Points != null && cmd.Points.Length >= 2)
|
||
{
|
||
var poly = new System.Windows.Shapes.Polygon
|
||
{
|
||
Points = new System.Windows.Media.PointCollection(
|
||
cmd.Points.Select(p => new System.Windows.Point(p.X, p.Y))),
|
||
Stroke = new SolidColorBrush(cmd.Color),
|
||
StrokeThickness = cmd.Thickness,
|
||
Fill = cmd.FillColor.HasValue
|
||
? new SolidColorBrush(Color.FromArgb(80, cmd.FillColor.Value.R, cmd.FillColor.Value.G, cmd.FillColor.Value.B))
|
||
: Brushes.Transparent,
|
||
Tag = "DrawOverlay"
|
||
};
|
||
EditImageEdit.Draw(poly);
|
||
}
|
||
break;
|
||
}
|
||
cmd.IsDrawnEdit = true;
|
||
}
|
||
}
|
||
var imageEdit = GlobalModel.GetImageControl(ControlName);
|
||
if (imageEdit == null) return;
|
||
|
||
{
|
||
foreach (var cmd in commands)
|
||
{
|
||
if (cmd.IsDrawn) continue; // 如果已经绘制过,则跳过
|
||
|
||
switch (cmd.Shape)
|
||
{
|
||
case DrawCommand.Type.Line:
|
||
var line = new System.Windows.Shapes.Line
|
||
{
|
||
X1 = cmd.X1,
|
||
Y1 = cmd.Y1,
|
||
X2 = cmd.X2,
|
||
Y2 = cmd.Y2,
|
||
Stroke = new SolidColorBrush(cmd.Color),
|
||
StrokeThickness = cmd.Thickness,
|
||
Tag = "DrawOverlay"
|
||
};
|
||
imageEdit.Draw(line);
|
||
break;
|
||
|
||
case DrawCommand.Type.Circle:
|
||
if (cmd.Filled)
|
||
{
|
||
var dot = new System.Windows.Shapes.Ellipse
|
||
{
|
||
Width = cmd.Radius * 2,
|
||
Height = cmd.Radius * 2,
|
||
Fill = new SolidColorBrush(cmd.Color),
|
||
Tag = "DrawOverlay"
|
||
};
|
||
imageEdit.Draw(dot, (int)(cmd.Y1 - cmd.Radius), (int)(cmd.X1 - cmd.Radius));
|
||
}
|
||
else
|
||
{
|
||
var circle = new System.Windows.Shapes.Ellipse
|
||
{
|
||
Width = cmd.Radius * 2,
|
||
Height = cmd.Radius * 2,
|
||
Stroke = new SolidColorBrush(cmd.Color),
|
||
StrokeThickness = cmd.Thickness,
|
||
Tag = "DrawOverlay"
|
||
};
|
||
imageEdit.Draw(circle, (int)(cmd.Y1 - cmd.Radius), (int)(cmd.X1 - cmd.Radius));
|
||
}
|
||
break;
|
||
|
||
case DrawCommand.Type.Polygon:
|
||
if (cmd.Points != null && cmd.Points.Length >= 2)
|
||
{
|
||
var poly = new System.Windows.Shapes.Polygon
|
||
{
|
||
Points = new System.Windows.Media.PointCollection(
|
||
cmd.Points.Select(p => new System.Windows.Point(p.X, p.Y))),
|
||
Stroke = new SolidColorBrush(cmd.Color),
|
||
StrokeThickness = cmd.Thickness,
|
||
Fill = cmd.FillColor.HasValue
|
||
? new SolidColorBrush(Color.FromArgb(80, cmd.FillColor.Value.R, cmd.FillColor.Value.G, cmd.FillColor.Value.B))
|
||
: Brushes.Transparent,
|
||
Tag = "DrawOverlay"
|
||
};
|
||
imageEdit.Draw(poly);
|
||
}
|
||
break;
|
||
|
||
case DrawCommand.Type.Text:
|
||
var mainText = new System.Windows.Controls.TextBlock
|
||
{
|
||
Text = cmd.Text,
|
||
FontSize = cmd.FontSize,
|
||
Foreground = new SolidColorBrush(cmd.Color),
|
||
Tag = "DrawOverlay"
|
||
};
|
||
imageEdit.Draw(mainText, (int)cmd.Y1, (int)cmd.X1);
|
||
break;
|
||
}
|
||
cmd.IsDrawn = true;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// VisionParams
|
||
/// </summary>
|
||
public class VisionParams : BindableBase
|
||
{
|
||
private ObservableDictionary<string, double> _ResultDoubles = [];
|
||
|
||
/// <summary>
|
||
/// 通用数值结果(如 ORB 匹配输出的 X/Y/角度偏移)
|
||
/// </summary>
|
||
public ObservableDictionary<string, double> ResultDoubles
|
||
{
|
||
get { return _ResultDoubles; }
|
||
set { SetProperty(ref _ResultDoubles, value); }
|
||
}
|
||
|
||
private ObservableCollection<LocalVariableItem> _Variables = [];
|
||
|
||
/// <summary>
|
||
/// 局部变量表(坐标点、直线、圆、矩形、数值),序列化保存在 JSON 中
|
||
/// </summary>
|
||
public ObservableCollection<LocalVariableItem> Variables
|
||
{
|
||
get { return _Variables; }
|
||
set { SetProperty(ref _Variables, value); }
|
||
}
|
||
}
|
||
} |