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: a scan with a stamp is still recognized
cre8ive-marketinge77c45706c5c
diff --git a/src/Filee.Engines/Ocr/TesseractConverter.cs b/src/Filee.Engines/Ocr/TesseractConverter.csindex 242fee1d..a5c8f9f5 100644--- a/src/Filee.Engines/Ocr/TesseractConverter.cs+++ b/src/Filee.Engines/Ocr/TesseractConverter.cs@@ -17,6 +17,7 @@ using PdfSharp.Pdf;using PdfSharp.Pdf.IO;using PDFtoImage;using SkiaSharp;+using UglyToad.PdfPig.Core;using UglyToad.PdfPig.Exceptions;// PDFtoImage marks its API for the platforms PDFium ships for; Filee only targets desktop platforms, all covered.@@ -201,13 +202,17 @@ public sealed class TesseractConverter : IConverter}}- /// <summary>For each page: true when it has any text, visible or not (an OCR layer counts).</summary>+ /// <summary>+ /// For each page: true when it has text of its own. An OCR layer (invisible text) always counts. A scan with a+ /// stamp does not: a picture over most of the page whose only text is a little visible text (a page number or a+ /// Bates number the scanner software added, under <see cref="StampCoverage"/> of the page).+ /// </summary>internal static List<bool> PagesWithText(string path){try{using var pdf = UglyToad.PdfPig.PdfDocument.Open(path, new UglyToad.PdfPig.ParsingOptions { UseLenientParsing = true, SkipMissingFonts = true });- return [.. pdf.GetPages().Select(page => page.Letters.Any(l => !string.IsNullOrWhiteSpace(l.Value)))];+ return [.. pdf.GetPages().Select(HasText)];}catch (PdfDocumentEncryptedException){@@ -215,6 +220,23 @@ public sealed class TesseractConverter : IConverter}}+ /// <summary>The share of a page that visible text may cover on a scan and still be only a stamp.</summary>+ internal const double StampCoverage = 0.01;++ private static bool HasText(UglyToad.PdfPig.Content.Page page)+ {+ var letters = page.Letters.Where(l => !string.IsNullOrWhiteSpace(l.Value)).ToList();+ if (letters.Count == 0)+ return false;+ if (letters.Any(l => l.RenderingMode is TextRenderingMode.Neither or TextRenderingMode.NeitherClip))+ return true;+ var area = page.Width * page.Height;+ if (area <= 0 || letters.Sum(l => l.GlyphRectangle.Area) >= StampCoverage * area)+ return true;+ // A page drawn with little text and no picture under it (a cover, a short note) is not a scan.+ return !page.GetImages().Any(image => image.Bounds.Area >= area / 2);+ }+/// <summary>/// True when the picture says how large it is meant to be. Without that Tesseract guesses 70 dpi (a page of a/// metre and more); 300 dpi is what scanners write.diff --git a/tests/Filee.Engines.Tests/OcrTests.cs b/tests/Filee.Engines.Tests/OcrTests.csindex 66aeadb5..b8582e33 100644--- a/tests/Filee.Engines.Tests/OcrTests.cs+++ b/tests/Filee.Engines.Tests/OcrTests.cs@@ -3,6 +3,7 @@// language data they read the first time (English, German).using System.IO.Compression;+using System.Text;using Filee.Core.Conversion;using Filee.Core.Presets;using Filee.Engines.Infrastructure;@@ -43,6 +44,62 @@ public class OcrLanguageListTests}}+/// <summary>Which pages count as having text, so OCR leaves them alone (no Tesseract needed).</summary>+public class OcrPagesWithTextTests(EngineFixture fx) : IClassFixture<EngineFixture>+{+ /// <summary>+ /// A one-page A4 PDF written by hand: optionally a picture over the whole page (a scan), then lines of Helvetica+ /// text, visible or invisible (render mode 3, as an OCR layer is written).+ /// </summary>+ private string Pdf(bool scan, int lines, string text = "ABC-000123", bool invisible = false)+ {+ var content = new StringBuilder();+ if (scan)+ content.Append("q 595 0 0 842 0 0 cm /Im1 Do Q\n");+ for (var i = 0; i < lines; i++)+ content.Append($"BT /F1 10 Tf {(invisible ? "3" : "0")} Tr 40 {800 - i * 12} Td ({text}) Tj ET\n");+ string[] objects =+ [+ "<< /Type /Catalog /Pages 2 0 R >>",+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> /XObject << /Im1 6 0 R >> >> >>",+ $"<< /Length {content.Length} >>\nstream\n{content}endstream",+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",+ "<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8 /Length 1 >>\nstream\n\u00c8\nendstream",+ ];+ var pdf = new StringBuilder("%PDF-1.4\n");+ var offsets = new List<int>();+ for (var i = 0; i < objects.Length; i++)+ {+ offsets.Add(pdf.Length);+ pdf.Append($"{i + 1} 0 obj\n{objects[i]}\nendobj\n");+ }+ var xref = pdf.Length;+ pdf.Append($"xref\n0 {objects.Length + 1}\n0000000000 65535 f \n");+ foreach (var offset in offsets)+ pdf.Append($"{offset:D10} 00000 n \n");+ pdf.Append($"trailer\n<< /Size {objects.Length + 1} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n");+ var path = Path.Combine(fx.NewFolder(), "page.pdf");+ File.WriteAllBytes(path, Encoding.Latin1.GetBytes(pdf.ToString()));+ return path;+ }++ [Fact]+ public void A_scan_with_only_a_stamp_is_recognized()+ {+ Assert.Equal([false], TesseractConverter.PagesWithText(Pdf(scan: true, lines: 1)));+ Assert.Equal([false], TesseractConverter.PagesWithText(Pdf(scan: true, lines: 0)));+ }++ [Fact]+ public void An_ocr_layer_a_page_of_text_and_a_drawn_note_are_left_alone()+ {+ Assert.Equal([true], TesseractConverter.PagesWithText(Pdf(scan: true, lines: 1, invisible: true)));+ Assert.Equal([true], TesseractConverter.PagesWithText(Pdf(scan: true, lines: 40, text: "The quick brown fox jumps over the lazy dog again and again")));+ Assert.Equal([true], TesseractConverter.PagesWithText(Pdf(scan: false, lines: 1)));+ }+}+public class OcrTests(EngineFixture fx) : IClassFixture<EngineFixture>{private static readonly bool Installed = TesseractConverter.Locate() is not null;