check point
This commit is contained in:
125
Hawkeye.VisionBuilder.Workflow/Operations/AI/AnomalyAI.cs
Normal file
125
Hawkeye.VisionBuilder.Workflow/Operations/AI/AnomalyAI.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
public class AnomalyAI: BaseOperation
|
||||
{
|
||||
|
||||
private readonly string _modelName;
|
||||
|
||||
public AnomalyAI()
|
||||
{
|
||||
|
||||
_cSharpDataTransferMQRPC = PythonModelProxyRPC.GetInterface();
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
private readonly CSharpDataTransferMQRPC _cSharpDataTransferMQRPC;
|
||||
private int _imageWidth;
|
||||
private int _imageHeight;
|
||||
|
||||
private bool _initialized = false;
|
||||
private FilePath _modelFilePath = new FilePath();
|
||||
|
||||
|
||||
public string MemorySlotName { get; set; } = "Anomaly";
|
||||
|
||||
public FilePath ModelFilePath
|
||||
{
|
||||
get => _modelFilePath;
|
||||
set
|
||||
{
|
||||
_modelFilePath = value;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ModelFilePath?.Path))
|
||||
{
|
||||
this.SetError("Model file not found");
|
||||
return;
|
||||
}
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("load_model", ModelFilePath.Path, _modelName));
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("activate_model", _modelName));
|
||||
|
||||
var sizeEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_accept_size"));
|
||||
var size = sizeEncoded.Select(Convert.ToInt32).ToArray();
|
||||
_imageWidth = size[2];
|
||||
_imageHeight = size[1];
|
||||
|
||||
var outputSize = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_output_size"));
|
||||
var outputSizeArray = outputSize.Select(Convert.ToInt32).ToArray();
|
||||
|
||||
|
||||
var currentImage = context.ActiveImage;
|
||||
|
||||
var rightColor = currentImage.ImageData.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
var resized = rightColor.Resize(new Size(_imageWidth,_imageHeight));
|
||||
int dataSize = resized.Rows * resized.Cols * resized.ElemSize();
|
||||
var byteArray = new byte[dataSize];
|
||||
Marshal.Copy(resized.Data, byteArray, 0, dataSize);
|
||||
|
||||
|
||||
var resultEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall( "predict_anomaly", byteArray));
|
||||
|
||||
var result = resultEncoded.Select(Convert.ToByte).ToArray();
|
||||
|
||||
var defect = result.Take(1).ToArray()[0];
|
||||
|
||||
if (defect > 50)
|
||||
{
|
||||
|
||||
|
||||
byte[] resultColor = result.Skip(1).ToArray();
|
||||
|
||||
|
||||
|
||||
// black and white mask
|
||||
var mask = new Mat(30, 48, MatType.CV_8UC1, resultColor);
|
||||
|
||||
var maskResized = mask.Resize(new Size(currentImage.ImageData.Width, currentImage.ImageData.Height));
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() {ImageData = maskResized};
|
||||
}
|
||||
else
|
||||
{
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = new Mat(currentImage.ImageData.Size(), MatType.CV_8UC1, new Scalar(0)) };
|
||||
}
|
||||
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(_modelFilePath.Path);
|
||||
bw.Write(_modelFilePath.Format);
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
_modelFilePath.Path = br.ReadString();
|
||||
_modelFilePath.Format = br.ReadString();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.FixedRectangle;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.ArrayHorizontal;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
[IgnoreOperation]
|
||||
public class ArrayModelMatchingOperation:BaseOperation
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
private readonly CSharpDataTransferMQ _transfer;
|
||||
public Guid ReferenceId { get; set; } = new Guid();
|
||||
public ArrayHorizontalElement SearchArea { get; set; }
|
||||
public string ModelName { get; set; } = "cls_candy5.h5";
|
||||
public Action ExportImages { get; set; }
|
||||
public int Margin { get; set; } = 5;
|
||||
public int Amount { get; set; } = 4;
|
||||
|
||||
private int _channels = 3;
|
||||
private List<Mat> _lastCutImages;
|
||||
|
||||
public ArrayModelMatchingOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
_transfer = PythonModelProxy.GetInterface();
|
||||
|
||||
|
||||
SearchArea = new ArrayHorizontalElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.One * 100,
|
||||
BlockSize = new Vector2(100, 100),
|
||||
|
||||
};
|
||||
ExportImages = () =>
|
||||
{
|
||||
if (!Directory.Exists(Path.Combine(@"..\Data\Export", ModelName)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(@"..\Data\Export", ModelName));
|
||||
}
|
||||
|
||||
// save last cut images
|
||||
var i = 0;
|
||||
foreach (var cutImage in _lastCutImages)
|
||||
{
|
||||
cutImage.SaveImage(Path.Combine(@"..\Data\Export", ModelName, $"{Guid.NewGuid().ToString()}.png"));
|
||||
}
|
||||
|
||||
};
|
||||
ReloadModelInfo();
|
||||
}
|
||||
|
||||
private void ReloadModelInfo()
|
||||
{
|
||||
var responseBytes = _transfer.TransferData(ModelName, 2, Array.Empty<byte>());
|
||||
int[] sizes = new int[3];
|
||||
Buffer.BlockCopy(responseBytes, 0, sizes, 0, 12);
|
||||
SearchArea.BlockSize = new Vector2(sizes[1], sizes[0]);
|
||||
_channels = sizes[2];
|
||||
}
|
||||
|
||||
byte[] MatToBytes(Mat mat)
|
||||
{
|
||||
if (mat.Channels() == 3)
|
||||
{
|
||||
mat = mat.CvtColor(_channels == 3 ? ColorConversionCodes.BGR2RGB : ColorConversionCodes.BGR2GRAY);
|
||||
}
|
||||
|
||||
|
||||
IntPtr dataPtr = mat.Data;
|
||||
|
||||
// Calculate the size of the image data
|
||||
int dataSize = mat.Rows * mat.Cols * mat.ElemSize();
|
||||
|
||||
|
||||
|
||||
|
||||
// Copy the image data into a byte array
|
||||
byte[] byteArray = new byte[dataSize];
|
||||
|
||||
Marshal.Copy(dataPtr, byteArray, 0, dataSize);
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
Result = true;
|
||||
if (!File.Exists(Path.Combine(@"..\Data\AI", ModelName)))
|
||||
{
|
||||
Status = $"Model `{ModelName}` not found";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
if (!CheckImageExists(context)) return;
|
||||
switch (_channels)
|
||||
{
|
||||
case 3 when !CheckColorful(context):
|
||||
case 1 when !CheckGrayscale(context):
|
||||
return;
|
||||
}
|
||||
|
||||
var refLocation = _workflowList.GetOriginById(ReferenceId).Origin.Location;
|
||||
SearchArea.MovePivot(refLocation);
|
||||
|
||||
SearchArea.Margin=Margin;
|
||||
SearchArea.BlockCount=Amount;
|
||||
var img = context.ActiveImage.ImageData;
|
||||
|
||||
_lastCutImages = SearchArea
|
||||
.GenerateLocations()
|
||||
.Select(x => img[(int) x.Y, (int) x.Y + (int) SearchArea.BlockSize.Y, (int) x.X,
|
||||
(int) x.X + (int) SearchArea.BlockSize.X]).ToList();
|
||||
var images= _lastCutImages.Select(MatToBytes).ToList();
|
||||
|
||||
|
||||
var bytes= images.SelectMany(x => x).ToArray();
|
||||
|
||||
var len = images.Count;
|
||||
var bytes2 = new byte[bytes.Length + 4];
|
||||
bytes2[3] = (byte)(len >> 24);
|
||||
bytes2[2] = (byte)(len >> 16);
|
||||
bytes2[1] = (byte)(len >> 8);
|
||||
bytes2[0] = (byte)(len >> 0);
|
||||
|
||||
Array.Copy(bytes, 0, bytes2, 4, bytes.Length);
|
||||
|
||||
var answer = _transfer.TransferData(ModelName, 0,bytes2);
|
||||
float[] floatArray = new float[answer.Length / 4];
|
||||
Buffer.BlockCopy(answer, 0, floatArray, 0, answer.Length);
|
||||
|
||||
// Print answer
|
||||
for (int i = 0; i < Amount; i++)
|
||||
{
|
||||
Console.WriteLine(floatArray[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < Amount; i++)
|
||||
{
|
||||
SearchArea.IsGood[i] = floatArray[i] > 0.7;
|
||||
}
|
||||
|
||||
|
||||
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
Result = SearchArea.IsGood.All(x=>x.Value==true);
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
ReloadModelInfo();
|
||||
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(ReferenceId.ToString());
|
||||
bw.Write(Margin);
|
||||
bw.Write(Amount);
|
||||
bw.Write(ModelName);
|
||||
SearchArea.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
ReferenceId = new Guid(br.ReadString());
|
||||
Margin = br.ReadInt32();
|
||||
Amount = br.ReadInt32();
|
||||
ModelName = br.ReadString();
|
||||
SearchArea = new ArrayHorizontalElement();
|
||||
SearchArea.Load(br);
|
||||
ReloadModelInfo();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.ArrayHorizontal;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
[IgnoreOperation]
|
||||
public class ArrayModelSamplerOperation:BaseOperation
|
||||
{
|
||||
private Mat _lastImage;
|
||||
|
||||
public RectangleElement SearchArea { get; set; }
|
||||
public string SampleStorage { get; set; } = "samples.storage";
|
||||
public Action Sample { get; set; }
|
||||
|
||||
public ArrayModelSamplerOperation()
|
||||
{
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.One * 100,
|
||||
Size = new Vector2(100, 100),
|
||||
|
||||
};
|
||||
Sample = SaveSample;
|
||||
}
|
||||
|
||||
byte[] MatToBytes(Mat mat)
|
||||
{
|
||||
if (mat.Channels() == 3)
|
||||
{
|
||||
mat = mat.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
}
|
||||
|
||||
|
||||
IntPtr dataPtr = mat.Data;
|
||||
int dataSize = mat.Rows * mat.Cols * mat.ElemSize();
|
||||
byte[] byteArray = new byte[dataSize];
|
||||
|
||||
Marshal.Copy(dataPtr, byteArray, 0, dataSize);
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
private void SaveSample()
|
||||
{
|
||||
|
||||
|
||||
|
||||
Mat mask = new Mat(_lastImage.Size(), MatType.CV_8UC1, Scalar.Black);
|
||||
SearchArea.FillMat(mask);
|
||||
var imageBytes = MatToBytes(_lastImage);
|
||||
var maskBytes = MatToBytes(mask);
|
||||
// Append to the end of storage file
|
||||
|
||||
var storagePath=Path.Combine("..\\Data\\AI", SampleStorage);
|
||||
|
||||
|
||||
|
||||
using (var file = new System.IO.FileStream(storagePath, System.IO.FileMode.Append))
|
||||
{
|
||||
// Write size
|
||||
file.Write(BitConverter.GetBytes(_lastImage.Width), 0, 4);
|
||||
file.Write(BitConverter.GetBytes(_lastImage.Height), 0, 4);
|
||||
file.Write(imageBytes, 0, imageBytes.Length);
|
||||
// Write mask
|
||||
file.Write(maskBytes, 0, maskBytes.Length);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if(!CheckColorful(context)) return;
|
||||
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
|
||||
_lastImage=context.ActiveImage.ImageData;
|
||||
Result = true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
|
||||
using Hawkeye.VisionBuilder.Workflow.Links;
|
||||
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
[IgnoreOperation]
|
||||
public class BackgroundSeparationModelOperation:BaseOperation,IHaveOrigin
|
||||
{
|
||||
private readonly CSharpDataTransferMQ _transfer;
|
||||
private int _channels;
|
||||
public string ModelName { get; set; } = "background.h5";
|
||||
|
||||
|
||||
public BackgroundSeparationModelOperation()
|
||||
{
|
||||
_transfer = PythonModelProxy.GetInterface();
|
||||
|
||||
}
|
||||
|
||||
byte[] MatToBytes(Mat mat)
|
||||
{
|
||||
if (mat.Channels() == 3)
|
||||
{
|
||||
mat = mat.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
}
|
||||
|
||||
|
||||
IntPtr dataPtr = mat.Data;
|
||||
int dataSize = mat.Rows * mat.Cols * mat.ElemSize();
|
||||
byte[] byteArray = new byte[dataSize];
|
||||
|
||||
Marshal.Copy(dataPtr, byteArray, 0, dataSize);
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
Result = true;
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
if (!File.Exists(Path.Combine(@"..\Data\AI", ModelName)))
|
||||
{
|
||||
Status= $"Model `{ModelName}` not found";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var img = context.ActiveImage.ImageData;
|
||||
|
||||
var bytes= MatToBytes(img);
|
||||
var responseBytes = _transfer.TransferData(ModelName, 3, bytes);
|
||||
var response = new Mat(img.Rows, img.Cols, MatType.CV_8UC1, responseBytes);
|
||||
|
||||
var contours = response.Threshold(128, 255, ThresholdTypes.Binary).FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
|
||||
|
||||
if (contours.Length == 0)
|
||||
{
|
||||
Status = "No object found";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// find biggest contour
|
||||
var max = 0;
|
||||
var maxIndex = 0;
|
||||
for (var i = 0; i < contours.Length; i++)
|
||||
{
|
||||
var area = Cv2.ContourArea(contours[i]);
|
||||
if (area > max)
|
||||
{
|
||||
max = (int)area;
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
PolyElement poly = new PolyElement() { IsGood = true,Editable = false};
|
||||
|
||||
poly.Points = contours[maxIndex].Select(x => new Vector2(x.X, x.Y)).ToArray();
|
||||
|
||||
context.GraphicsElements.Add(poly);
|
||||
|
||||
|
||||
var rect = Cv2.MinAreaRect(contours[maxIndex]);
|
||||
var bound = rect.BoundingRect();
|
||||
Origin=new OriginElement()
|
||||
{
|
||||
Location = new Vector2(bound.Left, bound.Top),
|
||||
};
|
||||
|
||||
Status= $"Object found at {Origin.Location}";
|
||||
Result = true;
|
||||
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(ModelName);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
ModelName = br.ReadString();
|
||||
}
|
||||
|
||||
public OriginElement Origin { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
[IgnoreOperation]
|
||||
public class Color128BinaryOperation:ColorAIOperation
|
||||
{
|
||||
public Color128BinaryOperation() : base("prob128binary")
|
||||
{
|
||||
IsRaw = true;
|
||||
}
|
||||
|
||||
|
||||
public override string FilterClasses { get; set; } = "1";
|
||||
public override FilePath ModelFilePath { get; set; } = new FilePath() { Format = "Model file(*.h5)|*.h5" };
|
||||
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(FilterClasses);
|
||||
bw.Write(ModelFilePath.Path);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
FilterClasses = br.ReadString();
|
||||
ModelFilePath.Path = br.ReadString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
[IgnoreOperation]
|
||||
public class Color128HalfOperation:BaseOperation
|
||||
{
|
||||
public Color128HalfOperation()
|
||||
{
|
||||
_cSharpDataTransferMQRPC = PythonModelProxyRPC.GetInterface();
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("activate_model", "segment128half"));
|
||||
var sizeEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_accept_size"));
|
||||
var size = sizeEncoded.Select(Convert.ToInt32).ToArray();
|
||||
_imageWidth = size[0];
|
||||
_imageHeight = size[1];
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public string FilterClasses { get; set; } = "0";
|
||||
public FilePath ModelFilePath { get; set; } = new FilePath() { Format = "Model file(*.h5)|*.h5" };
|
||||
|
||||
|
||||
private readonly CSharpDataTransferMQRPC _cSharpDataTransferMQRPC;
|
||||
private readonly int _imageWidth;
|
||||
private readonly int _imageHeight;
|
||||
|
||||
private bool _initialized = false;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if(!CheckImageExists(context))return;
|
||||
if(!CheckColorful(context))return;
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("activate_model", "segment128half"));
|
||||
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ModelFilePath.Path))
|
||||
{
|
||||
this.SetError("Model file not found");
|
||||
return;
|
||||
}
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("load_model", ModelFilePath.Path));
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
var currentImage = context.ActiveImage;
|
||||
|
||||
var rightColor = currentImage.ImageData.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
var resized = rightColor.Resize(new Size(_imageHeight, _imageWidth));
|
||||
int dataSize = resized.Rows * resized.Cols * resized.ElemSize();
|
||||
var byteArray = new byte[dataSize];
|
||||
Marshal.Copy(resized.Data, byteArray, 0, dataSize);
|
||||
|
||||
var allowed = FilterClasses.Split(',',StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToArray();
|
||||
|
||||
var allowedFlags = new byte[128];
|
||||
foreach (var i in allowed)
|
||||
{
|
||||
allowedFlags[i] = 1;
|
||||
}
|
||||
|
||||
var resultEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("predict", byteArray));
|
||||
|
||||
var result = resultEncoded.Select(Convert.ToByte).ToArray();
|
||||
|
||||
var resultColor = result.Select(x =>
|
||||
allowedFlags[x]==1
|
||||
? (byte)255
|
||||
: (byte)0)
|
||||
.ToArray();
|
||||
|
||||
// black and white mask
|
||||
var mask = new Mat(_imageHeight, _imageWidth, MatType.CV_8UC1, resultColor);
|
||||
|
||||
var maskResized = mask.Resize(new Size(currentImage.ImageData.Width, currentImage.ImageData.Height));
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() {ImageData = maskResized};
|
||||
|
||||
|
||||
Result = true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(FilterClasses);
|
||||
bw.Write(ModelFilePath.Path);
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
FilterClasses = br.ReadString();
|
||||
ModelFilePath.Path = br.ReadString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
[IgnoreOperation]
|
||||
public class Color128SimpleOperation: ColorAIOperation
|
||||
{
|
||||
public Color128SimpleOperation():base("bg128simple")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override string FilterClasses { get; set; } = "1";
|
||||
public override FilePath ModelFilePath { get; set; } = new FilePath(){Format = "Model file(*.h5)|*.h5"};
|
||||
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(FilterClasses);
|
||||
bw.Write(ModelFilePath.Path);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
FilterClasses = br.ReadString();
|
||||
ModelFilePath.Path = br.ReadString();
|
||||
}
|
||||
}
|
||||
114
Hawkeye.VisionBuilder.Workflow/Operations/AI/ColorAIOperation.cs
Normal file
114
Hawkeye.VisionBuilder.Workflow/Operations/AI/ColorAIOperation.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[IgnoreOperation]
|
||||
public abstract class ColorAIOperation:BaseOperation
|
||||
{
|
||||
private readonly string _modelName;
|
||||
|
||||
public ColorAIOperation(string model_name)
|
||||
{
|
||||
_modelName = model_name;
|
||||
_cSharpDataTransferMQRPC = PythonModelProxyRPC.GetInterface();
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
private readonly CSharpDataTransferMQRPC _cSharpDataTransferMQRPC;
|
||||
private int _imageWidth;
|
||||
private int _imageHeight;
|
||||
|
||||
private bool _initialized = false;
|
||||
private FilePath _modelFilePath;
|
||||
|
||||
|
||||
public virtual string FilterClasses { get; set; }
|
||||
|
||||
public virtual FilePath ModelFilePath
|
||||
{
|
||||
get => _modelFilePath;
|
||||
set
|
||||
{
|
||||
_modelFilePath = value;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsRaw { get; set; }
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ModelFilePath.Path))
|
||||
{
|
||||
this.SetError("Model file not found");
|
||||
return;
|
||||
}
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("load_model", ModelFilePath.Path, _modelName));
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("activate_model", _modelName));
|
||||
|
||||
var sizeEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_accept_size"));
|
||||
var size = sizeEncoded.Select(Convert.ToInt32).ToArray();
|
||||
_imageWidth = size[1];
|
||||
_imageHeight = size[2];
|
||||
|
||||
|
||||
var currentImage = context.ActiveImage;
|
||||
|
||||
var rightColor = currentImage.ImageData.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
var resized = rightColor.Resize(new Size(_imageHeight, _imageWidth));
|
||||
int dataSize = resized.Rows * resized.Cols * resized.ElemSize();
|
||||
var byteArray = new byte[dataSize];
|
||||
Marshal.Copy(resized.Data, byteArray, 0, dataSize);
|
||||
|
||||
var allowed = FilterClasses.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToArray();
|
||||
|
||||
var allowedFlags = new byte[128];
|
||||
foreach (var i in allowed)
|
||||
{
|
||||
allowedFlags[i] = 1;
|
||||
}
|
||||
|
||||
var resultEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall(IsRaw?"predict_raw": "predict", byteArray));
|
||||
|
||||
var result = resultEncoded.Select(Convert.ToByte).ToArray();
|
||||
|
||||
byte[] resultColor;
|
||||
if (IsRaw)
|
||||
{
|
||||
resultColor = result.Chunk(25).Select(x =>
|
||||
{
|
||||
return x.Select((x, i) => allowedFlags[i] == 1 ? (byte) (x) : (byte) 0).Max();
|
||||
}).ToArray();
|
||||
//resultColor = result.Select(x => (byte)(x)).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
resultColor = result.Select(x => allowedFlags[x]==1? (byte)(x * 255) : (byte)0).ToArray();
|
||||
}
|
||||
|
||||
|
||||
// black and white mask
|
||||
var mask = new Mat(_imageHeight, _imageWidth, MatType.CV_8UC1, resultColor);
|
||||
|
||||
var maskResized = mask.Resize(new Size(currentImage.ImageData.Width, currentImage.ImageData.Height));
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = maskResized };
|
||||
|
||||
|
||||
Result = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
public class ColorModelOperation : ColorAIOperation
|
||||
{
|
||||
public ColorModelOperation() : base("office2")
|
||||
{
|
||||
IsRaw = true;
|
||||
}
|
||||
|
||||
|
||||
public override string FilterClasses { get; set; } = "1";
|
||||
public override FilePath ModelFilePath { get; set; } = new FilePath() { Format = "Model file(*.h5)|*.h5" };
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(FilterClasses);
|
||||
// get current directory
|
||||
var dir = Directory.GetCurrentDirectory();
|
||||
// get relative path
|
||||
var relPath = Path.GetRelativePath(dir, ModelFilePath.Path);
|
||||
|
||||
|
||||
bw.Write(relPath);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
FilterClasses = br.ReadString();
|
||||
ModelFilePath.Path = br.ReadString();
|
||||
// get current directory
|
||||
var dir = Directory.GetCurrentDirectory();
|
||||
// get absolute path
|
||||
var absPath = Path.GetFullPath(Path.Combine(dir, ModelFilePath.Path));
|
||||
ModelFilePath.Path = absPath;
|
||||
}
|
||||
}
|
||||
159
Hawkeye.VisionBuilder.Workflow/Operations/AI/ModelAIOperation.cs
Normal file
159
Hawkeye.VisionBuilder.Workflow/Operations/AI/ModelAIOperation.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
public class ModelAIOperation: BaseOperation
|
||||
{
|
||||
|
||||
|
||||
private readonly string _modelName;
|
||||
|
||||
public ModelAIOperation()
|
||||
{
|
||||
|
||||
_cSharpDataTransferMQRPC = PythonModelProxyRPC.GetInterface();
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
private readonly CSharpDataTransferMQRPC _cSharpDataTransferMQRPC;
|
||||
private int _imageWidth;
|
||||
private int _imageHeight;
|
||||
|
||||
|
||||
|
||||
public string FilterClasses { get; set; } = "1";
|
||||
|
||||
private bool _initialized = false;
|
||||
private FilePath _modelFilePath=new FilePath();
|
||||
|
||||
|
||||
public FilePath ModelFilePath
|
||||
{
|
||||
get => _modelFilePath;
|
||||
set
|
||||
{
|
||||
_modelFilePath = value;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCategorical { get; set; }
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ModelFilePath?.Path))
|
||||
{
|
||||
this.SetError("Model file not found");
|
||||
return;
|
||||
}
|
||||
|
||||
// is relative path
|
||||
if (ModelFilePath.Path.StartsWith("."))
|
||||
{
|
||||
var dir = Directory.GetCurrentDirectory();
|
||||
var absPath = Path.GetFullPath(Path.Combine(dir, ModelFilePath.Path));
|
||||
ModelFilePath.Path = absPath;
|
||||
}
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("load_model", ModelFilePath.Path, _modelName));
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("activate_model", _modelName));
|
||||
|
||||
var sizeEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_accept_size"));
|
||||
var size = sizeEncoded.Select(Convert.ToInt32).ToArray();
|
||||
_imageWidth = size[1];
|
||||
_imageHeight = size[2];
|
||||
|
||||
var outputSize = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_output_size"));
|
||||
var outputSizeArray = outputSize.Select(Convert.ToInt32).ToArray();
|
||||
|
||||
int classes;
|
||||
if (outputSizeArray.Length<3)
|
||||
{
|
||||
classes=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
classes = outputSizeArray[2];
|
||||
}
|
||||
|
||||
|
||||
|
||||
var currentImage = context.ActiveImage;
|
||||
|
||||
var rightColor = currentImage.ImageData.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
var resized = rightColor.Resize(new Size(_imageHeight, _imageWidth));
|
||||
int dataSize = resized.Rows * resized.Cols * resized.ElemSize();
|
||||
var byteArray = new byte[dataSize];
|
||||
Marshal.Copy(resized.Data, byteArray, 0, dataSize);
|
||||
|
||||
var allowed = FilterClasses.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToArray();
|
||||
|
||||
var allowedFlags = new byte[128];
|
||||
foreach (var i in allowed)
|
||||
{
|
||||
allowedFlags[i] = 1;
|
||||
}
|
||||
|
||||
var resultEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall(IsCategorical ? "predict_raw" : "predict", byteArray));
|
||||
|
||||
var result = resultEncoded.Select(Convert.ToByte).ToArray();
|
||||
// todo: copy and name RawModelAIOperation
|
||||
byte[] resultColor;
|
||||
if (IsCategorical)
|
||||
{
|
||||
resultColor = result.Chunk(classes).Select(x =>
|
||||
{
|
||||
return x.Select((x, i) => allowedFlags[i] == 1 ? (byte)(x) : (byte)0).Max();
|
||||
}).ToArray();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
resultColor = result.Select(x => allowedFlags[x] == 1 ? (byte)(x * 255) : (byte)0).ToArray();
|
||||
}
|
||||
|
||||
|
||||
// black and white mask
|
||||
var mask = new Mat(outputSizeArray[0], outputSizeArray[1], MatType.CV_8UC1, resultColor);
|
||||
|
||||
var maskResized = mask.Resize(new Size(currentImage.ImageData.Width, currentImage.ImageData.Height));
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = maskResized };
|
||||
|
||||
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(_modelFilePath.Path);
|
||||
bw.Write(_modelFilePath.Format);
|
||||
bw.Write(FilterClasses);
|
||||
bw.Write(IsCategorical);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
_modelFilePath.Path = br.ReadString();
|
||||
_modelFilePath.Format = br.ReadString();
|
||||
FilterClasses = br.ReadString();
|
||||
IsCategorical = br.ReadBoolean();
|
||||
}
|
||||
}
|
||||
148
Hawkeye.VisionBuilder.Workflow/Operations/AI/MultichannelAI.cs
Normal file
148
Hawkeye.VisionBuilder.Workflow/Operations/AI/MultichannelAI.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
public class MultichannelAI: BaseOperation
|
||||
{
|
||||
|
||||
private readonly string _modelName;
|
||||
|
||||
public MultichannelAI()
|
||||
{
|
||||
|
||||
_cSharpDataTransferMQRPC = PythonModelProxyRPC.GetInterface();
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
private readonly CSharpDataTransferMQRPC _cSharpDataTransferMQRPC;
|
||||
private int _imageWidth;
|
||||
private int _imageHeight;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private bool _initialized = false;
|
||||
private FilePath _modelFilePath = new FilePath();
|
||||
|
||||
|
||||
public FilePath ModelFilePath
|
||||
{
|
||||
get => _modelFilePath;
|
||||
set
|
||||
{
|
||||
_modelFilePath = value;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected unsafe override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ModelFilePath?.Path))
|
||||
{
|
||||
this.SetError("Model file not found");
|
||||
return;
|
||||
}
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("load_model", ModelFilePath.Path, _modelName));
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("activate_model", _modelName));
|
||||
|
||||
var sizeEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_accept_size"));
|
||||
var size = sizeEncoded.Select(Convert.ToInt32).ToArray();
|
||||
_imageWidth = size[1];
|
||||
_imageHeight = size[2];
|
||||
|
||||
var outputSize = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_output_size"));
|
||||
var outputSizeArray = outputSize.Select(Convert.ToInt32).ToArray();
|
||||
|
||||
int classes;
|
||||
if (outputSizeArray.Length < 3)
|
||||
{
|
||||
classes = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
classes = outputSizeArray[2];
|
||||
}
|
||||
|
||||
|
||||
|
||||
var currentImage = context.ActiveImage;
|
||||
|
||||
var rightColor = currentImage.ImageData.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
var resized = rightColor.Resize(new Size(_imageHeight, _imageWidth));
|
||||
int dataSize = resized.Rows * resized.Cols * resized.ElemSize();
|
||||
var byteArray = new byte[dataSize];
|
||||
Marshal.Copy(resized.Data, byteArray, 0, dataSize);
|
||||
|
||||
Mat[] channels = new Mat[classes];
|
||||
for (int i = 0; i < classes; i++)
|
||||
{
|
||||
channels[i] = new Mat(_imageHeight, _imageWidth, MatType.CV_8UC1);
|
||||
}
|
||||
|
||||
var resultEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("predict_raw", byteArray));
|
||||
|
||||
var result = resultEncoded.Select(Convert.ToByte).ToArray();
|
||||
|
||||
var chunked = result.Chunk(classes).ToArray();
|
||||
|
||||
int pixel=0;
|
||||
foreach (var chunk in chunked)
|
||||
{
|
||||
for (int i = 0; i < classes; i++)
|
||||
{
|
||||
var data = (byte*)channels[i].Data.ToPointer();
|
||||
var coordX = (int)(pixel / _imageWidth);
|
||||
var coordY = pixel % _imageWidth;
|
||||
data[coordX * _imageWidth + coordY] = chunk[i];
|
||||
}
|
||||
pixel++;
|
||||
}
|
||||
|
||||
//Cv2.ImShow("0", channels[0]);
|
||||
//Cv2.ImShow("1", channels[1]);
|
||||
|
||||
using var res = new Mat();
|
||||
Cv2.Merge(channels,res);
|
||||
|
||||
var resResized = res.Resize(new Size(currentImage.ImageData.Width, currentImage.ImageData.Height));
|
||||
context.ActiveImage = new HawkeyeImage() {ImageData = resResized };
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(_modelFilePath.Path);
|
||||
bw.Write(_modelFilePath.Format);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
_modelFilePath.Path = br.ReadString();
|
||||
_modelFilePath.Format = br.ReadString();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using OpenCvSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using Hawkeye.VisionBuilder.Workflow.DataTransfer;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
public class RawModelAIOperation: BaseOperation
|
||||
{
|
||||
|
||||
|
||||
private string _modelName;
|
||||
|
||||
public RawModelAIOperation()
|
||||
{
|
||||
|
||||
_cSharpDataTransferMQRPC = PythonModelProxyRPC.GetInterface();
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
private CSharpDataTransferMQRPC _cSharpDataTransferMQRPC;
|
||||
private int _imageWidth;
|
||||
private int _imageHeight;
|
||||
|
||||
|
||||
|
||||
public string FilterClasses { get; set; } = "1";
|
||||
|
||||
private bool _initialized = false;
|
||||
private FilePath _modelFilePath=new FilePath();
|
||||
|
||||
|
||||
public FilePath ModelFilePath
|
||||
{
|
||||
get => _modelFilePath;
|
||||
set
|
||||
{
|
||||
_modelFilePath = value;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsCategorical { get; set; }
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ModelFilePath?.Path))
|
||||
{
|
||||
this.SetError("Model file not found");
|
||||
return;
|
||||
}
|
||||
|
||||
_modelName = Path.GetFileNameWithoutExtension(ModelFilePath.Path);
|
||||
|
||||
// is relative path
|
||||
if (ModelFilePath.Path.StartsWith("."))
|
||||
{
|
||||
var dir = Directory.GetCurrentDirectory();
|
||||
var absPath = Path.GetFullPath(Path.Combine(dir, ModelFilePath.Path));
|
||||
ModelFilePath.Path = absPath;
|
||||
}
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("load_model", ModelFilePath.Path, _modelName));
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
_cSharpDataTransferMQRPC.TransferData<object>(new MethodCall("activate_model", _modelName));
|
||||
|
||||
var sizeEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_accept_size"));
|
||||
var size = sizeEncoded.Select(Convert.ToInt32).ToArray();
|
||||
_imageWidth = size[1];
|
||||
_imageHeight = size[2];
|
||||
|
||||
var outputSize = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("get_output_size"));
|
||||
var outputSizeArray = outputSize.Select(Convert.ToInt32).ToArray();
|
||||
|
||||
int classes;
|
||||
if (outputSizeArray.Length<3)
|
||||
{
|
||||
classes=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
classes = outputSizeArray[2];
|
||||
}
|
||||
|
||||
|
||||
|
||||
var currentImage = context.ActiveImage;
|
||||
|
||||
var rightColor = currentImage.ImageData.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
var resized = rightColor.Resize(new Size(_imageHeight, _imageWidth));
|
||||
int dataSize = resized.Rows * resized.Cols * resized.ElemSize();
|
||||
var byteArray = new byte[dataSize];
|
||||
Marshal.Copy(resized.Data, byteArray, 0, dataSize);
|
||||
|
||||
var allowed = FilterClasses.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToArray();
|
||||
|
||||
var allowedFlags = new byte[128];
|
||||
foreach (var i in allowed)
|
||||
{
|
||||
allowedFlags[i] = 1;
|
||||
}
|
||||
|
||||
var resultEncoded = _cSharpDataTransferMQRPC.TransferData<object[]>(new MethodCall("predict_raw", byteArray));
|
||||
|
||||
var result = resultEncoded.Select(Convert.ToByte).ToArray();
|
||||
|
||||
byte[] resultColor;
|
||||
resultColor = result.Select(x => (byte)(x) ).ToArray();
|
||||
|
||||
|
||||
// black and white mask
|
||||
var mask = new Mat(outputSizeArray[0], outputSizeArray[1], MatType.CV_8UC1, resultColor);
|
||||
|
||||
var maskResized = mask.Resize(new Size(currentImage.ImageData.Width, currentImage.ImageData.Height));
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = maskResized };
|
||||
|
||||
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(_modelFilePath.Path);
|
||||
bw.Write(_modelFilePath.Format);
|
||||
bw.Write(FilterClasses);
|
||||
bw.Write(IsCategorical);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
_modelFilePath.Path = br.ReadString();
|
||||
_modelFilePath.Format = br.ReadString();
|
||||
FilterClasses = br.ReadString();
|
||||
IsCategorical = br.ReadBoolean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
using System.Diagnostics;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using static IronPython.Runtime.Profiler;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Compunet.YoloV8;
|
||||
using Compunet.YoloV8.Data;
|
||||
using OpenCvSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
[Category("AI")]
|
||||
public class YoloDetectionOperation:BaseOperation
|
||||
{
|
||||
|
||||
private bool _initialized = false;
|
||||
private FilePath _modelFilePath = new FilePath(){ Format = "Onnx format(*.onnx)|*.onnx" };
|
||||
|
||||
|
||||
public FilePath ModelFilePath
|
||||
{
|
||||
get => _modelFilePath;
|
||||
set
|
||||
{
|
||||
_modelFilePath = value;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EnsureInitialized()
|
||||
{
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ModelFilePath?.Path))
|
||||
{
|
||||
this.SetError("Model file not found");
|
||||
return false;
|
||||
}
|
||||
_predictor = YoloV8Predictor.Create(ModelFilePath?.Path);
|
||||
|
||||
_predictor.Configuration.SuppressParallelInference=true;
|
||||
_initialized = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
byte[] _buffer=new byte[0];
|
||||
private YoloV8Predictor _predictor;
|
||||
|
||||
|
||||
void EnsureBuffer(int size)
|
||||
{
|
||||
if (_buffer.Length != size)
|
||||
{
|
||||
_buffer = new byte[size];
|
||||
}
|
||||
}
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
CheckColorful(context);
|
||||
try
|
||||
{
|
||||
if (!EnsureInitialized()) return;
|
||||
var swImagePreparation = Stopwatch.StartNew();
|
||||
|
||||
var image = context.ActiveImage.ImageData.CvtColor(ColorConversionCodes.BGR2RGB);
|
||||
EnsureBuffer(image.Cols * image.Rows * image.Channels());
|
||||
Marshal.Copy(image.Data, _buffer, 0, _buffer.Length);
|
||||
Image<Rgb24> img = SixLabors.ImageSharp.Image.LoadPixelData<Rgb24>(_buffer, image.Width, image.Height);
|
||||
// convert to rgb24
|
||||
|
||||
|
||||
swImagePreparation.Stop();
|
||||
|
||||
var swPrediction = Stopwatch.StartNew();
|
||||
|
||||
|
||||
|
||||
var result = _predictor.Detect(img);
|
||||
|
||||
|
||||
|
||||
swPrediction.Stop();
|
||||
|
||||
|
||||
var swPostProcessing = Stopwatch.StartNew();
|
||||
Mat res = new Mat(image.Size(), MatType.CV_8UC1, new Scalar(0));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Dictionary<int, string> classes = new Dictionary<int, string>();
|
||||
|
||||
|
||||
|
||||
foreach (BoundingBox box in result.Boxes.OrderBy(x=>x.Class.Id).ToList())
|
||||
{
|
||||
if (box.Confidence<0.7)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (box.Class.Id + 1 == 10)
|
||||
{
|
||||
|
||||
}
|
||||
classes[box.Class.Id + 1] = box.Class.Name;
|
||||
var rect = box.Bounds;
|
||||
res.Rectangle(new OpenCvSharp.Rect(rect.Left, rect.Top, rect.Width, rect.Height),
|
||||
new Scalar(box.Class.Id + 1), -1);
|
||||
}
|
||||
swPostProcessing.Stop();
|
||||
|
||||
|
||||
foreach (var c in classes)
|
||||
{
|
||||
sb.AppendLine($"{c.Key}={c.Value};");
|
||||
}
|
||||
|
||||
Status = sb.ToString();
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() {ImageData = res};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(ModelFilePath.Path);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
ModelFilePath.Path = br.ReadString();
|
||||
}
|
||||
}
|
||||
115
Hawkeye.VisionBuilder.Workflow/Operations/AI/YoloPickDetected.cs
Normal file
115
Hawkeye.VisionBuilder.Workflow/Operations/AI/YoloPickDetected.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
using static Hawkeye.VisionBuilder.Workflow.Operations.FindManyBlobsOperation;
|
||||
using Point = OpenCvSharp.Point;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.AI;
|
||||
|
||||
[Category("AI")]
|
||||
public class YoloPickDetected:BaseOperation
|
||||
{
|
||||
public string SlotName { get; set; } = "Image";
|
||||
public int TypeId { get; set; }
|
||||
|
||||
public int MinArea { get; set; } = 0;
|
||||
public int MaxArea { get; set; } = 99999;
|
||||
public int MinWidth { get; set; } = 0;
|
||||
public int MaxWidth { get; set; } = 99999;
|
||||
public int MinHeight { get; set; } = 0;
|
||||
public int MaxHeight { get; set; } = 99999;
|
||||
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
Status = "No blobs found";
|
||||
var image=context.Memory[SlotName];
|
||||
var threshold= image.ImageData.InRange(TypeId, TypeId);
|
||||
var contours = threshold
|
||||
.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
|
||||
|
||||
//case: no blobs found
|
||||
if (contours.Length == 0)
|
||||
{
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var validContours = new List<Point[]>();
|
||||
|
||||
foreach (var contour in contours)
|
||||
{
|
||||
var boundingRect = Cv2.BoundingRect(contour);
|
||||
var area = Cv2.ContourArea(contour);
|
||||
if (boundingRect.Width >= MinWidth && boundingRect.Width <= MaxWidth &&
|
||||
boundingRect.Height >= MinHeight && boundingRect.Height <= MaxHeight &&
|
||||
area >= MinArea && area <= MaxArea
|
||||
)
|
||||
{
|
||||
validContours.Add(contour);
|
||||
}
|
||||
}
|
||||
|
||||
int badCount = 0;
|
||||
foreach (var contourMatch in validContours)
|
||||
{
|
||||
PolyElement poly = new PolyElement() { IsGood = false };
|
||||
|
||||
poly.Points = contourMatch.Select(x => new Vector2(x.X, x.Y)).ToArray();
|
||||
|
||||
context.GraphicsElements.Add(poly);
|
||||
badCount++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (badCount > 0)
|
||||
{
|
||||
// find smallest contour size
|
||||
var smallest = validContours.OrderBy(x => Cv2.ContourArea(x)).First();
|
||||
var smallestBoundingRect = Cv2.BoundingRect(smallest);
|
||||
var biggest = validContours.OrderBy(x => Cv2.ContourArea(x)).Last();
|
||||
var biggestBoundingRect = Cv2.BoundingRect(biggest);
|
||||
var smallestArea = Cv2.ContourArea(smallest);
|
||||
var biggestArea = Cv2.ContourArea(biggest);
|
||||
Status = $"Found: {badCount} Min area:{smallestArea} Max area:{biggestArea}";
|
||||
|
||||
|
||||
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
Result = true;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(SlotName);
|
||||
bw.Write(TypeId);
|
||||
bw.Write(MinWidth);
|
||||
bw.Write(MaxWidth);
|
||||
bw.Write(MinHeight);
|
||||
bw.Write(MaxHeight);
|
||||
bw.Write(MinArea);
|
||||
bw.Write(MaxArea);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
SlotName = br.ReadString();
|
||||
TypeId = br.ReadInt32();
|
||||
MinWidth = br.ReadInt32();
|
||||
MaxWidth = br.ReadInt32();
|
||||
MinHeight = br.ReadInt32();
|
||||
MaxHeight = br.ReadInt32();
|
||||
MinArea = br.ReadInt32();
|
||||
MaxArea = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
|
||||
public class ActivateImageOperation:BaseOperation
|
||||
{
|
||||
public ScriptValue ImageSource { get; set; } = new ScriptValue();
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
context.ActiveImage = GetScriptValue<HawkeyeImage>(ImageSource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class Category:Attribute
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
public Category(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class,Inherited = false)]
|
||||
public class IgnoreOperationAttribute : Attribute
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class NotForToolAttribute:Attribute
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class RestrictReferenceAttribute:Attribute
|
||||
{
|
||||
public RestrictReferenceAttribute(Type type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public Type Type { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;
|
||||
|
||||
[Category("Basic")]
|
||||
public class CannyOperation:BaseOperation
|
||||
{
|
||||
public int Threshold1 { get; set; } = 100;
|
||||
public int Threshold2 { get; set; } = 200;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage.ImageData.Canny(Threshold1, Threshold2)
|
||||
};
|
||||
|
||||
Status = $"Canny with Threshold1={Threshold1} and Threshold2={Threshold2}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(Threshold1);
|
||||
bw.Write(Threshold2);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
Threshold1 = br.ReadInt32();
|
||||
Threshold2 = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;
|
||||
|
||||
[Category("Basic")]
|
||||
public class DetectionPaddingOperation:BaseOperation
|
||||
{
|
||||
public int HorizontalPadding { get; set; }
|
||||
public int VerticalPadding { get; set; }
|
||||
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
// fill edges with black on top and bottom without changing the image size
|
||||
var image = context.ActiveImage;
|
||||
var newImage = new Mat(image.ImageData.Rows, image.ImageData.Cols, MatType.CV_8UC1, Scalar.Black);
|
||||
|
||||
|
||||
|
||||
var mask = new Mat(image.ImageData.Rows, image.ImageData.Cols, MatType.CV_8UC1, Scalar.Black);
|
||||
var maskRoi = mask[VerticalPadding, image.ImageData.Rows - VerticalPadding, HorizontalPadding, image.ImageData.Cols - HorizontalPadding];
|
||||
//fill the mask with white
|
||||
maskRoi.SetTo(Scalar.White);
|
||||
|
||||
image.ImageData.CopyTo(newImage, mask);
|
||||
|
||||
mask.Dispose();
|
||||
maskRoi.Dispose();
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = newImage
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(HorizontalPadding);
|
||||
bw.Write(VerticalPadding);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
HorizontalPadding = br.ReadInt32();
|
||||
VerticalPadding = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;
|
||||
|
||||
[Category("Basic")]
|
||||
public class GaussianBlurOperation : BaseOperation
|
||||
{
|
||||
public ScriptValue KernelSize { get; set; } = new ScriptValue() { Script = "3" };
|
||||
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
var kernelSize = GetScriptValue<int>(KernelSize);
|
||||
if (kernelSize < 1||IsEven(kernelSize))
|
||||
{
|
||||
Status = $"{nameof(KernelSize)} should be more than 0 and be odd number";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage.ImageData.GaussianBlur(new OpenCvSharp.Size(kernelSize, kernelSize), 0, 0)
|
||||
};
|
||||
Status = $"Gaussian blur with kernel size {KernelSize}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
private bool IsEven(int kernelSize)
|
||||
{
|
||||
return kernelSize % 2 == 0;
|
||||
}
|
||||
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(KernelSize.Script);
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
KernelSize.Script = br.ReadString();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Line;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp.Extensions;
|
||||
using Point = OpenCvSharp.Point;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;
|
||||
|
||||
[Category("Basic")]
|
||||
public class PorabollisticHoughOperation:BaseOperation
|
||||
{
|
||||
public double DistanceResolution { get; set; } = 1;
|
||||
public double AngleResolution { get; set; } = Math.PI / 180.0;
|
||||
public int Threshold { get; set; } = 50;
|
||||
public int MinLineLength { get; set; } = 50;
|
||||
public int MaxLineGap { get; set; } = 10;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
var segments = context.ActiveImage.ImageData.HoughLinesP(DistanceResolution, AngleResolution, Threshold, MinLineLength, MaxLineGap);
|
||||
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
context.GraphicsElements.Add(new LineElement()
|
||||
{
|
||||
Start = new Vector2(segment.P1.X, segment.P1.Y),
|
||||
End = new Vector2(segment.P2.X, segment.P2.Y)
|
||||
});
|
||||
}
|
||||
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(DistanceResolution);
|
||||
bw.Write(AngleResolution);
|
||||
bw.Write(Threshold);
|
||||
bw.Write(MinLineLength);
|
||||
bw.Write(MaxLineGap);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
DistanceResolution = br.ReadDouble();
|
||||
AngleResolution = br.ReadDouble();
|
||||
Threshold = br.ReadInt32();
|
||||
MinLineLength = br.ReadInt32();
|
||||
MaxLineGap = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;
|
||||
|
||||
[Category("Basic")]
|
||||
public class SkeletonOperation:BaseOperation
|
||||
{
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if(!CheckGrayscale(context)) return;
|
||||
|
||||
|
||||
var skeleton = new Mat(context.ActiveImage.ImageData.Size(), MatType.CV_8UC1, 0);
|
||||
var temp = context.ActiveImage.ImageData.Clone();
|
||||
var element = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(3, 3));
|
||||
var image = context.ActiveImage.ImageData.Clone();
|
||||
|
||||
do
|
||||
{
|
||||
var eroded = image.Erode(element);
|
||||
|
||||
temp =eroded.Dilate(element);
|
||||
|
||||
Cv2.Subtract(image,temp, temp);
|
||||
Cv2.BitwiseOr(skeleton, temp, skeleton);
|
||||
|
||||
eroded.CopyTo(image);
|
||||
} while (Cv2.CountNonZero(image) != 0);
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = skeleton
|
||||
};
|
||||
|
||||
Result = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;
|
||||
|
||||
using global::Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
|
||||
|
||||
[Category("Basic")]
|
||||
public class ThresholdMinMaxOperation : BaseOperation
|
||||
{
|
||||
public ScriptValue ThresholdMin { get; set; } = new ScriptValue() { Script = "128" };
|
||||
public ScriptValue ThresholdMax { get; set; } = new ScriptValue() { Script = "255" };
|
||||
|
||||
public ThresholdMinMaxOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage.ImageData.InRange(GetScriptValue<float>(ThresholdMin), GetScriptValue<float>(ThresholdMax))
|
||||
};
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(ThresholdMin.Script);
|
||||
bw.Write(ThresholdMax.Script);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
ThresholdMin.Script = br.ReadString();
|
||||
ThresholdMax.Script = br.ReadString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Basic;
|
||||
|
||||
[Category("Basic")]
|
||||
public class ThresholdOperation:BaseOperation
|
||||
{
|
||||
|
||||
public ScriptValue ThresholdMin { get; set; } = new ScriptValue(){Script = "128"};
|
||||
|
||||
public ThresholdOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
if(!CheckGrayscale(context))return;
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage.ImageData.Threshold(GetScriptValue<float>(ThresholdMin), 255,ThresholdTypes.Binary)
|
||||
};
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(ThresholdMin.Script);
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
ThresholdMin.Script = br.ReadString();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Edge;
|
||||
|
||||
[Category("Edge")]
|
||||
public class SobelOperation:BaseOperation
|
||||
{
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if(!CheckImageExists(context)) return;
|
||||
var sobelY = context.ActiveImage!.ImageData.Sobel(MatType.CV_16S, 0, 1);
|
||||
var sobelX = context.ActiveImage!.ImageData.Sobel(MatType.CV_16S, 1, 0);
|
||||
|
||||
sobelX = sobelX.ConvertScaleAbs() * 0.5;
|
||||
sobelY = sobelY.ConvertScaleAbs() * 0.5;
|
||||
|
||||
sobelX += sobelY;
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = sobelX
|
||||
};
|
||||
Status = "Sobel";
|
||||
Result = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Hawkeye.VisionBuilder.Workflow.ColorStudioClasses;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Filters;
|
||||
|
||||
[Category("Filters")]
|
||||
public class ColorProfileOperation:BaseOperation
|
||||
{
|
||||
private FilePath _colorProfilePath = new FilePath(){Format = "*.colorprofile|*.colorprofile" };
|
||||
|
||||
|
||||
bool _initialized = false;
|
||||
private CategoryTree _categoryTree;
|
||||
private CategoryItem _categoryItem;
|
||||
private SelectFromList _category;
|
||||
|
||||
|
||||
public Action RunColorStudio => RunColorStudioInternal;
|
||||
|
||||
void RunColorStudioInternal()
|
||||
{
|
||||
var colorStudioPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"..\ColorStudio\Hawkeye.ColorStudio.exe");
|
||||
var process = Process.Start(colorStudioPath, new[]{ _colorProfilePath.Path,_lastImagePath,Category.Value});
|
||||
process?.WaitForExit();
|
||||
_initialized=false;
|
||||
}
|
||||
|
||||
|
||||
public ColorProfileOperation()
|
||||
{
|
||||
_category = new SelectFromList()
|
||||
{
|
||||
Value = "none",
|
||||
GetList = ()=> GetCategories()
|
||||
};
|
||||
}
|
||||
|
||||
private List<string> GetCategories()
|
||||
{
|
||||
var list = new List<string>();
|
||||
if (_categoryTree != null)
|
||||
{
|
||||
list = _categoryTree.CategoryItems.Select(c => c.Name).ToList();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public SelectFromList Category
|
||||
{
|
||||
get => _category;
|
||||
set
|
||||
{
|
||||
_category = value;
|
||||
_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
public FilePath ColorProfilePath
|
||||
{
|
||||
get => _colorProfilePath;
|
||||
set
|
||||
{
|
||||
_initialized=false;
|
||||
_colorProfilePath = value;
|
||||
}
|
||||
}
|
||||
|
||||
string _lastImagePath = "";
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
_lastImagePath = context.ActiveImage?.Filename;
|
||||
if (!_initialized)
|
||||
{
|
||||
if (!File.Exists(ColorProfilePath?.Path))
|
||||
{
|
||||
this.SetError("Color profile file not found");
|
||||
|
||||
Result=false;
|
||||
return;
|
||||
}
|
||||
_categoryTree = CategoryTree.LoadFromFile(ColorProfilePath.Path);
|
||||
_categoryItem = _categoryTree.CategoryItems.FirstOrDefault(c => c.Name == Category.Value);
|
||||
if (_categoryItem == null)
|
||||
{
|
||||
this.SetError("Category not found");
|
||||
Result=false;
|
||||
return;
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
var hsv = context.ActiveImage.ImageData.CvtColor(ColorConversionCodes.BGR2HSV_FULL);
|
||||
var filters = _categoryItem.Filters;
|
||||
|
||||
|
||||
int index = 0;
|
||||
var resized = hsv.Resize(new Size(256,256),interpolation:InterpolationFlags.Nearest);
|
||||
Mat res = new Mat(resized.Size(), MatType.CV_8UC1, (Scalar)0);
|
||||
foreach (var filter in filters)
|
||||
{
|
||||
|
||||
filter.Transform(ref resized, ref res);
|
||||
|
||||
index++;
|
||||
}
|
||||
var upsampled = res.Resize(hsv.Size(), interpolation:InterpolationFlags.Nearest);
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() {ImageData = upsampled };
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(_colorProfilePath.Path);
|
||||
bw.Write(_category.Value);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
_colorProfilePath.Path = EnsureRelativePath(br.ReadString());
|
||||
_category.Value = br.ReadString();
|
||||
_initialized = false;
|
||||
}
|
||||
|
||||
string EnsureRelativePath(string path)
|
||||
{
|
||||
if (path.StartsWith(".."))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
path = Path.GetFullPath(path);
|
||||
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
path = Path.GetRelativePath(dir, path);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Filters;
|
||||
|
||||
[Category("Filters")]
|
||||
public class GaborFilterOperation : BaseOperation
|
||||
{
|
||||
public GaborFilterOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public int KSize { get; set; } = 35;
|
||||
public double Sigma { get; set; } = 3;
|
||||
public int NumFilters { get; set; } = 1;
|
||||
public double Lambda { get; set; } = 10;
|
||||
public double Gamma { get; set; } = 0.5;
|
||||
public double ThetaFromAngle { get; set; } = 0;
|
||||
public double ThetaToAngle { get; set; } = 0;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
Result = true;
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
var thetaFrom = ThetaFromAngle / 180 * Math.PI;
|
||||
var thetaTo = ThetaToAngle / 180 * Math.PI;
|
||||
|
||||
|
||||
double[] steps;
|
||||
if (NumFilters == 1)
|
||||
{
|
||||
steps=new double[] {thetaFrom};
|
||||
}
|
||||
else
|
||||
{
|
||||
// check theta not equal
|
||||
if (thetaFrom == thetaTo)
|
||||
{
|
||||
Status = "ThetaFrom and ThetaTo must be different";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
var step = (thetaTo - thetaFrom) / NumFilters;
|
||||
|
||||
steps = Enumerable.Range(0, NumFilters).Select(i => thetaFrom + i * step).ToArray();
|
||||
}
|
||||
|
||||
var image = context.ActiveImage.ImageData;
|
||||
var newimage = new Mat(image.Size(), image.Type(), 0);
|
||||
|
||||
foreach (double theta in steps)
|
||||
{
|
||||
var kernel = Cv2.GetGaborKernel(new Size(KSize, KSize), Sigma, theta, Lambda, Gamma, 0, MatType.CV_64F);
|
||||
kernel /= 1 * kernel.Sum().Val0;
|
||||
var filtered = new Mat(image.Size(), image.Type(), 0);
|
||||
Cv2.Filter2D(image, filtered, -1, kernel);
|
||||
Cv2.Max(newimage, filtered, newimage);
|
||||
}
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = newimage
|
||||
};
|
||||
|
||||
Result = true;
|
||||
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(KSize);
|
||||
bw.Write(Sigma);
|
||||
bw.Write(NumFilters);
|
||||
bw.Write(Lambda);
|
||||
bw.Write(Gamma);
|
||||
bw.Write(ThetaFromAngle);
|
||||
bw.Write(ThetaToAngle);
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
KSize = br.ReadInt32();
|
||||
Sigma = br.ReadDouble();
|
||||
NumFilters = br.ReadInt32();
|
||||
Lambda = br.ReadDouble();
|
||||
Gamma = br.ReadDouble();
|
||||
ThetaFromAngle = br.ReadDouble();
|
||||
ThetaToAngle = br.ReadDouble();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Filters;
|
||||
[Category("Filters")]
|
||||
public class LUTFilterOperation : BaseOperation
|
||||
{
|
||||
|
||||
|
||||
|
||||
public LutData LutData { get; set; } = new LutData();
|
||||
|
||||
[NotForTool]
|
||||
public HawkeyeImage LastImage { get; set; } = null;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
LastImage = context.ActiveImage;
|
||||
var mat = context.ActiveImage.ImageData;
|
||||
if (mat.Channels() == 3)
|
||||
{
|
||||
Mat[] tmp = new Mat[3];
|
||||
Span<byte> span = new Span<byte>(LutData.LUT);
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
var channel = mat.ExtractChannel(i);
|
||||
|
||||
tmp[i] = channel.LUT(span[(i * 256)..((i + 1) * 256)].ToArray());
|
||||
}
|
||||
|
||||
mat = new Mat(mat.Size(), mat.Type());
|
||||
Cv2.Merge(tmp, mat);
|
||||
|
||||
}
|
||||
|
||||
if (mat.Channels() == 1)
|
||||
{
|
||||
Span<byte> span = new Span<byte>(LutData.LUT);
|
||||
mat = mat.LUT(span[0..256].ToArray());
|
||||
}
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = mat };
|
||||
Status = "";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
LutData.Serialize(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
LutData.Deserialize(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
|
||||
[Category("Filters")]
|
||||
public class NoiseFilterOperation:BaseOperation
|
||||
{
|
||||
public int NoiseSize { get; set; }
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if(!CheckGrayscale(context))return;
|
||||
if (NoiseSize < 1)
|
||||
{
|
||||
Status = $"{nameof(NoiseSize)} should be more than 0";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage.ImageData.MorphologyEx(MorphTypes.Open, Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(NoiseSize, NoiseSize)))
|
||||
};
|
||||
Status = $"Remove noise with size less than {NoiseSize}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(NoiseSize);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
NoiseSize = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Filters;
|
||||
|
||||
[Category("Filters")]
|
||||
public class RangeFilterOperation: BaseOperation
|
||||
{
|
||||
public RangeFilterOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public int C1Min { get; set; }
|
||||
public int C1Max { get; set; }
|
||||
public int C2Min { get; set; }
|
||||
public int C2Max { get; set; }
|
||||
public int C3Min { get; set; }
|
||||
public int C3Max { get; set; }
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
var mat = context.ActiveImage.ImageData;
|
||||
var res = mat.InRange(new Scalar(C1Min, C2Min, C3Min), new Scalar(C1Max, C2Max, C3Max));
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = res };
|
||||
Status = "";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(C1Min);
|
||||
bw.Write(C1Max);
|
||||
bw.Write(C2Min);
|
||||
bw.Write(C2Max);
|
||||
bw.Write(C3Min);
|
||||
bw.Write(C3Max);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
C1Min = br.ReadInt32();
|
||||
C1Max = br.ReadInt32();
|
||||
C2Min = br.ReadInt32();
|
||||
C2Max = br.ReadInt32();
|
||||
C3Min = br.ReadInt32();
|
||||
C3Max = br.ReadInt32();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Configuration;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Links;
|
||||
|
||||
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp.Extensions;
|
||||
using Rectangle = Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle.RectangleElement;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
|
||||
public class GetImageOperation:BaseOperation,IHaveImage
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
|
||||
|
||||
|
||||
public GetImageOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
}
|
||||
|
||||
public HawkeyeImage? Image { get; set; }
|
||||
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
|
||||
var image = _workflowList.ImageSource.GetImage(CancellationToken.None).Result;
|
||||
|
||||
|
||||
|
||||
|
||||
context.ActiveImage = new HawkeyeImage(){ImageData = image};
|
||||
context.LastCameraImage = context.ActiveImage;
|
||||
Image = context.ActiveImage;
|
||||
|
||||
if (CheckImageExists(context))
|
||||
{
|
||||
Status= $"Image size: {context.ActiveImage.ImageData.Width}x{context.ActiveImage.ImageData.Height}";
|
||||
Result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = "No image";
|
||||
Result = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Save the operation to the binary writer
|
||||
/// </summary>
|
||||
/// <param name="bw"></param>
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(Id.ToString());
|
||||
bw.Write(Label);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the operation from the binary reader
|
||||
/// </summary>
|
||||
/// <param name="br"></param>
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
Id = new Guid(br.ReadString());
|
||||
Label = br.ReadString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;
|
||||
|
||||
[Category("Image")]
|
||||
public class ConvertOperation: BaseOperation
|
||||
{
|
||||
public enum EConversionType
|
||||
{
|
||||
RGBToHSV,
|
||||
HSVToRGB,
|
||||
}
|
||||
public ConvertOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public EConversionType ConversionType { get; set; }
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
var mat = context.ActiveImage.ImageData;
|
||||
Mat res;
|
||||
switch (ConversionType)
|
||||
{
|
||||
case EConversionType.RGBToHSV:
|
||||
res = mat.CvtColor(OpenCvSharp.ColorConversionCodes.BGR2HSV);
|
||||
break;
|
||||
case EConversionType.HSVToRGB:
|
||||
res = mat.CvtColor(OpenCvSharp.ColorConversionCodes.HSV2BGR);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = res};
|
||||
Status = "";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;
|
||||
|
||||
[Category("Image")]
|
||||
public class GetChannelOperation: BaseOperation
|
||||
{
|
||||
public int Channel { get; set; }
|
||||
|
||||
public GetChannelOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
var channel = context.ActiveImage.ImageData.ExtractChannel(Channel);
|
||||
context.ActiveImage = new HawkeyeImage() {ImageData = channel};
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Get channel {Channel}";
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(Channel);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
Channel = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;
|
||||
|
||||
[Category("Image")]
|
||||
public class ImageSizeOperation : BaseOperation
|
||||
{
|
||||
|
||||
public ImageSizeOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
public int Width { get; set; } = 450;
|
||||
public int Height { get; set; } = 352;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage.ImageData.Resize(new OpenCvSharp.Size(Width, Height))
|
||||
};
|
||||
|
||||
Status = $"Image resized to {Width}x{Height}";
|
||||
Result = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;
|
||||
|
||||
[Category("Image")]
|
||||
public class ImageSizeProportionOperation:BaseOperation
|
||||
{
|
||||
public ImageSizeProportionOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public double Width { get; set; } = 0.25;
|
||||
public double Height { get; set; } = 0.25;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage!.ImageData.Resize(Size.Zero, Width, Height)
|
||||
};
|
||||
|
||||
Status = $"Image resized to {context.ActiveImage.ImageData.Width}x{context.ActiveImage.ImageData.Height}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(Width);
|
||||
bw.Write(Height);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
Width = br.ReadDouble();
|
||||
Height = br.ReadDouble();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;
|
||||
|
||||
[Category("Image")]
|
||||
public class InvertOperation:BaseOperation
|
||||
{
|
||||
|
||||
public InvertOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
|
||||
|
||||
var res = new Mat(context.ActiveImage.ImageData.Size(), context.ActiveImage.ImageData.Type(), 0);
|
||||
Cv2.BitwiseNot(context.ActiveImage.ImageData, res);
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = res
|
||||
};
|
||||
|
||||
Status = $"Image inverted";
|
||||
Result = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Image;
|
||||
|
||||
[Category("Image")]
|
||||
public class ToGrayscaleOperation : BaseOperation
|
||||
{
|
||||
public ToGrayscaleOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
public HawkeyeImage? Image { get; set; }
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckColorful(context)) return;
|
||||
|
||||
context.ActiveImage = new HawkeyeImage() { ImageData = context.ActiveImage.ImageData.CvtColor(ColorConversionCodes.BGR2GRAY) };
|
||||
Status = "Convert image to grayscale";
|
||||
Result = true;
|
||||
Image = context.ActiveImage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Memory;
|
||||
|
||||
[Category("Memory")]
|
||||
public class ImageFromMemoryOperation:BaseOperation
|
||||
{
|
||||
public ImageFromMemoryOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public string SlotName { get; set; } = "Image";
|
||||
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (context.Memory.ContainsKey(SlotName))
|
||||
{
|
||||
context.ActiveImage = context.Memory[SlotName];
|
||||
Status = "Image from slot "+SlotName;
|
||||
Result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Status = "Slot not found";
|
||||
Result = false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(SlotName);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
SlotName = br.ReadString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Memory;
|
||||
|
||||
[Category("Memory")]
|
||||
public class ImageToMemoryOperation:BaseOperation
|
||||
{
|
||||
public ImageToMemoryOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public string SlotName { get; set; } = "Image";
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if(!CheckImageExists(context)) return;
|
||||
|
||||
context.Memory[SlotName] = context.ActiveImage!;
|
||||
Status = "Image to slot "+SlotName;
|
||||
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(SlotName);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
SlotName = br.ReadString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class ClosingOperation:BaseOperation
|
||||
{
|
||||
public ClosingOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public int Closing { get; set; } = 1;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
var element = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(Closing, Closing));
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage!.ImageData.MorphologyEx(MorphTypes.Close, element)
|
||||
};
|
||||
|
||||
Status = $"Closing {Closing}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(Closing);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
Closing = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class CutOperation:BaseOperation
|
||||
{
|
||||
|
||||
private readonly WorkflowList _workflowList;
|
||||
public Guid ReferencePoint { get; set; } = new Guid();
|
||||
|
||||
public RectangleElement? SearchArea { get; set; } = null;
|
||||
|
||||
public CutOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.One * 5,
|
||||
Size = new Vector2(100, 100),
|
||||
IsGood = true
|
||||
};
|
||||
|
||||
CanHaveProcessingError = false;
|
||||
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
|
||||
SearchArea.SetPivot(_workflowList.GetOriginById(ReferencePoint).Origin.Location);
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context))return;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
SearchArea.MovePivot(_workflowList.GetOriginById(ReferencePoint).Origin.Location);
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
|
||||
var img = context.ActiveImage.ImageData;
|
||||
var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
|
||||
SearchArea.FillMat(mat);
|
||||
|
||||
var cut = img.BitwiseAnd(mat).ToMat();
|
||||
|
||||
context.ActiveImage=new HawkeyeImage()
|
||||
{
|
||||
ImageData = cut
|
||||
};
|
||||
|
||||
Status="Cut";
|
||||
Result = true;
|
||||
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(ReferencePoint.ToString());
|
||||
|
||||
SearchArea.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
ReferencePoint = new Guid(br.ReadString());
|
||||
SearchArea = new RectangleElement();
|
||||
|
||||
SearchArea.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class DilationOperation:BaseOperation
|
||||
{
|
||||
|
||||
public DilationOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public int Dilation { get; set; } = 1;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
var element=Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(Dilation, Dilation));
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage!.ImageData.Dilate(element)
|
||||
};
|
||||
|
||||
Status="Dilation "+Dilation;
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(Dilation);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
Dilation = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class HatsOperation : BaseOperation
|
||||
{
|
||||
public HatsOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public int HatSize { get; set; } = 1;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
var element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(HatSize, HatSize));
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage!.ImageData.MorphologyEx(MorphTypes.TopHat, element)
|
||||
};
|
||||
|
||||
Status = $"Black Hat {HatSize}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(HatSize);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
HatSize = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class OpeningOperation : BaseOperation
|
||||
{
|
||||
public OpeningOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
public int Opening { get; set; } = 1;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
var element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(Opening, Opening));
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = context.ActiveImage!.ImageData.MorphologyEx(MorphTypes.Open, element)
|
||||
};
|
||||
|
||||
Status = $"Opening {Opening}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(Opening);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
Opening = br.ReadInt32();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class SubtractOperation : BaseOperation
|
||||
{
|
||||
public string SlotName { get; set; } = "Image";
|
||||
|
||||
public SubtractOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
if (!context.Memory.ContainsKey(SlotName))
|
||||
{
|
||||
Status = "Slot not found";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var first = context.ActiveImage;
|
||||
var second = context.Memory[SlotName];
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = first.ImageData - second.ImageData
|
||||
};
|
||||
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System.Collections;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class TakeBiggestOperation : BaseOperation
|
||||
{
|
||||
public TakeBiggestOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
var image = context.ActiveImage!.ImageData;
|
||||
|
||||
Cv2.FindContours(image, out var contours, out var hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
|
||||
|
||||
if (contours.Length == 0)
|
||||
{
|
||||
SetError("No blobs found.");
|
||||
return;
|
||||
}
|
||||
|
||||
var largestContour = contours.OrderByDescending(c => Cv2.ContourArea(c)).First();
|
||||
var mask = Mat.Zeros(image.Size(), MatType.CV_8UC1).ToMat();
|
||||
|
||||
Cv2.DrawContours(mask, new[] {largestContour}, -1, 255, -1);
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = mask
|
||||
};
|
||||
|
||||
Status = "Biggest blob extracted";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Morphology;
|
||||
|
||||
[Category("Morphology")]
|
||||
public class UnionOperation : BaseOperation
|
||||
{
|
||||
public string SlotName { get; set; } = "Image";
|
||||
|
||||
public UnionOperation()
|
||||
{
|
||||
CanHaveProcessingError = false;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
if (!context.Memory.ContainsKey(SlotName))
|
||||
{
|
||||
Status = "Slot not found";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var first = context.ActiveImage;
|
||||
var second = context.Memory[SlotName];
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = first.ImageData.BitwiseOr(second.ImageData)
|
||||
};
|
||||
|
||||
|
||||
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
|
||||
using Hawkeye.VisionBuilder.Workflow.Links;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
using Point = OpenCvSharp.Point;
|
||||
using RectangleElement = Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle.RectangleElement;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
|
||||
[Category("Simple/Blobs")]
|
||||
public class FindBlobOperation : BaseOperation, IHaveOrigin, IHaveSearchArea
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
|
||||
public int MinArea { get; set; } = 100;
|
||||
|
||||
public int MaxArea { get; set; } = 99999;
|
||||
|
||||
public int MinRadius { get; set; } = 0;
|
||||
|
||||
public int MaxRadius { get; set; } = 99999;
|
||||
|
||||
public bool BadIfFound { get; set; }
|
||||
|
||||
|
||||
|
||||
public Guid ReferenceId { get; set; } = new Guid();
|
||||
|
||||
public RectangleElement SearchArea { get; set; }
|
||||
|
||||
//processing result of current element
|
||||
[NotForTool] public OriginElement Origin { get; private set; } = OriginElement.Default;
|
||||
|
||||
public FindBlobOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.One * 100,
|
||||
Size = Vector2.One * 100,
|
||||
IsGood = true
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
|
||||
var img = context.ActiveImage.ImageData;
|
||||
var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
|
||||
|
||||
SearchArea.FillMat(mat);
|
||||
|
||||
var contoursMat = img.BitwiseAnd(mat).ToMat();
|
||||
|
||||
|
||||
var contours = contoursMat.Threshold(128, 255, ThresholdTypes.Binary)
|
||||
.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
|
||||
|
||||
//case: no blobs found
|
||||
if (contours.Length == 0)
|
||||
{
|
||||
Status = "No blobs found";
|
||||
SearchArea.IsGood = false;
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
Result = BadIfFound;
|
||||
return;
|
||||
}
|
||||
|
||||
int GetContourRadius(Point[] contour)
|
||||
{
|
||||
Cv2.MinEnclosingCircle(contour, out var center, out var radius);
|
||||
return (int)radius;
|
||||
}
|
||||
//case: blobs found but not matching criteria
|
||||
var contour = contours.Select(x => new { c = x, area = (int)Cv2.ContourArea(x),radius= GetContourRadius(x) })
|
||||
.OrderBy(x => Math.Min(Math.Abs(x.area - MinArea), Math.Abs(x.area - MaxArea)));
|
||||
var match = contour.Where(x=>x.c.Length>1)
|
||||
.Where(x => x.area >= MinArea && x.area <= MaxArea)
|
||||
.FirstOrDefault(x => x.radius >= MinRadius && x.radius <= MaxRadius);
|
||||
|
||||
if (match == null)
|
||||
{
|
||||
var best = contour.First();
|
||||
|
||||
Status = $"Area: {best.area} Radius: {best.radius}";
|
||||
PolyElement poly = new PolyElement() { IsGood = BadIfFound };
|
||||
|
||||
poly.Points = best.c.Select(x => new Vector2(x.X, x.Y)).ToArray();
|
||||
|
||||
context.GraphicsElements.Add(poly);
|
||||
|
||||
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
Result = BadIfFound;
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
PolyElement poly = new PolyElement() { IsGood = !BadIfFound };
|
||||
|
||||
poly.Points = match.c.Select(x => new Vector2(x.X, x.Y)).ToArray();
|
||||
|
||||
context.GraphicsElements.Add(poly);
|
||||
SearchArea.IsGood = true;
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
var center = poly.Points.Aggregate((v1, v2) => v1 + v2) / poly.Points.Length;
|
||||
Origin = new OriginElement() { Location = center };
|
||||
Status = $"Area: {match.area} Radius: {match.radius}";
|
||||
Result = !BadIfFound;
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
|
||||
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
}
|
||||
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(Id.ToString());
|
||||
bw.Write(Label);
|
||||
bw.Write(MinArea);
|
||||
bw.Write(MaxArea);
|
||||
bw.Write(MinRadius);
|
||||
bw.Write(MaxRadius);
|
||||
bw.Write(BadIfFound);
|
||||
bw.Write(ReferenceId.ToString());
|
||||
|
||||
SearchArea.Save(bw);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
Id = new Guid(br.ReadString());
|
||||
Label = br.ReadString();
|
||||
MinArea = br.ReadInt32();
|
||||
MaxArea = br.ReadInt32();
|
||||
MinRadius = br.ReadInt32();
|
||||
MaxRadius = br.ReadInt32();
|
||||
BadIfFound = br.ReadBoolean();
|
||||
ReferenceId = new Guid(br.ReadString());
|
||||
|
||||
SearchArea.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
using Point = OpenCvSharp.Point;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
|
||||
[Category("Simple/Blobs")]
|
||||
public class FindManyBlobsExtOperation : BaseOperation
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
|
||||
public int MinArea { get; set; } = 100;
|
||||
public int MaxArea { get; set; } = 99999;
|
||||
|
||||
public int MinRadius { get; set; } = 0;
|
||||
public int MaxRadius { get; set; } = 99999;
|
||||
|
||||
public bool CheckInternalRadius { get; set; } = false;
|
||||
public int MinInternalRadius { get; set; } = 0;
|
||||
public int MaxInternalRadius { get; set; } = 99999;
|
||||
|
||||
public bool CheckWidth { get; set; } = false;
|
||||
public int MinWidth { get; set; } = 0;
|
||||
public int MaxWidth { get; set; } = 99999;
|
||||
|
||||
public bool CheckHeight { get; set; } = false;
|
||||
public int MinHeight { get; set; } = 0;
|
||||
public int MaxHeight { get; set; } = 99999;
|
||||
|
||||
public Guid ReferenceId { get; set; } = new Guid();
|
||||
|
||||
public RectangleElement SearchArea { get; set; }
|
||||
|
||||
public FindManyBlobsExtOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.One * 100,
|
||||
Size = Vector2.One * 100,
|
||||
IsGood = true
|
||||
};
|
||||
}
|
||||
|
||||
public record ContourMatch(Point[] c, int area, int radius, int internalRadius, int width, int height)
|
||||
{
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{{area={area}, radius={radius}, internalRadius={internalRadius}, width={width}, height={height}}}";
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
Result = true;
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
if (SearchArea == null)
|
||||
{
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.Zero,
|
||||
Size = new Vector2(context.ActiveImage.ImageData.Width, context.ActiveImage.ImageData.Height),
|
||||
IsGood = true
|
||||
};
|
||||
}
|
||||
|
||||
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
|
||||
var img = context.ActiveImage.ImageData;
|
||||
var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
|
||||
|
||||
SearchArea.FillMat(mat);
|
||||
|
||||
var contoursMat = img.BitwiseAnd(mat).ToMat();
|
||||
|
||||
var contours = contoursMat.Threshold(128, 255, ThresholdTypes.Binary)
|
||||
.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
|
||||
// Case: no blobs found
|
||||
if (contours.Length == 0)
|
||||
{
|
||||
Status = "No blobs found";
|
||||
SearchArea.IsGood = true;
|
||||
return;
|
||||
}
|
||||
|
||||
int GetContourRadius(Point[] contour)
|
||||
{
|
||||
Cv2.MinEnclosingCircle(contour, out _, out var radius);
|
||||
return (int)radius;
|
||||
}
|
||||
|
||||
int GetContourInternalRadius(Point[] contour)
|
||||
{
|
||||
|
||||
// Create a mask for the contour with the size of the bounding rectangle.
|
||||
Mat mask = new Mat(contoursMat.Height, contoursMat.Width, MatType.CV_8UC1, Scalar.All(0));
|
||||
|
||||
|
||||
|
||||
// Fill the mask with the polygon defined by the contour.
|
||||
Cv2.FillPoly(mask, new Point[][] { contour }, Scalar.White);
|
||||
|
||||
// Compute the distance transform.
|
||||
using Mat dist = new Mat();
|
||||
Cv2.DistanceTransform(mask, dist, DistanceTypes.C, DistanceTransformMasks.Mask3);
|
||||
Cv2.MinMaxLoc(dist, out double minVal, out var maxVal);
|
||||
|
||||
return (int)maxVal;
|
||||
}
|
||||
|
||||
(int width, int height) GetContourDimensions(Point[] contour)
|
||||
{
|
||||
var rect = Cv2.BoundingRect(contour);
|
||||
return (rect.Width, rect.Height);
|
||||
}
|
||||
|
||||
var contourMatches = contours.Select(c =>
|
||||
{
|
||||
int area = (int)Cv2.ContourArea(c);
|
||||
int radius =-1;
|
||||
radius = GetContourRadius(c);
|
||||
int innerCircle=-1;
|
||||
if (CheckInternalRadius)
|
||||
innerCircle = GetContourInternalRadius(c);
|
||||
var (width, height) = GetContourDimensions(c);
|
||||
return new ContourMatch(c, area, radius, innerCircle, width, height);
|
||||
})
|
||||
.OrderBy(cm => Math.Min(Math.Abs(cm.area - MinArea), Math.Abs(cm.area - MaxArea)))
|
||||
.ToList();
|
||||
|
||||
var matches = contourMatches.Where(cm => cm.c.Length > 1)
|
||||
.Where(cm => cm.area >= MinArea && cm.area <= MaxArea)
|
||||
.Where(cm => cm.radius >= MinRadius && cm.radius <= MaxRadius);
|
||||
|
||||
|
||||
|
||||
if (CheckInternalRadius)
|
||||
{
|
||||
matches = matches.Where(cm => cm.internalRadius >= MinInternalRadius && cm.internalRadius <= MaxInternalRadius);
|
||||
}
|
||||
|
||||
if (CheckWidth)
|
||||
{
|
||||
matches = matches.Where(cm => cm.width >= MinWidth && cm.width <= MaxWidth);
|
||||
}
|
||||
|
||||
if (CheckHeight)
|
||||
{
|
||||
matches = matches.Where(cm => cm.height >= MinHeight && cm.height <= MaxHeight);
|
||||
}
|
||||
|
||||
var finalMatches = matches.ToList();
|
||||
|
||||
int badCount = 0;
|
||||
foreach (var contourMatch in finalMatches)
|
||||
{
|
||||
PolyElement poly = new PolyElement() { IsGood = false };
|
||||
poly.Points = contourMatch.c.Select(p => new Vector2(p.X, p.Y)).ToArray();
|
||||
context.GraphicsElements.Add(poly);
|
||||
badCount++;
|
||||
}
|
||||
|
||||
if (badCount > 0)
|
||||
{
|
||||
Status = $"Found: {badCount} First: {finalMatches.First()}";
|
||||
Result = false;
|
||||
SearchArea.IsGood = false;
|
||||
return;
|
||||
}
|
||||
|
||||
SearchArea.IsGood = true;
|
||||
Status = $"Best: {contourMatches.First()}";
|
||||
Result = true;
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(Id.ToString());
|
||||
bw.Write(Label);
|
||||
bw.Write(MinArea);
|
||||
bw.Write(MaxArea);
|
||||
bw.Write(MinRadius);
|
||||
bw.Write(MaxRadius);
|
||||
bw.Write(CheckInternalRadius);
|
||||
bw.Write(MinInternalRadius);
|
||||
bw.Write(MaxInternalRadius);
|
||||
bw.Write(CheckWidth);
|
||||
bw.Write(MinWidth);
|
||||
bw.Write(MaxWidth);
|
||||
bw.Write(CheckHeight);
|
||||
bw.Write(MinHeight);
|
||||
bw.Write(MaxHeight);
|
||||
bw.Write(ReferenceId.ToString());
|
||||
SearchArea.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
Id = new Guid(br.ReadString());
|
||||
Label = br.ReadString();
|
||||
MinArea = br.ReadInt32();
|
||||
MaxArea = br.ReadInt32();
|
||||
MinRadius = br.ReadInt32();
|
||||
MaxRadius = br.ReadInt32();
|
||||
CheckInternalRadius = br.ReadBoolean();
|
||||
MinInternalRadius = br.ReadInt32();
|
||||
MaxInternalRadius = br.ReadInt32();
|
||||
CheckWidth = br.ReadBoolean();
|
||||
MinWidth = br.ReadInt32();
|
||||
MaxWidth = br.ReadInt32();
|
||||
CheckHeight = br.ReadBoolean();
|
||||
MinHeight = br.ReadInt32();
|
||||
MaxHeight = br.ReadInt32();
|
||||
ReferenceId = new Guid(br.ReadString());
|
||||
SearchArea.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Poly;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
using Point = OpenCvSharp.Point;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
|
||||
[Category("Simple/Blobs")]
|
||||
public class FindManyBlobsOperation:BaseOperation
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
|
||||
public int MinArea { get; set; } = 100;
|
||||
|
||||
public int MaxArea { get; set; } = 99999;
|
||||
|
||||
public int MinRadius { get; set; } = 0;
|
||||
|
||||
public int MaxRadius { get; set; } = 99999;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Guid ReferenceId { get; set; } = new Guid();
|
||||
|
||||
public RectangleElement SearchArea { get; set; }
|
||||
|
||||
//processing result of current element
|
||||
|
||||
|
||||
public FindManyBlobsOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.One * 100,
|
||||
Size = Vector2.One * 100,
|
||||
IsGood = true
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public record ContourMatch(Point[] c, int area, int radius)
|
||||
{
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{{area={area}, radius={radius}}}";
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
Result = true;
|
||||
if (!CheckImageExists(context)) return;
|
||||
if (!CheckGrayscale(context)) return;
|
||||
|
||||
if (SearchArea==null)
|
||||
{
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.Zero,
|
||||
Size = new Vector2(context.ActiveImage.ImageData.Width, context.ActiveImage.ImageData.Height),
|
||||
IsGood = true
|
||||
};
|
||||
}
|
||||
|
||||
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
|
||||
var img = context.ActiveImage.ImageData;
|
||||
var mat = new Mat(img.Size(), img.Type(), Scalar.Black);
|
||||
|
||||
SearchArea.FillMat(mat);
|
||||
|
||||
var contoursMat = img.BitwiseAnd(mat).ToMat();
|
||||
|
||||
|
||||
var contours = contoursMat.Threshold(128, 255, ThresholdTypes.Binary)
|
||||
.FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxSimple);
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
//case: no blobs found
|
||||
if (contours.Length == 0)
|
||||
{
|
||||
Status = "No blobs found";
|
||||
SearchArea.IsGood = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int GetContourRadius(Point[] contour)
|
||||
{
|
||||
Cv2.MinEnclosingCircle(contour, out var center, out var radius);
|
||||
return (int)radius;
|
||||
}
|
||||
//case: blobs found but not matching criteria
|
||||
var contour = contours.Select(x => new ContourMatch(x, (int)Cv2.ContourArea(x), GetContourRadius(x)))
|
||||
.OrderBy(x => Math.Min(Math.Abs(x.area - MinArea), Math.Abs(x.area - MaxArea))).ToList();
|
||||
var match = contour.Where(x => x.c.Length > 1)
|
||||
.Where(x => x.area >= MinArea && x.area <= MaxArea)
|
||||
.Where(x => x.radius >= MinRadius && x.radius <= MaxRadius).ToList();
|
||||
|
||||
|
||||
|
||||
int badCount = 0;
|
||||
foreach (ContourMatch contourMatch in match)
|
||||
{
|
||||
PolyElement poly = new PolyElement() { IsGood = false };
|
||||
|
||||
poly.Points = contourMatch.c.Select(x => new Vector2(x.X, x.Y)).ToArray();
|
||||
|
||||
context.GraphicsElements.Add(poly);
|
||||
badCount++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (badCount > 0)
|
||||
{
|
||||
Status = $"Found: {badCount} First: {match.First().ToString()}";
|
||||
Result = false;
|
||||
SearchArea.IsGood = false;
|
||||
return;
|
||||
}
|
||||
|
||||
SearchArea.IsGood = true;
|
||||
Status = $"Best: {contour.First().ToString()}";
|
||||
Result = true;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
|
||||
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
}
|
||||
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(Id.ToString());
|
||||
bw.Write(Label);
|
||||
bw.Write(MinArea);
|
||||
bw.Write(MaxArea);
|
||||
bw.Write(MinRadius);
|
||||
bw.Write(MaxRadius);
|
||||
bw.Write(ReferenceId.ToString());
|
||||
|
||||
SearchArea.Save(bw);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
Id = new Guid(br.ReadString());
|
||||
Label = br.ReadString();
|
||||
MinArea = br.ReadInt32();
|
||||
MaxArea = br.ReadInt32();
|
||||
MinRadius = br.ReadInt32();
|
||||
MaxRadius = br.ReadInt32();
|
||||
ReferenceId = new Guid(br.ReadString());
|
||||
|
||||
SearchArea.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Rectangle;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
using System;
|
||||
using System.Security.Cryptography.Xml;
|
||||
using Point = OpenCvSharp.Point;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
[Category("Simple/Blobs")]
|
||||
public class FindRectangleOperation:BaseOperation
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
|
||||
public int MinWidth { get; set; }
|
||||
public int MaxWidth { get; set; } = 999999;
|
||||
public int MinHeight { get; set; }
|
||||
public int MaxHeight { get; set; } = 999999;
|
||||
|
||||
public Guid ReferenceId { get; set; } = new Guid();
|
||||
|
||||
public bool BadIfFound { get; set; }=false;
|
||||
|
||||
public FindRectangleOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
SearchArea = new RectangleElement()
|
||||
{
|
||||
Editable = true,
|
||||
Location = Vector2.One * 100,
|
||||
Size = Vector2.One * 100,
|
||||
IsGood = true
|
||||
};
|
||||
}
|
||||
|
||||
public RectangleElement SearchArea { get; set; }
|
||||
|
||||
//processing result of current element
|
||||
[NotForTool] public OriginElement Origin { get; private set; } = OriginElement.Default;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if(!CheckImageExists(context)) return;
|
||||
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
var image = context.ActiveImage.ImageData;
|
||||
var mat = new Mat(image.Size(), image.Type(), Scalar.Black);
|
||||
|
||||
SearchArea.FillMat(mat);
|
||||
|
||||
var contoursMat = image.BitwiseAnd(mat).ToMat();
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
// find rectangle on image
|
||||
Cv2.FindContours(contoursMat, out var contours, out _, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
|
||||
foreach (Point[] contour in contours)
|
||||
{
|
||||
var rect = Cv2.BoundingRect(contour);
|
||||
if (rect.Width >= MinWidth && rect.Width <= MaxWidth && rect.Height >= MinHeight &&
|
||||
rect.Height <= MaxHeight)
|
||||
{
|
||||
Status = "Rectangle: " + rect;
|
||||
Result = !BadIfFound;
|
||||
RectangleElement re = new RectangleElement()
|
||||
{
|
||||
Editable = false,
|
||||
IsGood = !BadIfFound,
|
||||
Location = new Vector2(rect.X, rect.Y),
|
||||
Size = new Vector2(rect.Width, rect.Height)
|
||||
};
|
||||
context.GraphicsElements.Add(re);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Status = "No rectangle found";
|
||||
Result = BadIfFound;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
|
||||
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(ReferenceId.ToString());
|
||||
bw.Write(MinWidth);
|
||||
bw.Write(MaxWidth);
|
||||
bw.Write(MinHeight);
|
||||
bw.Write(MaxHeight);
|
||||
bw.Write(BadIfFound);
|
||||
SearchArea.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
ReferenceId = new Guid(br.ReadString());
|
||||
MinWidth = br.ReadInt32();
|
||||
MaxWidth = br.ReadInt32();
|
||||
MinHeight = br.ReadInt32();
|
||||
MaxHeight = br.ReadInt32();
|
||||
BadIfFound = br.ReadBoolean();
|
||||
SearchArea.Load(br);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Align;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.Links;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations;
|
||||
|
||||
[Category("Simple/Blobs")]
|
||||
public class TwoBlobsAlign:BaseOperation,IHaveOrigin
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
|
||||
public TwoBlobsAlign(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
}
|
||||
|
||||
public Guid Reference1 { get; set; } = NoOrigin.Instance.Id;
|
||||
public Guid Reference2 { get; set; } = NoOrigin.Instance.Id;
|
||||
public int DesiredAngle { get; set; }
|
||||
|
||||
[NotForTool] public OriginElement Origin { get; private set; } = OriginElement.Default;
|
||||
private string _status;
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
var reference1 = _workflowList.GetOriginById(Reference1);
|
||||
var reference2 = _workflowList.GetOriginById(Reference2);
|
||||
if (reference1 == NoOrigin.Instance)
|
||||
{
|
||||
_status = $"{nameof(Reference1)} must be set";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
if (reference2 == NoOrigin.Instance)
|
||||
{
|
||||
_status = $"{nameof(Reference2)} must be set";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
if (!(reference1 as BaseOperation).Result)
|
||||
{
|
||||
_status = $"{nameof(Reference1)} detection failed";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
if (!(reference2 as BaseOperation).Result)
|
||||
{
|
||||
_status = $"{nameof(Reference2)} detection failed";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.ActiveImage == null)
|
||||
{
|
||||
_status = $"Image required";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var center = (reference1.Origin.Location + reference2.Origin.Location) / 2;
|
||||
|
||||
var deltaX = reference2.Origin.Location.X-reference1.Origin.Location.X;
|
||||
var deltaY = reference2.Origin.Location.Y-reference1.Origin.Location.Y;
|
||||
|
||||
var angle = ((float)Math.Atan2(deltaY, deltaX))/MathF.PI*180f;
|
||||
var deltaAngle = DesiredAngle - angle;
|
||||
var matrix=Cv2.GetRotationMatrix2D(new Point2f(center.X, center.Y), -deltaAngle, 1);
|
||||
|
||||
var destImage = new Mat(context.ActiveImage.ImageData.Size(), context.ActiveImage.ImageData.Type());
|
||||
|
||||
|
||||
|
||||
Cv2.WarpAffine(context.ActiveImage.ImageData, destImage,matrix,destImage.Size());
|
||||
|
||||
context.ActiveImage=new HawkeyeImage(){Filename = context.ActiveImage.Filename,ImageData = destImage };
|
||||
|
||||
Origin = new OriginElement() {Location = center };
|
||||
_status = center.ToString();
|
||||
Result = true;
|
||||
context.GraphicsElements.Add(new AlignElement(){Location = center});
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return _status;
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
base.Save(bw);
|
||||
bw.Write(Reference1.ToString());
|
||||
bw.Write(Reference2.ToString());
|
||||
bw.Write(DesiredAngle);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
base.Load(br);
|
||||
Reference1 = new Guid(br.ReadString());
|
||||
Reference2 = new Guid(br.ReadString());
|
||||
DesiredAngle = br.ReadInt32();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Align;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Edge;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.Links;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.PositionDetection.Edges;
|
||||
[Category("Simple/Edges")]
|
||||
public class EdgeIntersectionOperation : BaseOperation, IHaveOrigin
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
private string _status;
|
||||
public override string ToString()
|
||||
{
|
||||
return _status;
|
||||
}
|
||||
|
||||
public EdgeIntersectionOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
}
|
||||
|
||||
public Guid ReferenceEdge1 { get; set; }
|
||||
|
||||
public Guid ReferenceEdge2 { get; set; }
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
var reference1 = _workflowList.GetById(ReferenceEdge1);
|
||||
var reference2 = _workflowList.GetById(ReferenceEdge2);
|
||||
if (reference1 == NoOrigin.Instance)
|
||||
{
|
||||
_status = $"{nameof(ReferenceEdge1)} must be set";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
if (reference2 == NoOrigin.Instance)
|
||||
{
|
||||
_status = $"{nameof(ReferenceEdge2)} must be set";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
if (!(reference1 as BaseOperation).Result)
|
||||
{
|
||||
_status = $"{nameof(ReferenceEdge1)} detection failed";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
if (!(reference2 as BaseOperation).Result)
|
||||
{
|
||||
_status = $"{nameof(ReferenceEdge2)} detection failed";
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
var edge1 = reference1 as FindEdgeOperation;
|
||||
var edge2 = reference2 as FindEdgeOperation;
|
||||
|
||||
|
||||
//context.GraphicsElements.Add(new EdgeElement()
|
||||
//{
|
||||
// Location = edge1.EdgeOrigin.Location,
|
||||
// Rotation = edge1.EdgeOrigin.Rotation
|
||||
//});
|
||||
//context.GraphicsElements.Add(new EdgeElement()
|
||||
//{
|
||||
// Location = edge2.EdgeOrigin.Location,
|
||||
// Rotation = edge2.EdgeOrigin.Rotation
|
||||
//});
|
||||
|
||||
if (edge1.EdgeOrigin.Intersection(edge2.EdgeOrigin, out var x, out var y))
|
||||
{
|
||||
Result = true;
|
||||
Origin = new OriginElement()
|
||||
{
|
||||
Location = new Vector2(x, y)
|
||||
};
|
||||
_status = $"{Origin.Location}";
|
||||
context.GraphicsElements.Add(new AlignElement() { Location = Origin.Location });
|
||||
return;
|
||||
}
|
||||
_status = $"Edges are parallel";
|
||||
Result = false;
|
||||
|
||||
}
|
||||
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(Id.ToString());
|
||||
bw.Write(ReferenceEdge1.ToString());
|
||||
bw.Write(ReferenceEdge2.ToString());
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
Id = new Guid(br.ReadString());
|
||||
ReferenceEdge1 = new Guid(br.ReadString());
|
||||
ReferenceEdge2 = new Guid(br.ReadString());
|
||||
|
||||
}
|
||||
|
||||
public OriginElement Origin { get; set; } = OriginElement.Default;
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Align;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Edge;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.Origin;
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes.Elements.RotatedRectangle;
|
||||
using Hawkeye.VisionBuilder.Workflow.Links;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.PositionDetection.Edges;
|
||||
[Category("Simple/Edges")]
|
||||
public class FindEdgeOperation : BaseOperation, IHaveEdge, IHaveOrigin
|
||||
{
|
||||
private readonly WorkflowList _workflowList;
|
||||
|
||||
public Guid ReferenceId { get; set; } = new Guid();
|
||||
public EdgeOrigin EdgeOrigin { get; set; }
|
||||
public FindEdgeOperation(WorkflowList workflowList)
|
||||
{
|
||||
_workflowList = workflowList;
|
||||
SearchArea = new RotatedRectangleElement()
|
||||
{
|
||||
Location = new Vector2(100, 100),
|
||||
Editable = true,
|
||||
HalfWidth = 50,
|
||||
Height = new Vector2(0, 100),
|
||||
IsGood = true
|
||||
};
|
||||
}
|
||||
private string _status;
|
||||
public override string ToString()
|
||||
{
|
||||
return _status;
|
||||
}
|
||||
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (context.ActiveImage == null)
|
||||
{
|
||||
Result = false;
|
||||
_status = "No image provided";
|
||||
return;
|
||||
}
|
||||
|
||||
SearchArea.MovePivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
|
||||
|
||||
var img = context.ActiveImage.ImageData;
|
||||
var mask = new Mat(img.Size(), img.Type(), Scalar.Black);
|
||||
|
||||
SearchArea.FillMat(mask);
|
||||
|
||||
var edges = img.Canny(100, 200);
|
||||
var mat = edges.BitwiseAnd(mask).ToMat();
|
||||
|
||||
|
||||
mat.MinMaxLoc(out _, out var maxVal, out _, out var maxLoc);
|
||||
if (maxVal < 0.01)
|
||||
{
|
||||
_status = "No edges found";
|
||||
SearchArea.IsGood = false;
|
||||
context.GraphicsElements.Add(SearchArea);
|
||||
Result = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Cv2.FindContours(mat, out var contours, out var hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
|
||||
//var closest = contours.OrderBy(c =>
|
||||
//{
|
||||
|
||||
// return (c.Select(x => new Vector2(x.X, x.Y)).Aggregate((v1, v2) => v1 + v2) / c.Length - SearchArea.Location).Length();
|
||||
|
||||
//}).First();
|
||||
|
||||
var closest = contours.SelectMany(x => x);
|
||||
var line = Cv2.FitLine(closest, DistanceTypes.L2, 0, 0.01, 0.01);
|
||||
|
||||
|
||||
|
||||
context.GraphicsElements.Add(new EdgeElement()
|
||||
{
|
||||
Location = new Vector2((float)line.X1, (float)line.Y1),
|
||||
Rotation = (float)line.GetVectorRadian()
|
||||
});
|
||||
|
||||
EdgeOrigin = new EdgeOrigin()
|
||||
{
|
||||
Found = true,
|
||||
Location = new Vector2((float)line.X1, (float)line.Y1),
|
||||
Rotation = (float)line.GetVectorRadian()
|
||||
};
|
||||
Origin = new OriginElement()
|
||||
{
|
||||
Location = new Vector2((float)line.X1, (float)line.Y1)
|
||||
};
|
||||
context.GraphicsElements.Add(new AlignElement() { Location = new Vector2((float)line.X1, (float)line.Y1) });
|
||||
_status = EdgeOrigin.ToString();
|
||||
SearchArea.IsGood = true;
|
||||
Result = true;
|
||||
|
||||
}
|
||||
public override void SetParameters(Dictionary<string, object> parameters)
|
||||
{
|
||||
base.SetParameters(parameters);
|
||||
|
||||
SearchArea.SetPivot(_workflowList.GetOriginById(ReferenceId).Origin.Location);
|
||||
}
|
||||
public override void Save(BinaryWriter bw)
|
||||
{
|
||||
bw.Write(Id.ToString());
|
||||
bw.Write(Label);
|
||||
bw.Write(ReferenceId.ToString());
|
||||
SearchArea.Save(bw);
|
||||
}
|
||||
|
||||
public override void Load(BinaryReader br)
|
||||
{
|
||||
Id = new Guid(br.ReadString());
|
||||
Label = br.ReadString();
|
||||
ReferenceId = new Guid(br.ReadString());
|
||||
SearchArea = new RotatedRectangleElement();
|
||||
SearchArea.Load(br);
|
||||
}
|
||||
|
||||
|
||||
public RotatedRectangleElement SearchArea { get; set; }
|
||||
public OriginElement Origin { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Hawkeye.VisionBuilder.Workflow.Datatypes;
|
||||
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace Hawkeye.VisionBuilder.Workflow.Operations.Transformations;
|
||||
|
||||
[Category("Transformations")]
|
||||
public class DistanceTransformOperation:BaseOperation
|
||||
{
|
||||
protected override void InterpretInternal(Context context)
|
||||
{
|
||||
if (!CheckImageExists(context)) return;
|
||||
if(!CheckGrayscale(context))return;
|
||||
|
||||
Mat res=new Mat(context.ActiveImage.ImageData.Size(),MatType.CV_8UC1);
|
||||
var distance = context.ActiveImage.ImageData.DistanceTransform(DistanceTypes.C, DistanceTransformMasks.Mask3);
|
||||
|
||||
Cv2.MinMaxLoc(distance, out double minVal, out var maxVal);
|
||||
// clamp the distance transform to 0-255
|
||||
Cv2.Normalize(distance, res, 0, maxVal, NormTypes.MinMax, MatType.CV_8UC1);
|
||||
|
||||
|
||||
|
||||
context.ActiveImage = new HawkeyeImage()
|
||||
{
|
||||
ImageData = res
|
||||
};
|
||||
|
||||
Status = $"Distance transform";
|
||||
Result = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user