When I first wrote about tidy evaluation and the untangle2 function, I used restaurant menus as an example of how embedded subheaders are used to create small multiples of data (by type of menu item).
After a recent update to the tesseract optical character recognition (ocr) engine, I decided to try and digitize and parse a restaruant menu from a photo. I wanted to try with a real photo from a real menu, and I found this photo in my camera roll.
The menu is from a nice taco place in Puerto Vallarta. I cropped the photo to the beer selection. There are craft beers and commercial beers as little subsets of the menu, each with their own heading.
Code-through
Let’s ocr the text and then restructure the data.
To prepare the image for ocr, I followed some existing imagemagick tutorials, and the implementation in magick lets us chain the different operations together, making for very readable code. The code below will read the photo striaght from the web, so anyone can follow along after installing all the required packages.
The resulting image looks like this (even though we don’t need to write it to disk for the ocr process).
The image_ocr function runs the optical character recognition on the image and returns a character vector, which we can structure into a tibble.
In tibble form:
There is some minor cleanup to be done. I like this real-world example because the original photo is not the best to begin, and the text itself is a mix of English, Spanish, and made up brand names. Even so, the ocr performed really well. Once the text is clean, we can put the prices into their own variable, and then use unheadr to turn the subheaders into a tidy grouping variable
End result:
With the menu items in a tidy structure, we can now group by beer type and get summary data or find the most or least expensive options.
Let’s have a look at the results:
Average prices
Most expensive commercial beer
Least expensive (three-way tie)
Convert prices to USD
The example is a little silly because all the craft beers have the same price, but I always wanted to apply the untangle2 function to a real restaurant menu, especially a non-digitized one from an actual photo.