{"id":8478,"date":"2022-05-11T12:00:00","date_gmt":"2022-05-11T16:00:00","guid":{"rendered":"https:\/\/sites.nd.edu\/manuscript-studies\/?p=8478"},"modified":"2022-06-08T18:52:03","modified_gmt":"2022-06-08T22:52:03","slug":"optical-character-recognition-with-tesseract-a-tutorial-for-medievalists","status":"publish","type":"post","link":"https:\/\/sites.nd.edu\/manuscript-studies\/2022\/05\/11\/optical-character-recognition-with-tesseract-a-tutorial-for-medievalists\/","title":{"rendered":"Optical Character Recognition with Tesseract: a Tutorial for Medievalists"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This tutorial is for those of you who want to learn some basic programming in Python for the digital humanities, but also for those who have never programmed or may become filled with terror at the sight of a single line of code (trust me, I know the feeling!). At the end of this guide, you should know how to perform optical character recognition (OCR) to make a pdf searchable. For this, we are going to use \u201cocrmypdf\u201d, a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tesseract_(software)\">Tesseract<\/a>-based Python package with wonderful capabilities. Everything will be done online from your web browser, so don\u2019t worry, you will not have to install anything on your computer!\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have tried to keep the tutorial very simple and straight to the point, at the cost of occasionally sacrificing some useful and important explanations about the code. For that, I profusely apologize to my colleagues in the department of computer science. Please, do not send the Spanish Inquisition.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"702\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/MontyPython.png\" alt=\"\" class=\"wp-image-8479\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/MontyPython.png 936w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/MontyPython-300x225.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/MontyPython-768x576.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>The name &#8220;Python&#8221; is a tribute to <span style=\"text-decoration: underline\">Monty Python<\/span> (please excuse the poor joke). <\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Why Tesseract and ocrmypdf?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some of you may be familiar with, or even regular users of the OCR function provided by Adobe Acrobat DC pro. The ctrl+f function is one of the simplest and most efficient tools to support analysis of text and research. Acrobat DC is a powerful and very easy to use software, but this comes at a price. First, Acrobat works only with a handful of common modern languages such as English, French, German or Japanese. Tesseract on the other hand can recognize characters from a broad variety of modern and classical languages, including, but not limited to, Armenian, Classical Arabic, Classical Greek, Syriac, and Old Georgian, to name only a few. Second, Tesseract is a free, and open-source software, presenting a more cost-efficient option compared to other expensive commercial options and I am sure many of you would rather settle for the free but equally powerful alternative.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, go to <a href=\"https:\/\/colab.research.google.com\/\">https:\/\/colab.research.google.com\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is a free <a href=\"https:\/\/en.wikipedia.org\/wiki\/Project_Jupyter\">Jupyter<\/a> notebook that will save your data on your personal google drive. Just remember to hit the save button before closing the page.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"291\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture1-1024x291.png\" alt=\"\" class=\"wp-image-8480\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture1-1024x291.png 1024w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture1-300x85.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture1-768x219.png 768w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture1.png 1040w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Open a new notebook and sign up with a Gmail address. You will want to use your @nd.edu address but any personal google account will work too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a first step, we need to download ocrmypdf, and all its dependencies. To do so, simply type the following lines. The first will download the python package ocrmypdf, while the other lines will deal with the dependencies. For those of you new to coding, you will learn the first rule of coding: any errors in spelling, indentation and so forth can break your code. Be careful!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"327\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Screen-Shot-2022-05-10-at-7.18.10-PM-1024x327.png\" alt=\"\" class=\"wp-image-8494\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Screen-Shot-2022-05-10-at-7.18.10-PM-1024x327.png 1024w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Screen-Shot-2022-05-10-at-7.18.10-PM-300x96.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Screen-Shot-2022-05-10-at-7.18.10-PM-768x245.png 768w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Screen-Shot-2022-05-10-at-7.18.10-PM.png 1164w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When the above lines have been written, run the cell by pressing ctrl+enter or press the button in the upper left corner. The download process should take around a minute. Once the download is complete, you should see a little green tick next to the upper left arrow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the package and its dependencies have been downloaded, we will need to import \u201cocrmypdf\u201d so that we can put it to work. Add a line of code to your colab sheet by clicking on + Code and write the following code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"78\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture3.a.png\" alt=\"\" class=\"wp-image-8484\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture3.a.png 570w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture3.a-300x41.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"120\" src=\"http:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture3.png\" alt=\"\" class=\"wp-image-8482\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture3.png 708w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture3-300x51.png 300w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next, add a copy of your scanned pdf to the&nbsp;&#8220;files\/content&#8221;&nbsp;folder&nbsp;&nbsp;on the left&nbsp;side of your screen&nbsp;(or any other folder of your choice, you will just have to note its path somewhere). In our case, we are going to work with the first page of an article on the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mevlevi_Order\">Mevlevi Sufi order<\/a> published by the Byzantinist <a href=\"https:\/\/en.wikipedia.org\/wiki\/Speros_Vryonis\">Speros Vryonis Junior<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"636\" height=\"480\" src=\"http:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture4.png\" alt=\"\" class=\"wp-image-8483\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture4.png 636w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture4-300x226.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In a new cell, enter the following lines of code (beware, the underscores are double underscores!).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"70\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture5.png\" alt=\"\" class=\"wp-image-8485\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture5.png 936w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture5-300x22.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture5-768x57.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the name on the left should be that of the file you want to ocr (or its path if put in another folder), the one on the right should be that of the new, postprocessed file. The code uses the exact name of the file, \u2018Vryonis_Article.pdf\u2019. You can keep the exact same name if you want the new file to overwrite the original one. In my case, my code is directing ocrmypdf to create a new file: \u2018Vryonis_Article_OCR.pdf\u2019. Once generated, the post-processed article should appear in the same folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you ever get an error, simply restart the runtime before running the cell again.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"430\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture6.png\" alt=\"\" class=\"wp-image-8486\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture6.png 504w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture6-300x256.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"568\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture7.png\" alt=\"\" class=\"wp-image-8487\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture7.png 936w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture7-300x182.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture7-768x466.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Et voila! You can now search your pdf with ctrl+F or copy and paste any sentence you want.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But this is not the most exciting part of this tutorial, and we may want to spice things up a little bit. Tesseract is very good at OCRing (yes, this is a verb, at least according to the <a href=\"https:\/\/www.wordsense.eu\/OCR\/\">WordSense dictionary<\/a>) non-Latin scripts, but the process is a bit more involved. As an example, let\u2019s take a page from the <em>Mas\u0101lik al-ab\u1e63\u0101r<\/em> <em>f\u012b<\/em> <em>mam\u0101lik al-am\u1e63\u0101r<\/em> written in the 14<sup>th<\/sup> century by the Syrian polymath <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ibn_Fadlallah_al-Umari\">al-\u02bfUmar\u012b<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this, you need first to download the Arabic trained data at <a href=\"https:\/\/github.com\/tesseract-ocr\/tessdata\/tree\/main\/script\">https:\/\/github.com\/tesseract-ocr\/tessdata\/tree\/main\/script<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then move the downloaded file to the following folder \/usr\/share\/tesseract-ocr\/4.00\/tessdata<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"106\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture8.png\" alt=\"\" class=\"wp-image-8488\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture8.png 936w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture8-300x34.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture8-768x87.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The process is the same as before, simply change the language code to that of the language you just added, in our case \u201cara\u201d. The various language codes can be found here: (<a href=\"https:\/\/tesseract-ocr.github.io\/tessdoc\/Data-Files-in-different-versions.html\">https:\/\/tesseract-ocr.github.io\/tessdoc\/Data-Files-in-different-versions.html<\/a>)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"204\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture9.png\" alt=\"\" class=\"wp-image-8489\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture9.png 936w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture9-300x65.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture9-768x167.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u0648\u0647\u0648 \u0623\u062b\u0631\u0649 \u0627\u0644\u0645\u0645\u0627\u0644\u0643 \u0628\u0644\u0627 \u0627\u062d\u062a\u0634\u0627\u0645 \u062e\u0644\u0627 \u0623\u0646\u0647 \u0628\u0643\u062b\u0631\u0629<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And here we are. Those who can read Arabic will notice that the result is extremely impressive. This, however, is a rather neat scan and Arabic is often difficult to properly OCR because of the cursive nature of the script. If the quality of your scan is poor, you may also be able to clean it with Python beforehand for a better result. I may develop this point further in another post.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Beyond Arabic, Tesseract works very well with other non-cursive Semitic scripts, and you may get excellent results with Hebrew for example. Here is a last example from a Syriac Bible.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"246\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture10.png\" alt=\"\" class=\"wp-image-8490\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture10.png 936w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture10-300x79.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture10-768x202.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"84\" src=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture11.png\" alt=\"\" class=\"wp-image-8491\" srcset=\"https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture11.png 908w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture11-300x28.png 300w, https:\/\/sites.nd.edu\/manuscript-studies\/files\/2022\/05\/Picture11-768x71.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any questions or comments about this guide, feel free to <a href=\"mailto:rthurin@nd.edu\">contact me<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/nd.academia.edu\/RomainThurin\">Romain Thurin<\/a><br \/>PhD Candidate<br \/>Medieval Institute<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial is for those of you who want to learn some basic programming in Python for the digital humanities, but also for those who have never programmed or may become filled with terror at the sight of a single line of code (trust me, I know the feeling!). At the end of this guide, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sites.nd.edu\/manuscript-studies\/2022\/05\/11\/optical-character-recognition-with-tesseract-a-tutorial-for-medievalists\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Optical Character Recognition with Tesseract: a Tutorial for Medievalists&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1846,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[324774,43988,56012,483958],"tags":[518374,43988,518372,161,518373,518375],"class_list":["post-8478","post","type-post","status-publish","format-standard","hentry","category-arabic-languages-and-literatures","category-digital-humanities","category-english","category-public-medievalism","tag-coding","tag-digital-humanities","tag-python","tag-research","tag-tesseract","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/posts\/8478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/users\/1846"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/comments?post=8478"}],"version-history":[{"count":4,"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/posts\/8478\/revisions"}],"predecessor-version":[{"id":8534,"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/posts\/8478\/revisions\/8534"}],"wp:attachment":[{"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/media?parent=8478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/categories?post=8478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.nd.edu\/manuscript-studies\/wp-json\/wp\/v2\/tags?post=8478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}