I'm trying to OCR a very large book: 45 volumes of ~500 pages each. The digitization has been done (not very good but not too bad either), but the pages have comments in the margin and lots of footnotes.
Just doing plain OCR doesn't really work because the notes in the margin and the footnotes get mingled with the text, which results in gibberish. But, when sent to Google Vision API, each page results in a json file that has an object for each word and the four coordinates of its bounding box. That json file is pretty big (around 1.5 Mo when pretty printed, or 500 k with no indents or line breaks) but it can then be fed to Gemini, taking advantage of its large context window. Gemini is pretty good at identifying each section of the page (headers, main text, margin comments, footnotes) but it takes a looong time to respond (2-5 minutes per page). So another approach is to ask Gemini to write a python script to analyze the json result and group sections depending of the coordinates of each word, and then run that script against the json output by the OCR phase. But it's quite difficult to have a script that works for any page; comments in the margin are always in the margin so that's pretty easy, but footnotes can start at any height of the page (some pages contain only footnotes running from previous pages) and Gemini likes to be pretty specific, giving hard 'y' coordinates for where footnotes should start, which obviously only works for the one page it's working on. I'm iterating and making some progress but I feel like I miss a big breakthrough and it all should be simpler than it currently is. Information about OCR is pretty scarce online. Any pointer is welcome!
It's an early 20th century edition of 18th century memoirs, in French. The project is not secret by any means but I'd rather not name it directly so as to not generate expectations that I may not satisfy.
Are the footnotes in a different font or fontsize? If so, then the bounding box for footnote words should be smaller. Perhaps that can help with categorization.
Yes thank you, that's one piece of information that I examined. The font size of the footnotes is a little bit smaller but the difference is very narrow. On a given word it's not really obvious enough. But maybe by calculating the average height of a whole line it would be significant.
The width of words would have larger difference than the height of characters, so use the width. I would
1. Manually categorize a few thousand words of normal and footnote text. Then solve a linear system to figure out the width of each letter in normal and footnote. Now, you are able to compute the expected width of any word in normal or footnote font. 2. Now, when you get a fresh page, go down line by line. For every word in the line, compare the actual word width with the expected normal width and expected footnote width. Whichever is closer categorize the word as that. Then for the whole line, take the majority vote on whether to categorize it as normal or footnote line. Once you hit a footnote line, you are done.
I'm experimenting with a "new" kind of lisp macros.
Macros in lisp are just normal functions that receive the code they wrap as argument and return some modified code. Typically they will just wrap the passed code into some more code. But then there are code walking macro: macros that will traverse passed code to modify it in depth. What I'm working on is "code diving" macros. Not only will they traverse the passed code, but they will resolve called functions and macros, fetch their source code and traverse it too. And so on. All the redefined fns/macros are accumulated in a let/macrolet binding, topologically sorted by call/dependency order. Instrumented code will call these local redefinitions, shadowing the global definition lexically. This allow the programmer to write truly local monkey-patches for existing code he doesn't have control over (e.g, code from another library for instance). I'm writing this in Clojure, and the traditional way to do this is to temporarily change the global definitions of targeted variables using with-redefs. This is problematic because other threads will see these redefinitions, and not just threads created within the instrumented code, but already existing threads too. Another way to do it is to just redefine the targeted functions globally, but then your modifications are available to the whole program for the rest of its execution.
THANK YOU! I've been trying to understand wtf lisp macros are for so long I'm embarrassed to say. I've heard that their "extremely powerful" and kept thinking "Macros? like in C? In Excel? WTF is the big deal about macros?"
Now I understand why I should learn lisp other than to play with its weird-but-kinda-cool-looking prefix notation.
It's a very fun mix of hardware (for data collection), and crazy SQL queries to model energy flows between buildings, solar, batteries, etc. Considering just one building is pretty easy: consumption = imported - exported + generated - stored + dispatched.
carbon = carbon intensity * imported
cost = tariff * imported but then you add a site with a couple of buildings, solar on one of them, grid limited exports, etc modelling these flows is challenging. Like consider the case where one building got 10% of it's imported power from another building's excess solar, then calculating carbon becomes more difficult. and once you've figured all that - then you have to figure out what makes commercial sense to do next.. install a battery, expand solar, move onto a TOU tariff, do nothing - and that's a whole other world of optimisation problems.
Also somewhat working in this space. Building a BMS (Building management system) to manage and control everything in commercial buildings. Think Homekit for commercial. There's something like 70% of buildings don't use one and they can be much more environmental friendly.
Very familiar with BMSs but the lack of open standards and protocols has been extremely frustrating - makes me appreciate how good we have it with HTTP, etc. Lots say they support BACnet but that’s only if they’ve been configured and the points exported, etc. Haystack is a great step forward for labelling too but adoption seems fill with complexity :)
Have had to implement the BACnet spec for scheduling, and wow, that BACNet Standards PDF is huge :'}
Haystack definitely has it's challenges. My main concern is it's not very client-side friendly when attempting to use haystack-core types. But it's a cool framework. Would love some modern toolsets in the space.
Currently, other than my day job, I am obsessed with making sense of the "shape of stories". Mapping embeddings and see how they move through a sequence.
I'm obsessed with visual space representations of word as well. My application has a test mode where I render an entire dictionary (like, Webster) and then "plays" a book word by word, highlighting the word, then it's definition, and those words definitions and so on and so on, and it creates a trippy and distinct visualization kinda like an audio visualizer.. but with words. We should chat if you're interested in this kinda thing!
https://github.com/tikimcfee/LookAtThat
Yes. We have integrated this into our RAG pipeline. Looking for a benchmark which we can try. Have been using this to give feedback to my marketing team on our blog posts.
Comments (1,152) 576 threads
Earlier commentsJust doing plain OCR doesn't really work because the notes in the margin and the footnotes get mingled with the text, which results in gibberish.
But, when sent to Google Vision API, each page results in a json file that has an object for each word and the four coordinates of its bounding box.
That json file is pretty big (around 1.5 Mo when pretty printed, or 500 k with no indents or line breaks) but it can then be fed to Gemini, taking advantage of its large context window.
Gemini is pretty good at identifying each section of the page (headers, main text, margin comments, footnotes) but it takes a looong time to respond (2-5 minutes per page).
So another approach is to ask Gemini to write a python script to analyze the json result and group sections depending of the coordinates of each word, and then run that script against the json output by the OCR phase.
But it's quite difficult to have a script that works for any page; comments in the margin are always in the margin so that's pretty easy, but footnotes can start at any height of the page (some pages contain only footnotes running from previous pages) and Gemini likes to be pretty specific, giving hard 'y' coordinates for where footnotes should start, which obviously only works for the one page it's working on.
I'm iterating and making some progress but I feel like I miss a big breakthrough and it all should be simpler than it currently is. Information about OCR is pretty scarce online. Any pointer is welcome!
1. Manually categorize a few thousand words of normal and footnote text. Then solve a linear system to figure out the width of each letter in normal and footnote. Now, you are able to compute the expected width of any word in normal or footnote font.
2. Now, when you get a fresh page, go down line by line. For every word in the line, compare the actual word width with the expected normal width and expected footnote width. Whichever is closer categorize the word as that. Then for the whole line, take the majority vote on whether to categorize it as normal or footnote line. Once you hit a footnote line, you are done.
Continue this thread — 2 replies
Macros in lisp are just normal functions that receive the code they wrap as argument and return some modified code. Typically they will just wrap the passed code into some more code. But then there are code walking macro: macros that will traverse passed code to modify it in depth.
What I'm working on is "code diving" macros. Not only will they traverse the passed code, but they will resolve called functions and macros, fetch their source code and traverse it too. And so on. All the redefined fns/macros are accumulated in a let/macrolet binding, topologically sorted by call/dependency order. Instrumented code will call these local redefinitions, shadowing the global definition lexically.
This allow the programmer to write truly local monkey-patches for existing code he doesn't have control over (e.g, code from another library for instance). I'm writing this in Clojure, and the traditional way to do this is to temporarily change the global definitions of targeted variables using with-redefs. This is problematic because other threads will see these redefinitions, and not just threads created within the instrumented code, but already existing threads too.
Another way to do it is to just redefine the targeted functions globally, but then your modifications are available to the whole program for the rest of its execution.
Now I understand why I should learn lisp other than to play with its weird-but-kinda-cool-looking prefix notation.
It's a very fun mix of hardware (for data collection), and crazy SQL queries to model energy flows between buildings, solar, batteries, etc. Considering just one building is pretty easy:
consumption = imported - exported + generated - stored + dispatched. carbon = carbon intensity * imported cost = tariff * imported
but then you add a site with a couple of buildings, solar on one of them, grid limited exports, etc modelling these flows is challenging. Like consider the case where one building got 10% of it's imported power from another building's excess solar, then calculating carbon becomes more difficult.
and once you've figured all that - then you have to figure out what makes commercial sense to do next.. install a battery, expand solar, move onto a TOU tariff, do nothing - and that's a whole other world of optimisation problems.
Utilizing https://project-haystack.org/
Very familiar with BMSs but the lack of open standards and protocols has been extremely frustrating - makes me appreciate how good we have it with HTTP, etc.
Lots say they support BACnet but that’s only if they’ve been configured and the points exported, etc.
Haystack is a great step forward for labelling too but adoption seems fill with complexity :)
Haystack definitely has it's challenges. My main concern is it's not very client-side friendly when attempting to use haystack-core types. But it's a cool framework.
Would love some modern toolsets in the space.
Started of with [1] which showed that there might be some strength to the idea.
Applied it for chunking [2] and web site analysis[3] and got pretty good results.
Just started trying out experiments on video [4] and surprised that the structure seems to hold for image embeddings as well.
I have no clue if this has any value, but it is fun to go down this rabbit hole :)
[1]https://gpt3experiments.substack.com/p/the-shape-of-stories-...
[2]https://gpt3experiments.substack.com/p/a-new-chunking-approa...
[3]https://gpt3experiments.substack.com/p/using-semantic-chunki...
[4]https://x.com/nutanc/status/1849661242027409723
Also, as I mentioned, now, trying out experiments in the video domain with some pretty cool results. Will post the details this week.
https://x.com/nutanc/status/1849661242027409723
All comments shown.
Couldn't load more comments. Check your connection and try again.