<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/tesseract-and-magick-high-quality-ocr-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/technotes/2017/08/17/tesseract-16"> rOpenSci Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Last week we released an update of the tesseract package to CRAN. This package provides R bindings to Google's OCR library <a rel="nofollow" href="https://en.wikipedia.org/wiki/Tesseract_(software)" target="_blank">Tesseract</a>.</p>
<div>
<pre><code data-lang="r"><span></span>install.packages<span>(</span><span>&quot;tesseract&quot;</span><span>)</span>
</code></pre>
</div>
<p>The new version ships with the latest libtesseract 3.05.01 on Windows and MacOS. Furthermore it includes enhancements for managing language data and using tesseract together with the magick package.</p>
<h2>Installing Language Data</h2>
<p>The new version has several improvements for installing additional language data. On Windows and MacOS you use the <code>tesseract_download()</code> function to install additional languages:</p>
<div>
<pre><code data-lang="r"><span></span>tesseract_download<span>(</span><span>&quot;fra&quot;</span><span>)</span>
</code></pre>
</div>
<p>Language data are now stored in <code>rappdirs::user_data_dir('tesseract')</code> which makes it persist across updates of the package. To OCR french text:</p>
<div>
<pre><code data-lang="r"><span></span>french <span>&lt;-</span> tesseract<span>(</span><span>&quot;fra&quot;</span><span>)</span>
text <span>&lt;-</span> ocr<span>(</span><span>&quot;https://jeroen.github.io/images/french_text.png&quot;</span><span>,</span> engine <span>=</span> french<span>)</span>
<span>cat</span><span>(</span>text<span>)</span>
</code></pre>
</div>
<p>Très Bien! Note that on Linux you should not use <code>tesseract_download</code> but instead install languages using apt-get (e.g. <a rel="nofollow" href="https://packages.debian.org/testing/tesseract-ocr-fra" target="_blank">tesseract-ocr-fra</a>) or yum (e.g. <a rel="nofollow" href="https://apps.fedoraproject.org/packages/tesseract-langpack-fra" target="_blank">tesseract-langpack-fra</a>).</p>
<h2>Tesseract and Magick</h2>
<p>The tesseract developers <a rel="nofollow" href="https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality" target="_blank">recommend</a> to clean up the image before OCR'ing it to improve the quality of the output. This involves things like cropping out the text area, rescaling, increasing contrast, etc.</p>
<p>The rOpenSci <a rel="nofollow" href="https://ropensci.org/blog/blog/2017/08/15/magick-10" target="_blank">magick</a> package is perfectly suitable for this task. The latest version contains a convenient wrapper <code>image_ocr()</code> that works with pipes.</p>
<div>
<pre><code data-lang="r"><span></span>devtools<span>::</span>install_github<span>(</span><span>&quot;ropensci/magick&quot;</span><span>)</span>
</code></pre>
</div>
<p>Let's give it a try on some <a rel="nofollow" href="https://courses.cs.vt.edu/csonline/AI/Lessons/VisualProcessing/OCRscans.html" target="_blank">example scans</a>:</p>
<p><img alt="example" src="https://i1.wp.com/courses.cs.vt.edu/csonline/AI/Lessons/VisualProcessing/OCRscans_files/bowers.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<div>
<pre><code data-lang="r"><span></span><span># Requires devel version of magick</span>
<span># devtools::install_github(&quot;ropensci/magick&quot;)</span>

<span># Test it</span>
<span>library</span><span>(</span>magick<span>)</span>
<span>library</span><span>(</span>magrittr<span>)</span>

text <span>&lt;-</span> image_read<span>(</span><span>&quot;https://courses.cs.vt.edu/csonline/AI/Lessons/VisualProcessing/OCRscans_files/bowers.jpg&quot;</span><span>)</span> <span>%>%</span>
  image_resize<span>(</span><span>&quot;2000&quot;</span><span>)</span> <span>%>%</span>
  image_convert<span>(</span>colorspace <span>=</span> <span>'gray'</span><span>)</span> <span>%>%</span>
  image_trim<span>()</span> <span>%>%</span>
  image_ocr<span>()</span>

<span>cat</span><span>(</span>text<span>)</span>
</code></pre>
</div>
<pre>
The Llfe and Work of
Fredson Bowers
by
G. THOMAS TANSELLE

N EVERY FIELD OF ENDEAVOR THERE ARE A FEW FIGURES WHOSE ACCOM-
plishment and inﬂuence cause them to be the symbols of their age;
their careers and oeuvres become the touchstones by which the
ﬁeld is measured and its history told. In the related pursuits of
analytical and descriptive bibliography, textual criticism, and scholarly
editing, Fredson Bowers was such a ﬁgure, dominating the four decades
after 1949, when his Principles of Bibliographical Description was pub-
lished. By 1973 the period was already being called “the age of Bowers”:
in that year Norman Sanders, writing the chapter on textual scholarship
for Stanley Wells's Shakespeare: Select Bibliographies, gave this title to
a section of his essay. For most people, it would be achievement enough
to rise to such a position in a ﬁeld as complex as Shakespearean textual
studies; but Bowers played an equally important role in other areas.
Editors of ninetcemh-cemury American authors, for example, would
also have to call the recent past “the age of Bowers,&quot; as would the writers
of descriptive bibliographies of authors and presses. His ubiquity in
the broad ﬁeld of bibliographical and textual study, his seemingly com-
plete possession of it, distinguished him from his illustrious predeces-
sors and made him the personiﬁcation of bibliographical scholarship in

his time.

\Vhen in 1969 Bowers was awarded the Gold Medal of the Biblio-
graphical Society in London, John Carter’s citation referred to the
Principles as “majestic,&quot; called Bowers's current projects “formidable,&quot;
said that he had “imposed critical discipline&quot; on the texts of several
authors, described Studies in Bibliography as a “great and continuing
achievement,&quot; and included among his characteristics &quot;uncompromising
seriousness of purpose” and “professional intensity.&quot; Bowers was not
unaccustomed to such encomia, but he had also experienced his share of
attacks: his scholarly positions were not universally popular, and he
expressed them with an aggressiveness that almost seemed calculated to
</pre>
<p>Not bad but not perfect. Can you do a better job?</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ropensci.org/blog/technotes/2017/08/17/tesseract-16"> rOpenSci Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/u3w9OIk986k" height="1"></a></div>