Cleaned the code a little and the character recognition is now even better

This commit is contained in:
2023-04-05 15:36:28 +02:00
parent 341b9eb0a1
commit 41f7913b80
3 changed files with 94 additions and 150 deletions
+11 -94
View File
@@ -18,108 +18,25 @@ namespace OCR_Decode
public override object DecodePng(List<string> DriverList)
{
string result = "";
int[] recommendedTresholds = new int[] { 100, 125, 150, 175, 200 };
result = GetStringFromPng();
int index = 0;
while (!IsADriver(DriverList, result))
if (!IsADriver(DriverList, result))
{
if(index >= recommendedTresholds.Length -1)
{
//150 is usually the safest bet
result = GetStringFromPng(150);
//I put everything in uppercase to try to lower the chances of bad answers
result = FindClosestMatch(DriverList.ConvertAll(d => d.ToUpper()), result.ToUpper());
break;
}
result = GetStringFromPng(recommendedTresholds[index]);
index++;
}
//rawData.Save(Reader.DEBUG_DUMP_FOLDER + result + "_Before" + ".png");
return result;
}
private string GetStringFromPng(int BlackAndWhiteTresholh)
{
string result = "";
Bitmap rawData = WindowImage;
rawData = Window.ConvertToBlackAndWhite(rawData, 150);
TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage));
Page page = engine.Process(tessImage);
Graphics g = Graphics.FromImage(rawData);
// Get the iterator for the page layout
using (var iter = page.GetIterator())
{
// Loop over the elements of the page layout
iter.Begin();
do
{
Rect boundingBox;
// Get the bounding box for the current element
if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
{
g.DrawRectangle(Pens.Red, new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height));
}
// Get the text for the current element
result += iter.GetText(PageIteratorLevel.Word);
} while (iter.Next(PageIteratorLevel.Word));
//I put everything in uppercase to try to lower the chances of bad answers
result = FindClosestMatch(DriverList.ConvertAll(d => d.ToUpper()), result.ToUpper());
}
return result;
}
//This method has been gnerated using ChatGPT
public static string FindClosestMatch(List<string> array, string target)
private static bool IsADriver(List<string> drivers, string potentialDriver)
{
var closestMatch = "";
var closestDistance = int.MaxValue;
foreach (var item in array)
bool result = false;
//I cant use drivers.Contains because it has missmatched cases and all
foreach (string name in drivers)
{
var distance = LevenshteinDistance(item, target);
if (distance < closestDistance)
{
closestMatch = item;
closestDistance = distance;
}
if (name.ToUpper() == potentialDriver.ToUpper())
result = true;
}
return closestMatch;
}
public static int LevenshteinDistance(string s1, string s2)
{
if (string.IsNullOrEmpty(s1))
{
return string.IsNullOrEmpty(s2) ? 0 : s2.Length;
}
if (string.IsNullOrEmpty(s2))
{
return string.IsNullOrEmpty(s1) ? 0 : s1.Length;
}
var d = new int[s1.Length + 1, s2.Length + 1];
for (var i = 0; i <= s1.Length; i++)
{
d[i, 0] = i;
}
for (var j = 0; j <= s2.Length; j++)
{
d[0, j] = j;
}
for (var i = 1; i <= s1.Length; i++)
{
for (var j = 1; j <= s2.Length; j++)
{
var cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
}
}
return d[s1.Length, s2.Length];
return result;
}
}
}