hawkeye camera support(not tested)
This commit is contained in:
@@ -5,6 +5,7 @@ using System.ComponentModel.Design.Serialization;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Text.Json;
|
||||
using System.Xml;
|
||||
using VisionBuilder.UI.Common.RecipeProcessing;
|
||||
using Size = OpenCvSharp.Size;
|
||||
@@ -32,6 +33,11 @@ public class WorkflowList
|
||||
long total = 0;
|
||||
foreach (BaseOperation operation in Operations)
|
||||
{
|
||||
if (!operation.Enabled)
|
||||
{
|
||||
operation.Result=true;
|
||||
continue;
|
||||
}
|
||||
if (Context.CancellationToken.IsCancellationRequested) break;
|
||||
operation.Interpret(Context);
|
||||
}
|
||||
@@ -62,6 +68,7 @@ public class WorkflowList
|
||||
UpdateConfigVariables();
|
||||
foreach (BaseOperation operation in Operations)
|
||||
{
|
||||
if(!operation.Enabled)continue;
|
||||
if(operation == op) break;
|
||||
operation.Interpret(Context);
|
||||
}
|
||||
@@ -275,7 +282,194 @@ public class WorkflowList
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, object> ConvertJsonElementsToNatives(Dictionary<string, object> dict)
|
||||
{
|
||||
var result = new Dictionary<string, object>();
|
||||
|
||||
foreach (var kvp in dict)
|
||||
{
|
||||
result[kvp.Key] = ConvertJsonElementToNative(kvp.Value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private object ConvertJsonElementToNative(object value)
|
||||
{
|
||||
if (value is JsonElement element)
|
||||
{
|
||||
return element.ValueKind switch
|
||||
{
|
||||
JsonValueKind.String => element.GetString(),
|
||||
JsonValueKind.Number => element.TryGetInt32(out var intVal) ? intVal :
|
||||
element.TryGetDouble(out var doubleVal) ? doubleVal :
|
||||
element.GetDecimal(),
|
||||
JsonValueKind.True => true,
|
||||
JsonValueKind.False => false,
|
||||
JsonValueKind.Null => null,
|
||||
JsonValueKind.Object => ConvertJsonElementsToNatives(
|
||||
JsonSerializer.Deserialize<Dictionary<string, object>>(element.GetRawText())),
|
||||
JsonValueKind.Array => element.EnumerateArray()
|
||||
.Select(x => ConvertJsonElementToNative(x)).ToArray(),
|
||||
_ => value
|
||||
};
|
||||
}
|
||||
else if (value is Dictionary<string, object> nestedDict)
|
||||
{
|
||||
return ConvertJsonElementsToNatives(nestedDict);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public void SaveJSON(string fileName)
|
||||
{
|
||||
var workflowData = new Dictionary<string, object>();
|
||||
|
||||
// Save Configuration
|
||||
var configData = new Dictionary<string, object>
|
||||
{
|
||||
["RuntimeCameraType"] = Configuration.RuntimeCameraType.ToString(),
|
||||
["DevelopmentCameraType"] = Configuration.DevelopmentCameraType.ToString(),
|
||||
["Outputs"] = Configuration.Outputs.ToString(),
|
||||
["EmulationPath"] = Configuration.EmulationPath,
|
||||
["PythonPath"] = Configuration.PythonPath,
|
||||
["ResultPin"] = Configuration.ResultPin,
|
||||
["SerialPort"] = Configuration.SerialPort,
|
||||
["Delay"] = Configuration.Delay
|
||||
};
|
||||
workflowData["Configuration"] = configData;
|
||||
|
||||
// Save Operations
|
||||
var operationsData = new List<Dictionary<string, object>>();
|
||||
foreach (BaseOperation operation in Operations)
|
||||
{
|
||||
var operationData = new Dictionary<string, object>
|
||||
{
|
||||
["Type"] = operation.GetType().AssemblyQualifiedName
|
||||
};
|
||||
|
||||
var operationDict = new Dictionary<string, object>();
|
||||
operation.Save(operationDict);
|
||||
operationData["Data"] = operationDict;
|
||||
|
||||
operationsData.Add(operationData);
|
||||
}
|
||||
workflowData["Operations"] = operationsData;
|
||||
|
||||
// Save Recipe Image
|
||||
if (RecipeImage == null)
|
||||
{
|
||||
var mat = new Mat(128, 128, MatType.CV_8UC3, Scalar.Gray);
|
||||
var image = mat.ToBytes();
|
||||
workflowData["RecipeImage"] = Convert.ToBase64String(image);
|
||||
}
|
||||
else
|
||||
{
|
||||
var mat = RecipeImage;
|
||||
var image = mat.Resize(new Size(128, 128)).ToBytes();
|
||||
workflowData["RecipeImage"] = Convert.ToBase64String(image);
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
string jsonString = JsonSerializer.Serialize(workflowData, options);
|
||||
File.WriteAllText(fileName, jsonString);
|
||||
}
|
||||
|
||||
public void LoadJSON(string fileName, OperationDiscoveryService operationDiscoveryService)
|
||||
{
|
||||
string jsonString = File.ReadAllText(fileName);
|
||||
var workflowData = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);
|
||||
|
||||
if (workflowData == null) return;
|
||||
|
||||
// Load Configuration
|
||||
if (workflowData.ContainsKey("Configuration"))
|
||||
{
|
||||
var configElement = (JsonElement)workflowData["Configuration"];
|
||||
var configData = JsonSerializer.Deserialize<Dictionary<string, object>>(configElement.GetRawText());
|
||||
|
||||
if (configData != null)
|
||||
{
|
||||
var convertedConfigData = ConvertJsonElementsToNatives(configData);
|
||||
|
||||
if (convertedConfigData.ContainsKey("RuntimeCameraType"))
|
||||
Configuration.RuntimeCameraType = Enum.Parse<ECameraType>(convertedConfigData["RuntimeCameraType"].ToString());
|
||||
if (convertedConfigData.ContainsKey("DevelopmentCameraType"))
|
||||
Configuration.DevelopmentCameraType = Enum.Parse<ECameraType>(convertedConfigData["DevelopmentCameraType"].ToString());
|
||||
if (convertedConfigData.ContainsKey("Outputs"))
|
||||
Configuration.Outputs = Enum.Parse<EOutputs>(convertedConfigData["Outputs"].ToString());
|
||||
if (convertedConfigData.ContainsKey("EmulationPath"))
|
||||
Configuration.EmulationPath = convertedConfigData["EmulationPath"].ToString();
|
||||
if (convertedConfigData.ContainsKey("PythonPath"))
|
||||
Configuration.PythonPath = convertedConfigData["PythonPath"].ToString();
|
||||
if (convertedConfigData.ContainsKey("ResultPin"))
|
||||
Configuration.ResultPin = (int)convertedConfigData["ResultPin"];
|
||||
if (convertedConfigData.ContainsKey("SerialPort"))
|
||||
Configuration.SerialPort = convertedConfigData["SerialPort"].ToString();
|
||||
if (convertedConfigData.ContainsKey("Delay"))
|
||||
Configuration.Delay = (int)convertedConfigData["Delay"];
|
||||
}
|
||||
}
|
||||
|
||||
if (!File.Exists(Configuration.PythonPath))
|
||||
PythonMissing();
|
||||
|
||||
// Load Operations
|
||||
Operations.Clear();
|
||||
if (workflowData.ContainsKey("Operations"))
|
||||
{
|
||||
var operationsElement = (JsonElement)workflowData["Operations"];
|
||||
var operationsArray = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(operationsElement.GetRawText());
|
||||
|
||||
if (operationsArray != null)
|
||||
{
|
||||
foreach (var operationData in operationsArray)
|
||||
{
|
||||
if (operationData.ContainsKey("Type") && operationData.ContainsKey("Data"))
|
||||
{
|
||||
string operationType = operationData["Type"].ToString();
|
||||
var type = Type.GetType(operationType);
|
||||
if (type != null)
|
||||
{
|
||||
BaseOperation operation = operationDiscoveryService.CreateInstance(type);
|
||||
|
||||
var dataElement = (JsonElement)operationData["Data"];
|
||||
var operationDict = JsonSerializer.Deserialize<Dictionary<string, object>>(dataElement.GetRawText());
|
||||
|
||||
if (operationDict != null)
|
||||
{
|
||||
var convertedDict = ConvertJsonElementsToNatives(operationDict);
|
||||
operation.Load(convertedDict);
|
||||
}
|
||||
|
||||
Operations.Add(operation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load Recipe Image
|
||||
if (workflowData.ContainsKey("RecipeImage"))
|
||||
{
|
||||
string base64Image = workflowData["RecipeImage"].ToString();
|
||||
if (!string.IsNullOrEmpty(base64Image))
|
||||
{
|
||||
byte[] imageData = Convert.FromBase64String(base64Image);
|
||||
RecipeImage = Mat.FromImageData(imageData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static WorkflowList LoadJSONFromFile(string fileName)
|
||||
{
|
||||
var workflowList = new WorkflowList();
|
||||
workflowList.LoadJSON(fileName, new OperationDiscoveryService(workflowList));
|
||||
return workflowList;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user