23 ms·
Show HN: LLM-aided OCR – Correcting Tesseract OCR errors with LLMs
Almost exactly 1 year ago, I submitted something to HN about using Llama2 (which had just come out) to improve the output of Tesseract OCR by correcting obvious OCR errors [0]. That was exciting at the time because OpenAI's API calls were still quite expensive for GPT4, and the cost of running it on a book-length PDF would just be prohibitive. In contrast, you could run Llama2 locally on a machine with just a CPU, and it would be extremely slow, but "free" if you had a spare machine lying around.
Well, it's amazing how things have changed since then. Not only have models gotten a lot better, but the latest "low tier" offerings from OpenAI (GPT4o-mini) and Anthropic (Claude3-Haiku) are incredibly cheap and incredibly fast. So cheap and fast, in fact, that you can now break the document up into little chunks and submit them to the API concurrently (where each chunk can go through a multi-stage process, in which the output of the first stage is passed into another prompt for the next stage) and assemble it all in a shockingly short amount of time, and for basically a rounding error in terms of cost.
My original project had all sorts of complex stuff for detecting hallucinations and incorrect, spurious additions to the text (like "Here is the corrected text" preambles). But the newer models are already good enough to eliminate most of that stuff. And you can get very impressive results with the multi-stage approach. In this case, the first pass asks it to correct OCR errors and to remove line breaks in the middle of a word and things like that. The next stage takes that as the input and asks the model to do things like reformat the text using markdown, to suppress page numbers and repeated page headers, etc. Anyway, I think the samples (which take less than 1-2 minutes to generate) show the power of the approach:
Original PDF:
https://github.com/Dicklesworthstone/llm_aided_ocr/blob/main/160301289-Warren-Buffett-Katharine-Graham-Letter.pdf https://github.com/Dicklesworthstone/llm_aided_ocr/blob/main...
Raw OCR Output:
https://github.com/Dicklesworthstone/llm_aided_ocr/blob/main/160301289-Warren-Buffett-Katharine-Graham-Letter__raw_ocr_output.txt https://github.com/Dicklesworthstone/llm_aided_ocr/blob/main...
LLM-Corrected Markdown Output:
https://github.com/Dicklesworthstone/llm_aided_ocr/blob/main/160301289-Warren-Buffett-Katharine-Graham-Letter_llm_corrected.md https://github.com/Dicklesworthstone/llm_aided_ocr/blob/main...
One interesting thing I found was that almost all my attempts to fix/improve things using "classical" methods like regex and other rule based things made everything worse and more brittle, and the real improvements came from adjusting the prompts to make things clearer for the model, and not asking the model to do too much in a single pass (like fixing OCR mistakes AND converting to markdown format).
Anyway, this project is very handy if you have some old scanned books you want to read from Archive.org or Google Books on a Kindle or other ereader device and want things to be re-flowable and clear. It's still not perfect, but I bet within the next year the models will improve even more that it will get closer to 100%. Hope you like it!
[0] https://news.ycombinator.com/item?id=36976333 https://news.ycombinator.com/item?id=36976333
- dr_dshiv 2y agoI use Google lens for OCR 15th century Latin books — then paste to ChatGPT and ask to correct OCR errors. Spot checking, it is very reliable. Then translation can occur
- eigenvalue 2y agoYes, the dream is to fully automate the entire pipeline, then let it loose on a massive collection of scanned manuscripts and come back in a couple days to perfect markdown formatted copies. I wish they would run my project on all the books on Archive.org because the current OCRed output is not usable generally.
- nottorp 2y agoHmm I know someone adding a nn based ocr to number plate recognition. In production. Why bring llms into this? Because all you have is a hammer?
- Zambyte 2y agoVery cool! I have a hotkey to grab a region and pipe a screenshot through tesseract and then pipe that into my clipboard. I'll have to add on to it to pipe it though Ollama too :)
- eigenvalue 2y agoCool, I know there's a little Windows Power Toy for doing something similar: https://learn.microsoft.com/en-us/windows/powertoys/text-extractor https://learn.microsoft.com/en-us/windows/powertoys/text-ext... But the built-in functionality for iOS has the best quality OCR of anything I've seem (much better than the Windows tool), and I constantly find myself screenshotting my phone screen and using that to extract the text. My project is really for longer scanned documents like old books and articles.
- f_k 2y agoWe've built an app like that but for PDF table extraction, https://table2xl.com https://table2xl.com