Urgent.News

What's breaking now, across thousands of outlets.

Tech

OCR that looked like it worked

For months the OCR on this site returned a file. It took a believable four or five seconds, reported no error, and handed back a PDF of the right page count. The text layer inside it was empty. Nobody complained, because there was nothing to complain about. A searchable PDF with no searchable text looks exactly like a searchable PDF until you press Ctrl+F. It took a benchmark harness with a…

In recent months, an online OCR service appeared to function correctly. When users submitted scanned documents, the system took a tolerable amount of time to process them, reported no errors, and returned a PDF with the correct page count. However, the text layer within the output PDFs was empty, making the search functionality unresponsive. It took a benchmark harness with a ground-truth word list to identify the issue, which was a single number indicating the extent of the problem: 0% word recall.

There were five underlying faults that contributed to this broken OCR system, each of which obscured the next one. First, a crash occurred only when processing high-quality scans. Scans with 300 dpi resolution failed on the first page, while scans with 150 dpi resolution completed successfully. This was counterintuitive, as larger files were expected to be more challenging to process.

The root cause was a code path in pdf.js that only activated for large pages, which only ran in a Web Worker environment where the document object did not exist. The fix involved replacing the DOMCanvasFactory with an OffscreenCanvas implementation.

Secondly, even after fixing the crash, the 300 dpi scans still produced empty text layers. The code attempted to read the recognised words from result.data.words, but this field no longer existed in tesseract.js v7. Instead, the words were located at data.blocks[].paragraphs[].lines[].words[]. The guard around the code that drew the invisible text layer checked if result.words.length was greater than 0. Since result.words was undefined, the guard never fired, resulting in no text layer being drawn and no error raised.

Thirdly, the data.blocks field existed in the result object, but its value was null. This field was off by default in tesseract.js and required explicit specification to be enabled. When blocks were requested, the code returned null for languages that were not supported, such as Polish, Russian, Japanese, Chinese, Arabic, and Hindi. The presence of this null value served as a normal response for a normal question, leading users to believe that the OCR was functioning correctly.

Fourthly, the system lost polish and support for several languages. With words being drawn onto the PDF, page.drawText() attempted to write them using a standard font with WinAnsi encoding. This encoding could not represent most characters from the language dropdown, such as ąćęłńóśźż, leaving only ó as the only supported character.

For other languages like Russian, Japanese, Chinese, Arabic, and Hindi, every word threw an encoding exception, causing no text layer to be generated for these languages. The encoding exceptions were caught and ignored, resulting in clean and wordless pages.

Lastly, the error message blamed the user for any issues with reading the output. The error handler classified any message containing the word "read" as a file problem, even though the actual issue was within the OCR engine. The message reported by the system was: "Cannot read properties of undefined," misleading users into thinking that their PDF was damaged. This outcome resulted from a bug in the code that produced a plausible success message, masking the underlying problem with the OCR system.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Govt Sees Petrol Differently Than Public

We’re witnessing a unique war of words over how to explain Pakistan’s economy. Like oxygen, the debate over fuel prices … Read More The post Govt Sees Petrol Differently Than Public appeared first on…

More from Monday 21 September →