Infineon.EAF.Runtime v2.59.0
With .gitignore *.traineddata
This commit is contained in:
229
Adaptation/FileHandlers/jpeg/Description.cs
Normal file
229
Adaptation/FileHandlers/jpeg/Description.cs
Normal file
@ -0,0 +1,229 @@
|
||||
using Adaptation.Shared;
|
||||
using Adaptation.Shared.Methods;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Adaptation.FileHandlers.jpeg;
|
||||
|
||||
public class Description : IDescription, Shared.Properties.IDescription
|
||||
{
|
||||
|
||||
public int Test { get; set; }
|
||||
public int Count { get; set; }
|
||||
public int Index { get; set; }
|
||||
//
|
||||
public string EventName { get; set; }
|
||||
public string NullData { get; set; }
|
||||
public string JobID { get; set; }
|
||||
public string Sequence { get; set; }
|
||||
public string MesEntity { get; set; }
|
||||
public string ReportFullPath { get; set; }
|
||||
public string ProcessJobID { get; set; }
|
||||
public string MID { get; set; }
|
||||
//
|
||||
public string Date { get; set; }
|
||||
public string Employee { get; set; }
|
||||
public string Lot { get; set; }
|
||||
public string PSN { get; set; }
|
||||
public string Reactor { get; set; }
|
||||
public string Recipe { get; set; }
|
||||
//
|
||||
public int Red { get; set; }
|
||||
public int Green { get; set; }
|
||||
public int TotalDelta { get; set; }
|
||||
public string ClosestMatchFileName { get; set; }
|
||||
|
||||
public string RDS { get; set; }
|
||||
|
||||
string IDescription.GetEventDescription() => "File Has been read and parsed";
|
||||
|
||||
List<string> IDescription.GetNames(IFileRead fileRead, Logistics logistics)
|
||||
{
|
||||
List<string> results = new();
|
||||
IDescription description = GetDefault(fileRead, logistics);
|
||||
string json = JsonSerializer.Serialize(description, description.GetType());
|
||||
object @object = JsonSerializer.Deserialize<object>(json);
|
||||
if (@object is not JsonElement jsonElement)
|
||||
throw new Exception();
|
||||
foreach (JsonProperty jsonProperty in jsonElement.EnumerateObject())
|
||||
results.Add(jsonProperty.Name);
|
||||
return results;
|
||||
}
|
||||
|
||||
List<string> IDescription.GetDetailNames()
|
||||
{
|
||||
List<string> results = new();
|
||||
return results;
|
||||
}
|
||||
|
||||
List<string> IDescription.GetHeaderNames()
|
||||
{
|
||||
List<string> results = new()
|
||||
{
|
||||
nameof(Date),
|
||||
nameof(Employee),
|
||||
nameof(Lot),
|
||||
nameof(PSN),
|
||||
nameof(Reactor),
|
||||
nameof(Recipe)
|
||||
};
|
||||
return results;
|
||||
}
|
||||
|
||||
IDescription IDescription.GetDisplayNames()
|
||||
{
|
||||
Description result = GetDisplayNames();
|
||||
return result;
|
||||
}
|
||||
|
||||
List<string> IDescription.GetParameterNames()
|
||||
{
|
||||
List<string> results = new()
|
||||
{
|
||||
nameof(Red),
|
||||
nameof(Green),
|
||||
nameof(TotalDelta),
|
||||
nameof(ClosestMatchFileName),
|
||||
};
|
||||
return results;
|
||||
}
|
||||
|
||||
JsonProperty[] IDescription.GetDefault(IFileRead fileRead, Logistics logistics)
|
||||
{
|
||||
JsonProperty[] results;
|
||||
IDescription description = GetDefault(fileRead, logistics);
|
||||
string json = JsonSerializer.Serialize(description, description.GetType());
|
||||
object @object = JsonSerializer.Deserialize<object>(json);
|
||||
results = ((JsonElement)@object).EnumerateObject().ToArray();
|
||||
return results;
|
||||
}
|
||||
|
||||
List<string> IDescription.GetPairedParameterNames()
|
||||
{
|
||||
string[] results = new string[] { "Test Name", "Test Value" };
|
||||
return results.ToList();
|
||||
}
|
||||
|
||||
List<string> IDescription.GetIgnoreParameterNames(Test test)
|
||||
{
|
||||
List<string> results = new();
|
||||
return results;
|
||||
}
|
||||
|
||||
IDescription IDescription.GetDefaultDescription(IFileRead fileRead, Logistics logistics)
|
||||
{
|
||||
Description result = GetDefault(fileRead, logistics);
|
||||
return result;
|
||||
}
|
||||
|
||||
Dictionary<string, string> IDescription.GetDisplayNamesJsonElement(IFileRead fileRead)
|
||||
{
|
||||
Dictionary<string, string> results = new();
|
||||
IDescription description = GetDisplayNames();
|
||||
string json = JsonSerializer.Serialize(description, description.GetType());
|
||||
JsonElement jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
|
||||
foreach (JsonProperty jsonProperty in jsonElement.EnumerateObject())
|
||||
{
|
||||
if (!results.ContainsKey(jsonProperty.Name))
|
||||
results.Add(jsonProperty.Name, string.Empty);
|
||||
if (jsonProperty.Value is JsonElement jsonPropertyValue)
|
||||
results[jsonProperty.Name] = jsonPropertyValue.ToString();
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
List<IDescription> IDescription.GetDescriptions(IFileRead fileRead, Logistics logistics, List<Test> tests, IProcessData iProcessData)
|
||||
{
|
||||
List<IDescription> results = new();
|
||||
|
||||
if (iProcessData is null || iProcessData.Details.Count == 0 || iProcessData is not ProcessData processData)
|
||||
results.Add(GetDefault(fileRead, logistics));
|
||||
else
|
||||
{
|
||||
string nullData;
|
||||
Description description;
|
||||
object configDataNullData = fileRead.NullData;
|
||||
if (configDataNullData is null)
|
||||
nullData = string.Empty;
|
||||
else
|
||||
nullData = configDataNullData.ToString();
|
||||
for (int i = 0; i < iProcessData.Details.Count; i++)
|
||||
{
|
||||
if (iProcessData.Details[i] is not string _)
|
||||
continue;
|
||||
description = new Description
|
||||
{
|
||||
Test = (int)tests[i],
|
||||
Count = tests.Count,
|
||||
Index = i,
|
||||
//
|
||||
EventName = fileRead.EventName,
|
||||
NullData = nullData,
|
||||
JobID = fileRead.CellInstanceName,
|
||||
Sequence = logistics.Sequence.ToString(),
|
||||
MesEntity = logistics.MesEntity,
|
||||
ReportFullPath = logistics.ReportFullPath,
|
||||
ProcessJobID = logistics.ProcessJobID,
|
||||
MID = logistics.MID,
|
||||
//
|
||||
Date = logistics.DateTimeFromSequence.ToUniversalTime().ToString("MM/dd/yyyy HH:mm:ss"),
|
||||
Employee = string.Empty,
|
||||
Lot = processData.Text,
|
||||
PSN = string.Empty,
|
||||
Reactor = logistics.ProcessJobID,
|
||||
Recipe = string.Empty,
|
||||
//
|
||||
Red = processData.Red,
|
||||
Green = processData.Green,
|
||||
TotalDelta = processData.TotalDelta,
|
||||
ClosestMatchFileName = processData.ClosestMatchFileName,
|
||||
};
|
||||
results.Add(description);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static Description GetDisplayNames()
|
||||
{
|
||||
Description result = new();
|
||||
return result;
|
||||
}
|
||||
|
||||
private Description GetDefault(IFileRead fileRead, Logistics logistics)
|
||||
{
|
||||
Description result = new()
|
||||
{
|
||||
Test = -1,
|
||||
Count = 0,
|
||||
Index = -1,
|
||||
//
|
||||
EventName = fileRead.EventName,
|
||||
NullData = fileRead.NullData,
|
||||
JobID = fileRead.CellInstanceName,
|
||||
Sequence = logistics.Sequence.ToString(),
|
||||
MesEntity = fileRead.MesEntity,
|
||||
ReportFullPath = logistics.ReportFullPath,
|
||||
ProcessJobID = logistics.ProcessJobID,
|
||||
MID = logistics.MID,
|
||||
//
|
||||
Date = nameof(Date),
|
||||
Employee = nameof(Employee),
|
||||
Lot = nameof(Lot),
|
||||
PSN = nameof(PSN),
|
||||
Reactor = nameof(Reactor),
|
||||
Recipe = nameof(Recipe),
|
||||
//
|
||||
Red = -1,
|
||||
Green = -1,
|
||||
TotalDelta = -1,
|
||||
ClosestMatchFileName = nameof(ClosestMatchFileName),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetDateFormat() => "MM/dd/yyyy hh:mm:ss tt";
|
||||
|
||||
}
|
216
Adaptation/FileHandlers/jpeg/FileRead.cs
Normal file
216
Adaptation/FileHandlers/jpeg/FileRead.cs
Normal file
@ -0,0 +1,216 @@
|
||||
using Adaptation.Eaf.Management.ConfigurationData.CellAutomation;
|
||||
using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration;
|
||||
using Adaptation.Shared;
|
||||
using Adaptation.Shared.Methods;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Adaptation.FileHandlers.jpeg;
|
||||
|
||||
public class FileRead : Shared.FileRead, IFileRead
|
||||
{
|
||||
|
||||
private long? _TickOffset;
|
||||
protected long _LastChange;
|
||||
protected string _LastText;
|
||||
protected readonly int _EndX;
|
||||
protected readonly int _EndY;
|
||||
protected readonly Size _Size;
|
||||
protected readonly int _StartX;
|
||||
protected readonly int _StartY;
|
||||
protected readonly int _OffSetX;
|
||||
protected readonly int _OffSetY;
|
||||
protected string _TessDataDirectory;
|
||||
protected List<int> _PreviousTotalDeltaCollection;
|
||||
protected readonly Dictionary<string, string> _Reactors;
|
||||
protected readonly List<Tuple<string, Color[]>> _ColorCollections;
|
||||
|
||||
public FileRead(ISMTP smtp, Dictionary<string, string> fileParameter, string cellInstanceName, int? connectionCount, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList<ModelObjectParameterDefinition> modelObjectParameters, string equipmentDictionaryName, Dictionary<string, List<long>> dummyRuns, Dictionary<long, List<string>> staticRuns, bool useCyclicalForDescription, bool isEAFHosted) :
|
||||
base(new Description(), true, smtp, fileParameter, cellInstanceName, connectionCount, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, staticRuns, useCyclicalForDescription, isEAFHosted: connectionCount is null)
|
||||
{
|
||||
_MinFileLength = 36790;
|
||||
_NullData = string.Empty;
|
||||
_LastText = string.Empty;
|
||||
_Logistics = new(this);
|
||||
if (_FileParameter is null)
|
||||
throw new Exception(cellInstanceConnectionName);
|
||||
if (_ModelObjectParameterDefinitions is null)
|
||||
throw new Exception(cellInstanceConnectionName);
|
||||
if (_IsDuplicator)
|
||||
throw new Exception(cellInstanceConnectionName);
|
||||
_LastChange = DateTime.Now.AddDays(-1).Ticks;
|
||||
_Reactors = new Dictionary<string, string>();
|
||||
string alias = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, string.Concat("CellInstance.", cellInstanceName, ".Alias"));
|
||||
_Reactors.Add(alias, alias);
|
||||
string x86 = Path.Combine(AppContext.BaseDirectory, "x86");
|
||||
if (!Directory.Exists(x86))
|
||||
_ = Directory.CreateDirectory(x86);
|
||||
_TessDataDirectory = Path.Combine(AppContext.BaseDirectory, "tessdata");
|
||||
if (!Directory.Exists(_TessDataDirectory))
|
||||
_ = Directory.CreateDirectory(_TessDataDirectory);
|
||||
string pdfttfSource = Path.Combine(AppContext.BaseDirectory, "pdf.ttf");
|
||||
string pdfttfDestination = Path.Combine(_TessDataDirectory, Path.GetFileName(pdfttfSource));
|
||||
if (File.Exists(pdfttfSource) && !File.Exists(pdfttfDestination))
|
||||
File.Copy(pdfttfSource, pdfttfDestination);
|
||||
string tesseract41dllSource = Path.Combine(AppContext.BaseDirectory, "tesseract41.dll");
|
||||
string tesseract41dllDestination = Path.Combine(x86, Path.GetFileName(tesseract41dllSource));
|
||||
if (File.Exists(tesseract41dllSource) && !File.Exists(tesseract41dllDestination))
|
||||
File.Copy(tesseract41dllSource, tesseract41dllDestination);
|
||||
string engtraineddataSource = Path.Combine(AppContext.BaseDirectory, "eng.traineddata");
|
||||
string engtraineddataDestination = Path.Combine(_TessDataDirectory, Path.GetFileName(engtraineddataSource));
|
||||
if (File.Exists(engtraineddataSource) && !File.Exists(engtraineddataDestination))
|
||||
File.Copy(engtraineddataSource, engtraineddataDestination);
|
||||
string leptonica1800dllSource = Path.Combine(AppContext.BaseDirectory, "leptonica-1.80.0.dll");
|
||||
string leptonica1800dllDestination = Path.Combine(x86, Path.GetFileName(leptonica1800dllSource));
|
||||
if (File.Exists(leptonica1800dllSource) && !File.Exists(leptonica1800dllDestination))
|
||||
File.Copy(leptonica1800dllSource, leptonica1800dllDestination);
|
||||
ModelObjectParameterDefinition[] images = GetProperties(cellInstanceConnectionName, modelObjectParameters, "Image.");
|
||||
string startX = GetPropertyValue(cellInstanceConnectionName, images, "Image.StartX");
|
||||
string startY = GetPropertyValue(cellInstanceConnectionName, images, "Image.StartY");
|
||||
string endX = GetPropertyValue(cellInstanceConnectionName, images, "Image.EndX");
|
||||
string endY = GetPropertyValue(cellInstanceConnectionName, images, "Image.EndY");
|
||||
_StartX = int.Parse(startX);
|
||||
_StartY = int.Parse(startY);
|
||||
_EndX = int.Parse(endX);
|
||||
_EndY = int.Parse(endY);
|
||||
_ColorCollections = new();
|
||||
_PreviousTotalDeltaCollection = new();
|
||||
(_OffSetX, _OffSetY) = ProcessData.GetOffSet(_FileConnectorConfiguration.SourceDirectoryCloaking);
|
||||
string masterImageDirectory = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, "Path.Memory.Master.Images");
|
||||
List<(string File, Size Size, Color[] Colors)> colorCollections = ProcessData.GetColorCollections(_StartX, _StartY, _EndX, _EndY, _OffSetX, _OffSetY, cellInstanceName, masterImageDirectory);
|
||||
int[] distinctSizes = (from l in colorCollections select l.Size.Width * l.Size.Height).Distinct().ToArray();
|
||||
if (distinctSizes.Length != 1)
|
||||
throw new Exception($"All Master Images must be the same size{Environment.NewLine}{string.Join(Environment.NewLine, distinctSizes)}");
|
||||
_Size = colorCollections[0].Size;
|
||||
_ColorCollections.AddRange(from l in colorCollections select new Tuple<string, Color[]>(l.File, l.Colors));
|
||||
}
|
||||
|
||||
void IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResults, Exception exception) => Move(extractResults);
|
||||
|
||||
void IFileRead.WaitForThread() => WaitForThread(thread: null, threadExceptions: null);
|
||||
|
||||
string IFileRead.GetEventDescription()
|
||||
{
|
||||
string result = _Description.GetEventDescription();
|
||||
return result;
|
||||
}
|
||||
|
||||
List<string> IFileRead.GetHeaderNames()
|
||||
{
|
||||
List<string> results = _Description.GetHeaderNames();
|
||||
return results;
|
||||
}
|
||||
|
||||
string[] IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResults, string to, string from, string resolvedFileLocation, Exception exception)
|
||||
{
|
||||
string[] results = Move(extractResults, to, from, resolvedFileLocation, exception);
|
||||
return results;
|
||||
}
|
||||
|
||||
JsonProperty[] IFileRead.GetDefault()
|
||||
{
|
||||
JsonProperty[] results = _Description.GetDefault(this, _Logistics);
|
||||
return results;
|
||||
}
|
||||
|
||||
Dictionary<string, string> IFileRead.GetDisplayNamesJsonElement()
|
||||
{
|
||||
Dictionary<string, string> results = _Description.GetDisplayNamesJsonElement(this);
|
||||
return results;
|
||||
}
|
||||
|
||||
List<IDescription> IFileRead.GetDescriptions(IFileRead fileRead, List<Test> tests, IProcessData processData)
|
||||
{
|
||||
List<IDescription> results = _Description.GetDescriptions(fileRead, _Logistics, tests, processData);
|
||||
return results;
|
||||
}
|
||||
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.GetExtractResult(string reportFullPath, string eventName)
|
||||
{
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
||||
if (string.IsNullOrEmpty(eventName))
|
||||
throw new Exception();
|
||||
_ReportFullPath = reportFullPath;
|
||||
DateTime dateTime = DateTime.Now;
|
||||
results = GetExtractResult(reportFullPath, dateTime);
|
||||
if (results.Item3 is null)
|
||||
results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(results.Item1, Array.Empty<Test>(), JsonSerializer.Deserialize<JsonElement[]>("[]"), results.Item4);
|
||||
if (results.Item3.Length > 0 && _IsEAFHosted)
|
||||
WritePDSF(this, results.Item3);
|
||||
UpdateLastTicksDuration(DateTime.Now.Ticks - dateTime.Ticks);
|
||||
return results;
|
||||
}
|
||||
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.ReExtract()
|
||||
{
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
||||
List<string> headerNames = _Description.GetHeaderNames();
|
||||
Dictionary<string, string> keyValuePairs = _Description.GetDisplayNamesJsonElement(this);
|
||||
results = ReExtract(this, headerNames, keyValuePairs);
|
||||
return results;
|
||||
}
|
||||
|
||||
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
|
||||
{
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> results = new(string.Empty, null, null, new List<FileInfo>());
|
||||
_TickOffset ??= new FileInfo(reportFullPath).LastWriteTime.Ticks - dateTime.Ticks;
|
||||
_Logistics = new Logistics(this, _TickOffset.Value, reportFullPath, useSplitForMID: true);
|
||||
SetFileParameterLotIDToLogisticsMID();
|
||||
if (_Logistics.FileInfo.Length < _MinFileLength)
|
||||
results.Item4.Add(_Logistics.FileInfo);
|
||||
else
|
||||
{
|
||||
IProcessData iProcessData = new ProcessData(this, _Logistics, results.Item4, _StartX, _StartY, _EndX, _EndY, _OffSetX, _OffSetY, _Size, _ColorCollections);
|
||||
if (iProcessData is not ProcessData processData)
|
||||
throw new Exception(string.Concat("A) No Data - ", dateTime.Ticks));
|
||||
string lastText = Regex.Replace(_LastText, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
|
||||
string text = Regex.Replace(processData.Text, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
|
||||
if (string.IsNullOrEmpty(lastText))
|
||||
lastText = "_";
|
||||
string mid = string.Concat(lastText, '-', text);
|
||||
SetFileParameterLotID(mid);
|
||||
string processJobID = iProcessData.GetCurrentReactor(this, _Logistics, _Reactors);
|
||||
_Logistics.Update(mid, processJobID);
|
||||
if (iProcessData.Details.Count == 0)
|
||||
throw new Exception(string.Concat("B) No Data - ", dateTime.Ticks));
|
||||
if (processData.Text == _LastText && _LastChange > DateTime.Now.AddMinutes(-30).Ticks)
|
||||
File.Delete(reportFullPath);
|
||||
else
|
||||
{
|
||||
if (processData.Text != _LastText)
|
||||
{
|
||||
if (_PreviousTotalDeltaCollection.Count > 50)
|
||||
{
|
||||
double average = _PreviousTotalDeltaCollection.Average();
|
||||
double sum = _PreviousTotalDeltaCollection.Sum(l => Math.Pow(l - average, 2));
|
||||
double standardDeviation = Math.Sqrt(sum / _PreviousTotalDeltaCollection.Count);
|
||||
double deviation = standardDeviation * 3;
|
||||
double upper = average + deviation;
|
||||
double lower = average - deviation;
|
||||
var dynamic = new { _Logistics.Sequence, average, sum, standardDeviation, upper, lower, processData.TotalDelta, processData.Text, _PreviousTotalDeltaCollection.Count };
|
||||
string message = JsonSerializer.Serialize(dynamic, new JsonSerializerOptions { WriteIndented = true });
|
||||
_Log.Debug(message);
|
||||
if (processData.TotalDelta > upper || processData.TotalDelta < lower)
|
||||
_SMTP.SendHighPriorityEmailMessage($"Exception:{_CellInstanceConnectionName}", message);
|
||||
}
|
||||
_PreviousTotalDeltaCollection.Add(processData.TotalDelta);
|
||||
if (_PreviousTotalDeltaCollection.Count > short.MaxValue)
|
||||
{
|
||||
for (int i = 1; i < 500; i++)
|
||||
_PreviousTotalDeltaCollection.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
_LastText = processData.Text;
|
||||
_LastChange = DateTime.Now.Ticks;
|
||||
results = iProcessData.GetResults(this, _Logistics, results.Item4);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
486
Adaptation/FileHandlers/jpeg/ProcessData.cs
Normal file
486
Adaptation/FileHandlers/jpeg/ProcessData.cs
Normal file
@ -0,0 +1,486 @@
|
||||
using Adaptation.Shared;
|
||||
using Adaptation.Shared.Methods;
|
||||
using log4net;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Tesseract;
|
||||
|
||||
namespace Adaptation.FileHandlers.jpeg;
|
||||
|
||||
public class ProcessData : IProcessData
|
||||
{
|
||||
|
||||
private readonly List<object> _Details;
|
||||
|
||||
private readonly ILog _Log;
|
||||
|
||||
public string JobID { get; set; }
|
||||
public string MesEntity { get; set; }
|
||||
|
||||
public int Red { get; set; }
|
||||
public int Green { get; set; }
|
||||
public string Text { get; set; }
|
||||
public int TotalDelta { get; set; }
|
||||
public string ClosestMatchFileName { get; set; }
|
||||
|
||||
List<object> Shared.Properties.IProcessData.Details => _Details;
|
||||
|
||||
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, int startX, int startY, int endX, int endY, int offSetX, int offSetY, Size size, List<Tuple<string, Color[]>> colorCollections)
|
||||
{
|
||||
JobID = logistics.JobID;
|
||||
fileInfoCollection.Clear();
|
||||
_Details = new List<object>();
|
||||
MesEntity = logistics.MesEntity;
|
||||
_Log = LogManager.GetLogger(typeof(ProcessData));
|
||||
Parse(fileRead, fileInfoCollection, startX, startY, endX, endY, offSetX, offSetY, size, colorCollections);
|
||||
}
|
||||
|
||||
internal static (int, int) GetOffSet(string sourceDirectoryCloaking)
|
||||
{
|
||||
int offSetX;
|
||||
int offSetY;
|
||||
string[] offset = sourceDirectoryCloaking.Split('x');
|
||||
if (offset.Length != 2 || !int.TryParse(offset[0], out int x) || !int.TryParse(offset[1], out int y))
|
||||
{
|
||||
offSetX = 0;
|
||||
offSetY = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
offSetX = x;
|
||||
offSetY = y;
|
||||
}
|
||||
return new(offSetX, offSetY);
|
||||
}
|
||||
|
||||
private static string Get(string value, bool useSplitForMID)
|
||||
{
|
||||
string result = value;
|
||||
if (useSplitForMID)
|
||||
{
|
||||
if (result.IndexOf(".") > -1)
|
||||
result = result.Split('.')[0].Trim();
|
||||
if (result.IndexOf("_") > -1)
|
||||
result = result.Split('_')[0].Trim();
|
||||
if (result.IndexOf("-") > -1)
|
||||
result = result.Split('-')[0].Trim();
|
||||
}
|
||||
result = string.Concat(result.Substring(0, 1).ToUpper(), result.Substring(1).ToLower());
|
||||
return result;
|
||||
}
|
||||
|
||||
string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors)
|
||||
{
|
||||
string result = string.Empty;
|
||||
string filePrefixAsMID;
|
||||
foreach (KeyValuePair<string, string> keyValuePair in reactors)
|
||||
{
|
||||
foreach (string filePrefix in keyValuePair.Value.Split('|'))
|
||||
{
|
||||
filePrefixAsMID = Get(filePrefix, useSplitForMID: true);
|
||||
if (logistics.MID.StartsWith(filePrefix) || logistics.MID.StartsWith(filePrefixAsMID))
|
||||
{
|
||||
result = keyValuePair.Key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrEmpty(result) && reactors.Count == 1)
|
||||
result = reactors.ElementAt(0).Key;
|
||||
return result;
|
||||
}
|
||||
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection)
|
||||
{
|
||||
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
||||
List<Test> tests = new();
|
||||
foreach (object item in _Details)
|
||||
tests.Add(Test.CDE);
|
||||
List<IDescription> descriptions = fileRead.GetDescriptions(fileRead, tests, this);
|
||||
if (tests.Count != descriptions.Count)
|
||||
throw new Exception();
|
||||
for (int i = 0; i < tests.Count; i++)
|
||||
{
|
||||
if (descriptions[i] is not Description description)
|
||||
throw new Exception();
|
||||
if (description.Test != (int)tests[i])
|
||||
throw new Exception();
|
||||
}
|
||||
List<Description> fileReadDescriptions = (from l in descriptions select (Description)l).ToList();
|
||||
string json = JsonSerializer.Serialize(fileReadDescriptions, fileReadDescriptions.GetType());
|
||||
JsonElement[] jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
|
||||
results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(logistics.Logistics1[0], tests.ToArray(), jsonElements, fileInfoCollection);
|
||||
return results;
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
#pragma warning disable CA1416
|
||||
|
||||
private static (Size, Color[]) Get(string reportFullPath, int startX, int startY, int endX, int endY, int offSetX, int offSetY)
|
||||
{
|
||||
Color color;
|
||||
List<Color> colors = new();
|
||||
int startXValue = startX + offSetX;
|
||||
int startYValue = startY + offSetY;
|
||||
int endXValue = endX + offSetX;
|
||||
int endYValue = endY + offSetY;
|
||||
using Bitmap? bitmap = Image.FromFile(reportFullPath) as Bitmap;
|
||||
if (bitmap is null)
|
||||
throw new Exception($"Couldn't load image from <{reportFullPath}>");
|
||||
for (int x = startXValue; x < endXValue; x++)
|
||||
{
|
||||
for (int y = startYValue; y < endYValue; y++)
|
||||
{
|
||||
color = bitmap.GetPixel(x, y);
|
||||
colors.Add(color);
|
||||
}
|
||||
}
|
||||
return new(bitmap.Size, colors.ToArray());
|
||||
}
|
||||
|
||||
private static System.Drawing.Imaging.ImageFormat Get(string extension)
|
||||
{
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = extension switch
|
||||
{
|
||||
".bmp" => System.Drawing.Imaging.ImageFormat.Bmp,
|
||||
".gif" => System.Drawing.Imaging.ImageFormat.Gif,
|
||||
".jpeg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".jpg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".png" => System.Drawing.Imaging.ImageFormat.Png,
|
||||
".tiff" => System.Drawing.Imaging.ImageFormat.Tiff,
|
||||
_ => throw new Exception("Extension not mapped"),
|
||||
};
|
||||
return imageFormat;
|
||||
}
|
||||
|
||||
private static (Color[], int, int) Get(IFileRead fileRead, List<FileInfo> fileInfoCollection, int startX, int startY, int endX, int endY, int offSetX, int offSetY, string extension, Size size)
|
||||
{
|
||||
Color color;
|
||||
List<Color> colors = new();
|
||||
MemoryStream memoryStream = new();
|
||||
int startXValue = startX + offSetX;
|
||||
int startYValue = startY + offSetY;
|
||||
int endXValue = endX + offSetX;
|
||||
int endYValue = endY + offSetY;
|
||||
Bitmap selectedBitmap = new(endXValue - startXValue, endYValue - startYValue);
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = Get(extension);
|
||||
using Bitmap? bitmap = Image.FromFile(fileRead.ReportFullPath) as Bitmap;
|
||||
string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, extension);
|
||||
if (bitmap is null)
|
||||
throw new Exception($"Couldn't load image from <{fileRead.ReportFullPath}>");
|
||||
if (!size.Equals(bitmap.Size))
|
||||
throw new Exception("Source size doesn't match master image size <http://10.95.154.28/set_output?input=0&output=0&venc_framerate=60&venc_gop=300&venc_width=640&venc_height=480&venc_bitrate=32000&http_ts_uri=/0.ts&http_flv_uri=/0.flv&rtsp_uri=/0&rtmp_enable=0&rtmp_uri=/0&rtmp_publish_uri=rtmp%3A%2F%2F192.168.1.169%2Flive%2F0&rtmp_publish_enable=0&http_ts_enable=1&http_flv_enable=1&rtsp_enable=1&venc_profile=0&http_hls_uri=/0.m3u8&http_hls_enable=0&venc_width_height_same_as_input=0&multicast_ip=238.0.0.1&multicast_port=1234&multicast_enable=0&venc_codec=265&srt_enable=0&srt_port=9000&srt_publish_enable=0&srt_publish_uri=srt%3A%2F%2F192.168.1.169%3A9000&srt_key=0123456789&srt_key_enable=0&max_qp=42&venc_rc_mode=0&ts_muxrate=0&_=1655307485097>");
|
||||
for (int x = startXValue; x < endXValue; x++)
|
||||
{
|
||||
for (int y = startYValue; y < endYValue; y++)
|
||||
{
|
||||
color = bitmap.GetPixel(x, y);
|
||||
colors.Add(color);
|
||||
selectedBitmap.SetPixel(x - startXValue, y - startYValue, color);
|
||||
}
|
||||
}
|
||||
selectedBitmap.Save(memoryStream, imageFormat);
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||
SaveToFile(extension, saveFileName, memoryStream);
|
||||
}
|
||||
return new(colors.ToArray(), endXValue - startXValue, endYValue - startYValue);
|
||||
}
|
||||
|
||||
private static string Get(IFileRead fileRead, string extension, string extra)
|
||||
{
|
||||
string result;
|
||||
string? directoryName = Path.GetDirectoryName(fileRead.ReportFullPath);
|
||||
if (string.IsNullOrEmpty(directoryName))
|
||||
throw new Exception("Couldn't get directoryName!");
|
||||
string? fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileRead.ReportFullPath);
|
||||
if (string.IsNullOrEmpty(fileNameWithoutExtension))
|
||||
throw new Exception("Couldn't get fileNameWithoutExtension!");
|
||||
result = Path.Combine(directoryName, $"{fileNameWithoutExtension} - {extra}{extension}");
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void SaveToFile(string extension, string saveFileName, MemoryStream memoryStream)
|
||||
{
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = extension switch
|
||||
{
|
||||
".bmp" => System.Drawing.Imaging.ImageFormat.Bmp,
|
||||
".gif" => System.Drawing.Imaging.ImageFormat.Gif,
|
||||
".jpeg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".jpg" => System.Drawing.Imaging.ImageFormat.Jpeg,
|
||||
".png" => System.Drawing.Imaging.ImageFormat.Png,
|
||||
".tiff" => System.Drawing.Imaging.ImageFormat.Tiff,
|
||||
_ => throw new Exception("Extension not mapped"),
|
||||
};
|
||||
using Bitmap bitmap = new(memoryStream);
|
||||
bitmap.Save(saveFileName, imageFormat);
|
||||
}
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
private static void SaveText(int red, int green, string closestMatchFileNameEnding, string textFileName, List<(string File, int TotalDelta)> totalDeltaCollection)
|
||||
{
|
||||
string format = "00000";
|
||||
List<string> lines = new() { red.ToString(format), green.ToString(format), closestMatchFileNameEnding };
|
||||
foreach ((string file, int totalDelta) in totalDeltaCollection)
|
||||
{
|
||||
lines.Add(file);
|
||||
lines.Add(totalDelta.ToString(format));
|
||||
}
|
||||
File.WriteAllLines(textFileName, lines);
|
||||
}
|
||||
|
||||
internal static List<(string, Size, Color[])> GetColorCollections(int startX, int startY, int endX, int endY, int offSetX, int offSetY, string cellInstanceName, string masterImageDirectory)
|
||||
{
|
||||
List<(string, Size, Color[])> results = new();
|
||||
(Size Size, Color[] Colors) result;
|
||||
string fileName;
|
||||
string[] files = Directory.GetFiles(masterImageDirectory, "*.jpeg", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileName = Path.GetFileName(file);
|
||||
if (!fileName.StartsWith(cellInstanceName))
|
||||
continue;
|
||||
result = Get(file, startX, startY, endX, endY, offSetX, offSetY);
|
||||
results.Add(new(file, result.Size, result.Colors));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection, int startX, int startY, int endX, int endY, int offSetX, int offSetY, Size size, List<Tuple<string, Color[]>> colorCollections)
|
||||
{
|
||||
Red = 0;
|
||||
Green = 0;
|
||||
int delta;
|
||||
Color color;
|
||||
int totalDelta;
|
||||
const int thresHold = 70;
|
||||
const string extension = ".tiff";
|
||||
List<(string File, int TotalDelta)> totalDeltaCollection = new();
|
||||
(Color[] sourceColors, int width, int height) = Get(fileRead, fileInfoCollection, startX, startY, endX, endY, offSetX, offSetY, extension, size);
|
||||
foreach ((string file, Color[] colors) in colorCollections)
|
||||
{
|
||||
totalDelta = 0;
|
||||
if (colors.Length != sourceColors.Length)
|
||||
continue;
|
||||
for (int i = 0; i < sourceColors.Length; i++)
|
||||
{
|
||||
color = sourceColors[i];
|
||||
if (color.R > thresHold)
|
||||
Red += 1;
|
||||
if (color.G > thresHold)
|
||||
Green += 1;
|
||||
delta = color.R - colors[i].R;
|
||||
if (delta > 0)
|
||||
totalDelta += delta;
|
||||
else
|
||||
totalDelta += delta * -1;
|
||||
delta = color.G - colors[i].G;
|
||||
if (delta > 0)
|
||||
totalDelta += delta;
|
||||
else
|
||||
totalDelta += delta * -1;
|
||||
delta = color.B - colors[i].B;
|
||||
if (delta > 0)
|
||||
totalDelta += delta;
|
||||
else
|
||||
totalDelta += delta * -1;
|
||||
}
|
||||
totalDeltaCollection.Add(new(file, totalDelta));
|
||||
}
|
||||
totalDeltaCollection = (from l in totalDeltaCollection orderby l.TotalDelta select l).ToList();
|
||||
ClosestMatchFileName = Path.GetFileNameWithoutExtension(totalDeltaCollection[0].File);
|
||||
string[] closestMatchFileNameSplit = ClosestMatchFileName.Split('-');
|
||||
Text = closestMatchFileNameSplit.Last().TrimStart();
|
||||
TotalDelta = totalDeltaCollection[0].TotalDelta;
|
||||
string textFileName = Get(fileRead, ".txt", $"{TotalDelta} - {Text}");
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
_Log.Debug(textFileName);
|
||||
fileInfoCollection.Add(new FileInfo(textFileName));
|
||||
SaveText(Red, Green, Text, textFileName, totalDeltaCollection);
|
||||
}
|
||||
_Details.Add(Text);
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
|
||||
internal static List<Description> GetDescriptions(JsonElement[] jsonElements)
|
||||
{
|
||||
List<Description> results = new();
|
||||
Description? description;
|
||||
JsonSerializerOptions jsonSerializerOptions = new() { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString };
|
||||
foreach (JsonElement jsonElement in jsonElements)
|
||||
{
|
||||
if (jsonElement.ValueKind != JsonValueKind.Object)
|
||||
throw new Exception();
|
||||
description = JsonSerializer.Deserialize<Description>(jsonElement.ToString(), jsonSerializerOptions);
|
||||
if (description is null)
|
||||
continue;
|
||||
results.Add(description);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
#pragma warning disable CA1416
|
||||
|
||||
private static MemoryStream GetMemoryStream(System.Drawing.Imaging.ImageFormat imageFormat, Bitmap bitmap, int startX, int startY, int endX, int endY, int offSetX, int offSetY)
|
||||
{
|
||||
Color color;
|
||||
int negitiveGrayScale;
|
||||
MemoryStream result = new();
|
||||
int startXValue = startX + offSetX;
|
||||
int startYValue = startY + offSetY;
|
||||
int endXValue = endX + offSetX;
|
||||
int endYValue = endY + offSetY;
|
||||
using Bitmap selectedBitmap = new(endXValue - startXValue, endYValue - startYValue);
|
||||
for (int x = startXValue; x < endXValue; x++)
|
||||
{
|
||||
for (int y = startYValue; y < endYValue; y++)
|
||||
{
|
||||
color = bitmap.GetPixel(x, y);
|
||||
negitiveGrayScale = 255 - (int)((color.R * 0.3) + (color.G * 0.59) + (color.B * 0.11));
|
||||
if (negitiveGrayScale < 162)
|
||||
selectedBitmap.SetPixel(x - startXValue, y - startYValue, Color.FromArgb(color.A, 0, 0, 0));
|
||||
else
|
||||
selectedBitmap.SetPixel(x - startXValue, y - startYValue, Color.FromArgb(color.A, 255, 255, 255));
|
||||
}
|
||||
}
|
||||
selectedBitmap.Save(result, imageFormat);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<MemoryStream> GetMemoryStreams(string reportFullPath, string extension)
|
||||
{
|
||||
List<MemoryStream> results = new();
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = Get(extension);
|
||||
using Bitmap? bitmap = Image.FromFile(reportFullPath) as Bitmap;
|
||||
if (bitmap is null)
|
||||
throw new Exception($"Couldn't load image from <{reportFullPath}>");
|
||||
results.Add(GetMemoryStream(imageFormat, bitmap, 6, 130, 791, 953, 0, 0));
|
||||
results.Add(GetMemoryStream(imageFormat, bitmap, 48, 795, 359, 869, 0, 0));
|
||||
results.Add(GetMemoryStream(imageFormat, bitmap, 376, 814, 430, 832, 0, 0));
|
||||
results.Add(GetMemoryStream(imageFormat, bitmap, 373, 793, 433, 874, 0, 0));
|
||||
return results;
|
||||
}
|
||||
|
||||
public static List<(float, string, List<string>)> Parse(string reportFullPath)
|
||||
{
|
||||
List<(float, string, List<string>)> results = new();
|
||||
string text;
|
||||
float pageMeanConfidence;
|
||||
MemoryStream memoryStream;
|
||||
List<string> blocks = new();
|
||||
const string extension = ".tiff";
|
||||
StringBuilder stringBuilder = new();
|
||||
using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default);
|
||||
List<MemoryStream> memoryStreams = GetMemoryStreams(reportFullPath, extension);
|
||||
for (int i = 0; i < memoryStreams.Count; i++)
|
||||
{
|
||||
_ = stringBuilder.Clear();
|
||||
memoryStream = memoryStreams[i];
|
||||
using Pix img = Pix.LoadFromMemory(memoryStream.GetBuffer());
|
||||
using Page page = engine.Process(img);
|
||||
text = page.GetText();
|
||||
pageMeanConfidence = page.GetMeanConfidence();
|
||||
using ResultIterator iter = page.GetIterator();
|
||||
iter.Begin();
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
_ = stringBuilder.Append(iter.GetText(PageIteratorLevel.Word)).Append(' ');
|
||||
if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word) && stringBuilder.Length > 0)
|
||||
{
|
||||
blocks.Add(stringBuilder.ToString());
|
||||
_ = stringBuilder.Clear();
|
||||
}
|
||||
} while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
|
||||
if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
|
||||
_ = stringBuilder.AppendLine();
|
||||
} while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
|
||||
} while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
|
||||
} while (iter.Next(PageIteratorLevel.Block));
|
||||
if (stringBuilder.Length > 0)
|
||||
blocks.Add(stringBuilder.ToString());
|
||||
results.Add(new(pageMeanConfidence, text, blocks));
|
||||
// if (!fileRead.IsEAFHosted)
|
||||
// {
|
||||
// fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||
SaveToFile(extension, $"{reportFullPath}{i}{extension}", memoryStream);
|
||||
// }
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
public static (float, string, List<string>) Parse(string reportFullPath, List<FileInfo> fileInfoCollection, int startX, int startY, int endX, int endY, int offSetX, int offSetY)
|
||||
{
|
||||
(float, string, List<string>) result;
|
||||
List<string> blocks = new();
|
||||
StringBuilder stringBuilder = new();
|
||||
using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default);
|
||||
using Pix img = Pix.LoadFromFile(reportFullPath);
|
||||
using Page page = engine.Process(img);
|
||||
string text = page.GetText();
|
||||
float pageMeanConfidence = page.GetMeanConfidence();
|
||||
using ResultIterator iter = page.GetIterator();
|
||||
iter.Begin();
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
_ = stringBuilder.Append(iter.GetText(PageIteratorLevel.Word)).Append(' ');
|
||||
if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word) && stringBuilder.Length > 0)
|
||||
{
|
||||
blocks.Add(stringBuilder.ToString());
|
||||
_ = stringBuilder.Clear();
|
||||
}
|
||||
} while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
|
||||
if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
|
||||
_ = stringBuilder.AppendLine();
|
||||
} while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
|
||||
} while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
|
||||
} while (iter.Next(PageIteratorLevel.Block));
|
||||
if (stringBuilder.Length > 0)
|
||||
blocks.Add(stringBuilder.ToString());
|
||||
result = new(pageMeanConfidence, text, blocks);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection, int startX, int startY, int endX, int endY, int offSetX, int offSetY)
|
||||
{
|
||||
(float pageMeanConfidence, string text, List<string> blocks) = Parse(fileRead.ReportFullPath, fileInfoCollection, startX, startY, endX, endY, offSetX, offSetY);
|
||||
_Log.Debug(string.Format("Mean confidence: {0}", pageMeanConfidence));
|
||||
_Log.Debug(string.Format("Text (GetText): \r\n{0}", text));
|
||||
_Log.Debug("Text (iterator):");
|
||||
if (blocks.Count == 0)
|
||||
_Details.Add(text);
|
||||
else
|
||||
{
|
||||
blocks = (from l in blocks where l.Split(':').Length == 3 select l).ToList();
|
||||
if (blocks.Count == 0)
|
||||
_Details.Add(text);
|
||||
else
|
||||
_Details.Add(blocks[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user