<p>Optical character recognition (OCR) is the process of extracting written or typed text from images such as photos and scanned documents into machine-encoded text. The new rOpenSci package <a href="https://cran.r-project.org/web/packages/tesseract/index.html">tesseract</a> brings one of the best open-source OCR engines to R. This enables researchers or journalists, for example, to search and analyze vast numbers of documents that are only available in printed form.</p>
<p>People looking to extract text and metadata from pdf files in R should try our <a href="https://cran.r-project.org/web/packages/pdftools/index.html">pdftools</a> package.</p>
<h2>Getting Started</h2>
<p>The package links to the libtesseract C++ library and works out of the box on Windows and Mac without installing any third party software.</p>
<div><pre><code>install.packages<span>(</span><span>&quot;tesseract&quot;</span><span>)</span>
</code></pre></div>
<p>On Linux you first need to install libtesseract (<a href="https://github.com/ropensci/tesseract#readme">readme</a>) which ships with every popular distribution (Debian, Ubuntu, Fedora, CentOS, etc).</p>
<p>The package itself is very simple. The <code>ocr</code> function takes a URL or path or raw vector with image data. On most platforms the image should either be in <code>png</code> or <code>jpeg</code> or <code>tiff</code> format.</p>
<div><pre><code><span>library</span><span>(</span>tesseract<span>)</span>
text <span>&lt;-</span> ocr<span>(</span><span>&quot;http://jeroenooms.github.io/images/testocr.png&quot;</span><span>)</span>
<span>cat</span><span>(</span>text<span>)</span>
</code></pre></div>
<p>Running this in R should recognize the text in the example image almost perfectly. The <code>ocr</code> function has one additional argument to set custom tesseract options. This is needed if you want to use custom or non-english training data (which we will explain below). The <code>?tesseract</code> manual page has more information.</p>
<h2>Why OCR is hard</h2>
<p>Finding and classifying visual patterns is incredibly difficult for computers, especially if the picture contains noise or other artifacts. Humans take advantage of prior knowledge about the language that we use to &quot;fill in the gaps&quot; when reading text. For this reason recognizing text within a blurred or deformed image is a common CAPTCHA method to tell humans apart from computers.</p>
<p></p>
<p>When using OCR to extract text from a document, the result will rarely be perfect. The accuracy of the results varies depending on the quality of the image. Obviously images used by CAPTCHA software are designed to be too difficult to recognize by state of the art OCR methods.</p>
<h2>Context Language</h2>
<p>A character can often only be recognized in the context of the word or sentence appears in. For example if a text contains the words In love the capital I and lower case l look (nearly) identical when printed.</p>
<p><a href="http://jeroenooms.github.io/files/inlove.png"></a></p>
<p>They can only be distinguished them from their context: both in and love are common words in English and a preposition may be followed by a noun. From from this context we can derive that the first character is most likely a capital I whereas the third character must be a lower case l.</p>
<div><pre><code>text <span>&lt;-</span> ocr<span>(</span><span>&quot;http://jeroenooms.github.io/files/inlove.png&quot;</span><span>)</span>
<span>cat</span><span>(</span>text<span>)</span> <span># In love</span>
</code></pre></div>
<p>The OCR method used by tesseract uses language specific training data to optimize character recognition. The default language is English, training data for other languages are provided via the official <a href="https://github.com/tesseract-ocr/tessdata">tessdata repository</a> directory. On Linux these can be installed directly with the <a href="https://apps.fedoraproject.org/packages/tesseract">yum</a> or <a href="https://packages.debian.org/search?suite=stable&section=all&arch=any&searchon=names&keywords=tesseract-ocr-">apt</a> package manager.</p>
<p>On Windows/MacOS you have to manually download training data for other langauges for now. The next version of the package will hopefully make this a little easier.</p>
<h2>Optimizing Performance</h2>
<p>Besides training data, the most important aspect of OCR performance is the quality of the input image. High resolution images with horizontal text, high contrast and little noise will achieve the best accuracy. The official <a href="https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality">Tesseract Wiki</a> has some advice on how to improve the image quality.</p>
<p><a href="http://jeroenooms.github.io/files/dog_hq.png"></a></p>
<p>To put this to the test, we made two screenshots from the Wikipedia page above. The high quality image in the code below has approximately double the resolution of the low quality image. In addition the font rendering seems slightly better for the high quality image.</p>
<div><pre><code><span># Low quality:</span>
text1 <span>&lt;-</span> ocr<span>(</span><span>&quot;http://jeroenooms.github.io/files/dog_lq.png&quot;</span><span>)</span>
<span>cat</span><span>(</span>text1<span>)</span>

<span># High quality:</span>
text2 <span>&lt;-</span> ocr<span>(</span><span>&quot;http://jeroenooms.github.io/files/dog_hq.png&quot;</span><span>)</span>
<span>cat</span><span>(</span>text2<span>)</span>
</code></pre></div>
<p>Running this example in R shows that the accuracy of text extraction from the first image is very high but it dramatically decreases for the second image. Because Tesseract relies on the context to recognize characters, accuracy drops exponentially as increasingly many characters become blurry and ambiguous.</p>
<h2>Future Plans</h2>
<p>The current version of the 'tesseract' package is stable and essentially feature complete. We may release an update in the future to include additional utitlities for downloading and managing training data. These updates may also include some integration with the rOpenSci's <a href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html">magick</a> package to help with preprocessing images.</p>
<p>All of our development at rOpenSci is driven by user feedback. If you find a problem or have suggestions for improvement, we would love to hear about it on our <a href="https://github.com/ropensci/tesseract/issues">Github page</a>!</p>