DEP08SIASM - Testing EDA
This commit is contained in:
@ -23,9 +23,15 @@ public class ProcessData : IProcessData
|
||||
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 Recipe { get; set; }
|
||||
|
||||
List<object> Shared.Properties.IProcessData.Details => _Details;
|
||||
|
||||
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string tessDataDirectory, int startX, int startY, int endX, int endY, List<(string, Color[])> colorCollections)
|
||||
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, int startX, int startY, int endX, int endY, List<(string, Color[])> colorCollections, List<int> previousTotalDeltaCollection, string lastText)
|
||||
{
|
||||
if (logistics is null)
|
||||
{ }
|
||||
@ -34,7 +40,7 @@ public class ProcessData : IProcessData
|
||||
_Details = new List<object>();
|
||||
MesEntity = logistics.MesEntity;
|
||||
_Log = LogManager.GetLogger(typeof(ProcessData));
|
||||
Parse(fileRead, fileInfoCollection, tessDataDirectory, startX, startY, endX, endY, colorCollections);
|
||||
Parse(fileRead, fileInfoCollection, startX, startY, endX, endY, colorCollections, previousTotalDeltaCollection, lastText);
|
||||
}
|
||||
|
||||
private static string Get(string value, bool useSplitForMID)
|
||||
@ -133,16 +139,17 @@ public class ProcessData : IProcessData
|
||||
return imageFormat;
|
||||
}
|
||||
|
||||
private static (Color[], int, int, MemoryStream) Get(string reportFullPath, string extension, int startX, int startY, int endX, int endY)
|
||||
private static (Color[], int, int) Get(IFileRead fileRead, List<FileInfo> fileInfoCollection, string extension, int startX, int startY, int endX, int endY)
|
||||
{
|
||||
Color color;
|
||||
List<Color> colors = new();
|
||||
MemoryStream memoryStream = new();
|
||||
Bitmap selectedBitmap = new(endX - startX, endY - startY);
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = Get(extension);
|
||||
using Bitmap? bitmap = Image.FromFile(reportFullPath) as Bitmap;
|
||||
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 <{reportFullPath}>");
|
||||
throw new Exception($"Couldn't load image from <{fileRead.ReportFullPath}>");
|
||||
for (int x = startX; x < endX; x++)
|
||||
{
|
||||
for (int y = startY; y < endY; y++)
|
||||
@ -153,16 +160,21 @@ public class ProcessData : IProcessData
|
||||
}
|
||||
}
|
||||
selectedBitmap.Save(memoryStream, imageFormat);
|
||||
return new(colors.ToArray(), endX - startX, endY - startY, memoryStream);
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||
SaveToFile(extension, saveFileName, memoryStream);
|
||||
}
|
||||
return new(colors.ToArray(), endX - startX, endY - startY);
|
||||
}
|
||||
|
||||
private static string Get(string saveFileName, string extension, string extra)
|
||||
private static string Get(IFileRead fileRead, string extension, string extra)
|
||||
{
|
||||
string result;
|
||||
string? directoryName = Path.GetDirectoryName(saveFileName);
|
||||
string? directoryName = Path.GetDirectoryName(fileRead.ReportFullPath);
|
||||
if (string.IsNullOrEmpty(directoryName))
|
||||
throw new Exception("Couldn't get directoryName!");
|
||||
string? fileNameWithoutExtension = Path.GetFileNameWithoutExtension(saveFileName);
|
||||
string? fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileRead.ReportFullPath);
|
||||
if (string.IsNullOrEmpty(fileNameWithoutExtension))
|
||||
throw new Exception("Couldn't get fileNameWithoutExtension!");
|
||||
result = Path.Combine(directoryName, $"{fileNameWithoutExtension} - {extra}{extension}");
|
||||
@ -185,45 +197,17 @@ public class ProcessData : IProcessData
|
||||
bitmap.Save(saveFileName, imageFormat);
|
||||
}
|
||||
|
||||
private static byte[] Get(bool development, string extension, string saveFileName, Color[] sourceColors, int width, int height, int thresHold, bool saveToFile)
|
||||
{
|
||||
int i = 0;
|
||||
Color color;
|
||||
MemoryStream memoryStream = new();
|
||||
Bitmap selectedBitmap = new(width, height);
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = Get(extension);
|
||||
string newSaveFileName = Get(saveFileName, extension, thresHold.ToString("000"));
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
color = sourceColors[i];
|
||||
if (color.R > thresHold || color.G > thresHold || color.B > thresHold)
|
||||
selectedBitmap.SetPixel(x, y, Color.Black);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
selectedBitmap.Save(memoryStream, imageFormat);
|
||||
if (development && saveToFile)
|
||||
SaveToFile(extension, newSaveFileName, memoryStream);
|
||||
byte[] bytes = memoryStream.GetBuffer();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
#pragma warning restore CA1416
|
||||
|
||||
private static void SaveText(int red, int green, string text, string textFileName, List<(string File, int TotalDelta)> totalDeltaCollection, int readAt, string reading, int score)
|
||||
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), text };
|
||||
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));
|
||||
}
|
||||
lines.Add(readAt.ToString(format));
|
||||
lines.Add(reading);
|
||||
lines.Add(score.ToString(format));
|
||||
File.WriteAllLines(textFileName, lines);
|
||||
}
|
||||
|
||||
@ -236,31 +220,19 @@ public class ProcessData : IProcessData
|
||||
return results;
|
||||
}
|
||||
|
||||
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection, string tessDataDirectory, int startX, int startY, int endX, int endY, List<(string, Color[])> colorCollections)
|
||||
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection, int startX, int startY, int endX, int endY, List<(string, Color[])> colorCollections, List<int> previousTotalDeltaCollection, string lastText)
|
||||
{
|
||||
Pix pix;
|
||||
Recipe = string.Empty;
|
||||
_Log.Debug("TODO: Get recipe");
|
||||
Red = 0;
|
||||
Green = 0;
|
||||
int delta;
|
||||
Page page;
|
||||
int readAt;
|
||||
Color color;
|
||||
int red = 0;
|
||||
string text;
|
||||
byte[] bytes;
|
||||
int green = 0;
|
||||
int totalDelta;
|
||||
string textFileName;
|
||||
string closestMatchFile;
|
||||
const int thresHold = 70;
|
||||
string closestMatchFileName;
|
||||
List<string> readings = new();
|
||||
const int upperThresHold = 153;
|
||||
Regex regex = new(@"[^a-zA-Z]");
|
||||
const string extension = ".tiff";
|
||||
string[] closestMatchFileNameSplit;
|
||||
char[] closestMatchFileNameDistinct;
|
||||
List<(string File, int TotalDelta)> totalDeltaCollection = new();
|
||||
string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, extension);
|
||||
(Color[] sourceColors, int width, int height, MemoryStream memoryStream) = Get(fileRead.ReportFullPath, extension, startX, startY, endX, endY);
|
||||
(Color[] sourceColors, int width, int height) = Get(fileRead, fileInfoCollection, extension, startX, startY, endX, endY);
|
||||
foreach ((string file, Color[] colors) in colorCollections)
|
||||
{
|
||||
totalDelta = 0;
|
||||
@ -270,9 +242,9 @@ public class ProcessData : IProcessData
|
||||
{
|
||||
color = sourceColors[i];
|
||||
if (color.R > thresHold)
|
||||
red += 1;
|
||||
Red += 1;
|
||||
if (color.G > thresHold)
|
||||
green += 1;
|
||||
Green += 1;
|
||||
delta = color.R - colors[i].R;
|
||||
if (delta > 0)
|
||||
totalDelta += delta;
|
||||
@ -283,100 +255,40 @@ public class ProcessData : IProcessData
|
||||
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();
|
||||
bytes = memoryStream.GetBuffer();
|
||||
closestMatchFile = totalDeltaCollection[0].File;
|
||||
closestMatchFileNameSplit = Path.GetFileNameWithoutExtension(closestMatchFile).Split('-');
|
||||
closestMatchFileName = closestMatchFileNameSplit.Last().TrimStart();
|
||||
closestMatchFileNameDistinct = closestMatchFileName.Distinct().ToArray();
|
||||
using TesseractEngine tesseractEngine = new(tessDataDirectory, "eng", EngineMode.Default);
|
||||
pix = Pix.LoadTiffFromMemory(bytes);
|
||||
page = tesseractEngine.Process(pix);
|
||||
text = page.GetText().Trim();
|
||||
pix.Dispose();
|
||||
page.Dispose();
|
||||
string closestMatchFile = totalDeltaCollection[0].File;
|
||||
string[] closestMatchFileNameSplit = Path.GetFileNameWithoutExtension(closestMatchFile).Split('-');
|
||||
Text = closestMatchFileNameSplit.Last().TrimStart();
|
||||
TotalDelta = totalDeltaCollection[0].TotalDelta;
|
||||
string textFileName = Get(fileRead, ".txt", $"{TotalDelta} - {Text}");
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||
SaveToFile(extension, saveFileName, memoryStream);
|
||||
fileInfoCollection.Add(new FileInfo(textFileName));
|
||||
SaveText(Red, Green, Text, textFileName, totalDeltaCollection);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
if (Text != lastText && previousTotalDeltaCollection.Count > 50)
|
||||
{
|
||||
text = regex.Replace(text, string.Empty);
|
||||
readings.Add(text);
|
||||
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;
|
||||
if (TotalDelta > upper)
|
||||
throw new Exception();
|
||||
if (TotalDelta < lower)
|
||||
throw new Exception();
|
||||
}
|
||||
if (text == closestMatchFileName)
|
||||
{
|
||||
readAt = thresHold;
|
||||
_Log.Info(text);
|
||||
textFileName = Get(saveFileName, ".txt", $"{thresHold:000} - {text}");
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
fileInfoCollection.Add(new FileInfo(textFileName));
|
||||
SaveText(red, green, text, textFileName, totalDeltaCollection, readAt, text, int.MaxValue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
readAt = -1;
|
||||
memoryStream.Dispose();
|
||||
for (int i = thresHold; i < upperThresHold; i += 10)
|
||||
{
|
||||
bytes = Get(!fileRead.IsEAFHosted, extension, saveFileName, sourceColors, width, height, i, i == thresHold);
|
||||
pix = Pix.LoadTiffFromMemory(bytes);
|
||||
page = tesseractEngine.Process(pix);
|
||||
text = page.GetText().Trim();
|
||||
pix.Dispose();
|
||||
page.Dispose();
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
text = regex.Replace(text, string.Empty);
|
||||
readings.Add(text);
|
||||
}
|
||||
if (text == closestMatchFileName)
|
||||
{
|
||||
readAt = i;
|
||||
_Log.Info(text);
|
||||
textFileName = Get(saveFileName, ".txt", $"{i:000} - {text}");
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
fileInfoCollection.Add(new FileInfo(textFileName));
|
||||
SaveText(red, green, text, textFileName, totalDeltaCollection, readAt, text, int.MaxValue);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (readAt < thresHold)
|
||||
{
|
||||
int score;
|
||||
char[] readingDistinct;
|
||||
text = closestMatchFileName;
|
||||
List<(string Reading, int Score)> readingEvaluations = new();
|
||||
foreach (string reading in readings)
|
||||
{
|
||||
score = 0;
|
||||
readingDistinct = reading.Distinct().ToArray();
|
||||
for (int i = 0; i < closestMatchFileNameDistinct.Length; i++)
|
||||
{
|
||||
if (!readingDistinct.Contains(closestMatchFileNameDistinct[i]))
|
||||
continue;
|
||||
score += 1;
|
||||
}
|
||||
readingEvaluations.Add(new(reading, score));
|
||||
}
|
||||
readingEvaluations = (from l in readingEvaluations orderby l.Score descending select l).ToList();
|
||||
textFileName = Get(saveFileName, ".txt", $"{readAt:000} - {readingEvaluations[0].Reading} - {text}");
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
fileInfoCollection.Add(new FileInfo(textFileName));
|
||||
SaveText(red, green, text, textFileName, totalDeltaCollection, readAt, readingEvaluations[0].Reading, readingEvaluations[0].Score);
|
||||
}
|
||||
}
|
||||
_Details.Add(text);
|
||||
previousTotalDeltaCollection.Add(TotalDelta);
|
||||
_Details.Add(Text);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user