From 4dd543ad7c712bdcc492c30fa476e023215a71c7 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Tue, 27 Jan 2026 14:42:14 +0500 Subject: [PATCH] try all image orientations for tesseract ocr to improve results slightly --- Clients/GithubClient/GithubClient.csproj | 2 +- CompatBot/CompatBot.csproj | 2 +- CompatBot/Ocr/Backend/Tesseract.cs | 41 ++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Clients/GithubClient/GithubClient.csproj b/Clients/GithubClient/GithubClient.csproj index 154806aa..02444794 100644 --- a/Clients/GithubClient/GithubClient.csproj +++ b/Clients/GithubClient/GithubClient.csproj @@ -7,7 +7,7 @@ - + diff --git a/CompatBot/CompatBot.csproj b/CompatBot/CompatBot.csproj index 54402cdc..634d409c 100644 --- a/CompatBot/CompatBot.csproj +++ b/CompatBot/CompatBot.csproj @@ -72,7 +72,7 @@ - + diff --git a/CompatBot/Ocr/Backend/Tesseract.cs b/CompatBot/Ocr/Backend/Tesseract.cs index d76af7ec..22b56d91 100644 --- a/CompatBot/Ocr/Backend/Tesseract.cs +++ b/CompatBot/Ocr/Backend/Tesseract.cs @@ -9,6 +9,7 @@ namespace CompatBot.Ocr.Backend; internal class Tesseract: BackendBase { private TesseractEngine engine; + private static readonly SemaphoreSlim limiter = new(1, 1); public override string Name => "tesseract"; @@ -68,9 +69,37 @@ internal class Tesseract: BackendBase public override async Task<(string result, double confidence)> GetTextAsync(string imgUrl, CancellationToken cancellationToken) { var imgData = await HttpClient.GetByteArrayAsync(imgUrl, cancellationToken).ConfigureAwait(false); - using var img = Pix.LoadFromMemory(imgData); - using var page = engine.Process(img); - return (page.GetText() ?? "", page.GetMeanConfidence()); + var img = Pix.LoadFromMemory(imgData); + var result = new (string text, float confidence)[4]; + await limiter.WaitAsync(Config.Cts.Token).ConfigureAwait(false); + try + { + var pass = 0; + do + { + using (var page = engine.Process(img)) + result[pass] = (page.GetText() ?? "", page.GetMeanConfidence()); + if (pass < 3) + { + var img2 = img.Rotate90((int)RotationDirection.Clockwise); + img.Dispose(); + img = img2; + } + pass++; + } while (pass < 4); + var longestText = result + .Where(i => i.confidence > 0.5) + .OrderByDescending(i => i.text.Length) + .FirstOrDefault(); + if (longestText is { confidence: > 0.5f, text.Length: > 0 }) + return longestText; + else + return result.MaxBy(i => i.confidence); + } + finally + { + limiter.Release(); + } } public override void Dispose() @@ -78,4 +107,10 @@ internal class Tesseract: BackendBase base.Dispose(); engine.Dispose(); } + + private enum RotationDirection + { + Clockwise = 1, + CounterClockwise = -1, + } } \ No newline at end of file