Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
OCR page by page, at most four Tesseract runs (#429 review)
cre8ive-marketing53f7384740ae
diff --git a/docs/ENGINES.md b/docs/ENGINES.mdindex 45e9f6f2..9f8a2a9c 100644--- a/docs/ENGINES.md+++ b/docs/ENGINES.md@@ -1469,7 +1469,9 @@ scans and pictures with Tesseract (`Ocr/TesseractConverter.cs`), in the languagebecomes a PNG first. OCR edges are never part of an ordinary route.- A searchable PDF keeps the page as it looks and puts the recognized words on it as invisible text. PDF pages thatalready have text (visible or an OCR layer) are copied as they are; only the others are rendered at 300 dpi- (PDFium) and recognized, several pages at a time with one Tesseract thread each (`OMP_THREAD_LIMIT=1`). A PDF whose+ (PDFium) and recognized page by page: a page is rendered when one of at most four Tesseract runs (fewer on PCs with+ fewer cores) is free, each with one thread (`OMP_THREAD_LIMIT=1`), and its picture is deleted once its PDF exists,+ so a long scan never has more than four pictures in the work folder. A PDF whosepages all have text is copied with a note. The new document keeps the pages and the document information, notbookmarks or form fields.- DOCX, Markdown and HWPX read the OCR layer as the page's text (`PdfDocumentReader.ReadWithOcrLayer`): the wordsdiff --git a/src/Filee.Engines/Ocr/TesseractConverter.cs b/src/Filee.Engines/Ocr/TesseractConverter.csindex a4433eed..242fee1d 100644--- a/src/Filee.Engines/Ocr/TesseractConverter.cs+++ b/src/Filee.Engines/Ocr/TesseractConverter.cs@@ -5,7 +5,7 @@// (PdfDocumentReader reads the layer when the preset asks for OCR).//// A PDF keeps its pages that already have text (born-digital pages are never turned into pictures); only pages-// without any are rendered at 300 dpi and recognized, several at a time. The language data comes from OcrLanguages,+// without any are rendered at 300 dpi and recognized, page by page, at most four at a time. The language data comes from OcrLanguages,// downloaded the first time a language is used, and is copied into the job's folder so Tesseract reads only it.using Filee.Core.Conversion;@@ -32,6 +32,12 @@ public sealed class TesseractConverter : IConverter/// <summary>Scans are rendered at this resolution, which Tesseract reads best (and the page keeps its size).</summary>internal const int Dpi = 300;+ /// <summary>+ /// Tesseract runs side by side at most: enough to keep a desktop busy, few enough for memory and disk on a long scan+ /// (each run holds one 300 dpi page). A PC with fewer cores runs fewer.+ /// </summary>+ internal static int Runs => Math.Clamp(System.Environment.ProcessorCount, 1, 4);+/// <summary>Pictures Tesseract opens itself (Leptonica); others become a PNG first (RoutePlanner).</summary>internal static readonly IReadOnlyList<string> ImageSources = ["png", "jpg", "tiff", "bmp", "webp", "gif"];@@ -123,23 +129,48 @@ public sealed class TesseractConverter : IConverterreturn;}- // PDFium draws one page at a time; Tesseract then reads them side by side.+ // Page by page: PDFium draws a page only when one of the Tesseract runs is free, and a page's picture is deleted+ // as soon as its PDF exists, so a 300-page scan never has more than Runs pictures in the work folder.var options = new RenderOptions(Dpi: Dpi, WithAnnotations: true, WithFormFill: true, BackgroundColor: SKColors.White);- for (var i = 0; i < scans.Count; i++)- {- cancellationToken.ThrowIfCancellationRequested();- using var bitmap = Conversion.ToImage(bytes, scans[i], null, options);- await using var file = File.Create(Path.Combine(scratch, $"p{scans[i]}.png"));- bitmap.Encode(file, SKEncodedImageFormat.Png, 100);- progress?.Report(0.05 + 0.25 * (i + 1) / scans.Count);- }+ using var free = new SemaphoreSlim(Runs);+ var runs = new List<Task>();var done = 0;- await Parallel.ForEachAsync(scans, new ParallelOptions { MaxDegreeOfParallelism = System.Environment.ProcessorCount, CancellationToken = cancellationToken },- async (page, ct) =>+ foreach (var page in scans)+ {+ await free.WaitAsync(cancellationToken);+ if (runs.Any(r => r.IsFaulted))+ {+ free.Release();+ break; // a page failed: stop rendering, the error comes out below+ }+ var picture = Path.Combine(scratch, $"p{page}.png");+ try{- await ocr.ToPdfAsync(Path.Combine(scratch, $"p{page}.png"), Path.Combine(scratch, $"p{page}"), Dpi, ct);- progress?.Report(0.3 + 0.65 * Interlocked.Increment(ref done) / scans.Count);- });+ using var bitmap = Conversion.ToImage(bytes, page, null, options);+ await using var file = File.Create(picture);+ bitmap.Encode(file, SKEncodedImageFormat.Png, 100);+ }+ catch+ {+ free.Release();+ await Task.WhenAll(runs).ContinueWith(_ => { }, TaskScheduler.Default); // let the running pages end first+ throw;+ }+ runs.Add(Task.Run(async () =>+ {+ try+ {+ await ocr.ToPdfAsync(picture, Path.Combine(scratch, $"p{page}"), Dpi, cancellationToken);+ File.Delete(picture);+ progress?.Report(0.05 + 0.9 * Interlocked.Increment(ref done) / scans.Count);+ }+ finally+ {+ free.Release();+ }+ }, cancellationToken));+ }+ await Task.WhenAll(runs);// A new document: the pages and the document information come along, bookmarks and form fields don't.using var source = PdfSharpConverter.Open(step.InputPath, PdfDocumentOpenMode.Import);diff --git a/tests/Filee.Engines.Tests/OcrTests.cs b/tests/Filee.Engines.Tests/OcrTests.csindex 7003773d..66aeadb5 100644--- a/tests/Filee.Engines.Tests/OcrTests.cs+++ b/tests/Filee.Engines.Tests/OcrTests.cs@@ -136,6 +136,24 @@ public class OcrTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Contains("picture", PageText(output, 2));}+ [Fact]+ public async Task A_long_scan_is_recognized_page_by_page_in_order()+ {+ Assert.SkipUnless(Installed, Skip);+ Assert.InRange(TesseractConverter.Runs, 1, 4);+ var folder = fx.NewFolder();+ var words = new[] { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf" };+ var scans = words.Select((w, i) => Scan(folder, $"p{i}.png", $"Page {w}")).ToList();+ var pdf = (await fx.ConvertAsync(scans, new Preset { TargetFormat = "pdf", Pdf = { MergeIntoSingle = true } })).Outputs.Single();++ var job = await fx.ConvertAsync([pdf], Ocr("pdf"));++ AssertDone(job);+ var output = job.Outputs.Single();+ for (var i = 0; i < words.Length; i++)+ Assert.Contains(words[i], PageText(output, i + 1));+ }+[Fact]public async Task A_pdf_that_has_text_everywhere_is_copied_with_a_note(){