Added _ColorCollections
This commit is contained in:
@ -8,6 +8,7 @@ using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Tesseract;
|
||||
|
||||
namespace Adaptation.FileHandlers.jpeg;
|
||||
@ -24,7 +25,7 @@ public class ProcessData : IProcessData
|
||||
|
||||
List<object> Shared.Properties.IProcessData.Details => _Details;
|
||||
|
||||
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection)
|
||||
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string tessDataDirectory, int startX, int startY, int endX, int endY, List<(string, Color[])> colorCollections)
|
||||
{
|
||||
if (logistics is null)
|
||||
{ }
|
||||
@ -33,7 +34,7 @@ public class ProcessData : IProcessData
|
||||
_Details = new List<object>();
|
||||
MesEntity = logistics.MesEntity;
|
||||
_Log = LogManager.GetLogger(typeof(ProcessData));
|
||||
Parse(fileRead, fileInfoCollection);
|
||||
Parse(fileRead, fileInfoCollection, tessDataDirectory, startX, startY, endX, endY, colorCollections);
|
||||
}
|
||||
|
||||
private static string Get(string value, bool useSplitForMID)
|
||||
@ -96,33 +97,79 @@ public class ProcessData : IProcessData
|
||||
return results;
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
#pragma warning disable CA1416
|
||||
|
||||
private static (MemoryStream memoryStream, Color[]) Get(IFileRead fileRead, int thresHold, int startX, int startY, int endX, int endY)
|
||||
private static Color[] Get(string reportFullPath, int startX, int startY, int endX, int endY)
|
||||
{
|
||||
Color color;
|
||||
List<Color> colors = new();
|
||||
MemoryStream memoryStream = new();
|
||||
int middle = (int)(endY - startY * .5);
|
||||
Bitmap selectedBitmap = new(endX - startX, endY - startY);
|
||||
using Bitmap bitmap = Image.FromFile(fileRead.ReportFullPath) as Bitmap;
|
||||
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png;
|
||||
using Bitmap? bitmap = Image.FromFile(reportFullPath) as Bitmap;
|
||||
if (bitmap is null)
|
||||
throw new Exception($"Couldn't load image from <{reportFullPath}>");
|
||||
for (int x = startX; x < endX; x++)
|
||||
{
|
||||
for (int y = startY; y < endY; y++)
|
||||
{
|
||||
color = bitmap.GetPixel(x, y);
|
||||
if (y == middle)
|
||||
colors.Add(color);
|
||||
if (color.R > thresHold || color.G > thresHold || color.B > thresHold)
|
||||
selectedBitmap.SetPixel(x - startX, y - startY, Color.Black);
|
||||
colors.Add(color);
|
||||
}
|
||||
}
|
||||
return 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, MemoryStream) Get(string reportFullPath, 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;
|
||||
if (bitmap is null)
|
||||
throw new Exception($"Couldn't load image from <{reportFullPath}>");
|
||||
for (int x = startX; x < endX; x++)
|
||||
{
|
||||
for (int y = startY; y < endY; y++)
|
||||
{
|
||||
color = bitmap.GetPixel(x, y);
|
||||
colors.Add(color);
|
||||
selectedBitmap.SetPixel(x - startX, y - startY, color);
|
||||
}
|
||||
}
|
||||
selectedBitmap.Save(memoryStream, imageFormat);
|
||||
return new(memoryStream, colors.ToArray());
|
||||
return new(colors.ToArray(), endX - startX, endY - startY, memoryStream);
|
||||
}
|
||||
|
||||
private static void SaveToFile(MemoryStream memoryStream, string extension, string saveFileName)
|
||||
private static string Get(string saveFileName, string extension, string extra)
|
||||
{
|
||||
string result;
|
||||
string? directoryName = Path.GetDirectoryName(saveFileName);
|
||||
if (string.IsNullOrEmpty(directoryName))
|
||||
throw new Exception("Couldn't get directoryName!");
|
||||
string? fileNameWithoutExtension = Path.GetFileNameWithoutExtension(saveFileName);
|
||||
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
|
||||
{
|
||||
@ -138,44 +185,197 @@ 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 (MemoryStream, Color[]) Get(IFileRead fileRead, int thresHold) => Get(fileRead, thresHold, 330, 16, 400, 32);
|
||||
|
||||
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection)
|
||||
private static void SaveText(int red, int green, string text, string textFileName, List<(string File, int TotalDelta)> totalDeltaCollection, int readAt, string reading, int score)
|
||||
{
|
||||
int thresHold = 76;
|
||||
(MemoryStream memoryStream, Color[] colors) = Get(fileRead, thresHold);
|
||||
byte[] bytes = memoryStream.GetBuffer();
|
||||
using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default);
|
||||
using Pix img = Pix.LoadFromMemory(bytes);
|
||||
using Page page = engine.Process(img);
|
||||
string text = page.GetText().Trim();
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
_Log.Debug(text);
|
||||
else
|
||||
string format = "00000";
|
||||
List<string> lines = new() { red.ToString(format), green.ToString(format), text };
|
||||
foreach ((string file, int totalDelta) in totalDeltaCollection)
|
||||
{
|
||||
int red = 0;
|
||||
int green = 0;
|
||||
_Log.Debug("Looking by color");
|
||||
string extension = ".png";
|
||||
string saveFileName = Path.ChangeExtension(fileRead.ReportFullPath, extension);
|
||||
SaveToFile(memoryStream, extension, saveFileName);
|
||||
foreach (Color color in colors)
|
||||
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);
|
||||
}
|
||||
|
||||
internal static List<(string, Color[])> GetColorCollections(int startX, int startY, int endX, int endY, string masterImageDirectory)
|
||||
{
|
||||
List<(string, Color[])> results = new();
|
||||
string[] files = Directory.GetFiles(masterImageDirectory, "*.jpeg", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
results.Add(new(file, Get(file, startX, startY, endX, endY)));
|
||||
return results;
|
||||
}
|
||||
|
||||
private void Parse(IFileRead fileRead, List<FileInfo> fileInfoCollection, string tessDataDirectory, int startX, int startY, int endX, int endY, List<(string, Color[])> colorCollections)
|
||||
{
|
||||
Pix pix;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
if (red > green)
|
||||
text = "Red*";
|
||||
else
|
||||
text = "Green*";
|
||||
fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||
totalDeltaCollection.Add(new(file, totalDelta));
|
||||
}
|
||||
if (memoryStream is not null)
|
||||
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();
|
||||
if (!fileRead.IsEAFHosted)
|
||||
{
|
||||
fileInfoCollection.Add(new FileInfo(saveFileName));
|
||||
SaveToFile(extension, saveFileName, memoryStream);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
text = regex.Replace(text, string.Empty);
|
||||
readings.Add(text);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user