{"id":189,"date":"2021-03-01T22:06:53","date_gmt":"2021-03-01T22:06:53","guid":{"rendered":"https:\/\/insidecrane.utoronto.ca\/?p=189"},"modified":"2021-03-01T22:06:55","modified_gmt":"2021-03-01T22:06:55","slug":"read-arabic-text-from-a-map-using-google-cloud-vision","status":"publish","type":"post","link":"https:\/\/insidecrane.utoronto.ca\/?p=189","title":{"rendered":"Read Arabic Text from a Map using Google Cloud Vision"},"content":{"rendered":"\n<p>By Jeff Blackadar<\/p>\n\n\n\n<p>Reposted from <a href=\"https:\/\/jeffblackadar.ca\/uncategorized\/read_arabic_text_google_cloud_vision\/\">Digital History Learning Journal, February 28, 2021.<\/a><\/p>\n\n\n\n<p>I would like to work with Arabic language maps and this post sets up transcription of one map tile using Google Cloud Vision.<\/p>\n\n\n\n<p>I am grateful to Dr Kristen Hopper and Dr. Dan Lawrence of Durham University and Dr. Hector Orengo of the University of Cambridge for sending me a set of georeferenced digital maps to work with. Thank you!<\/p>\n\n\n\n<p>I\u2019m working with a map titled Djeble, dated 1942 which is centered on&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Jableh\">Jableh, Syria<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Set up Google Cloud Vision<\/h2>\n\n\n\n<p>The steps to step up the project for Google Cloud Vision are in here.&nbsp;<a href=\"https:\/\/cloud.google.com\/vision\/docs\/setup\">https:\/\/cloud.google.com\/vision\/docs\/setup<\/a>. I have repeated the information below based on the steps I took in case it\u2019s useful. Skip to the next step if you followed all of the instructions in the setup.<\/p>\n\n\n\n<p>In the Dashboard of Google Cloud Platform:<\/p>\n\n\n\n<p>Create Project and give it a name.<\/p>\n\n\n\n<p>Check that Billing is enabled.<\/p>\n\n\n\n<p>Enable the API.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" width=\"472\" height=\"281\" src=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/Image-1.png\" alt=\"\" class=\"wp-image-191\" srcset=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/Image-1.png 472w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/Image-1-300x179.png 300w\" sizes=\"(max-width: 472px) 100vw, 472px\" \/><figcaption> <em>Register the new application to use Cloud Vision API.<\/em> <\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"411\" height=\"187\" src=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-2.png\" alt=\"\" class=\"wp-image-192\" srcset=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-2.png 411w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-2-300x136.png 300w\" sizes=\"(max-width: 411px) 100vw, 411px\" \/><figcaption><em>Enable the API.<\/em> <\/figcaption><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"765\" height=\"645\" src=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/Image-3.png\" alt=\"\" class=\"wp-image-193\" srcset=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/Image-3.png 765w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/Image-3-300x253.png 300w\" sizes=\"(max-width: 765px) 100vw, 765px\" \/><figcaption> <em>Get credentials to access the API.<\/em> <\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"899\" height=\"680\" src=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-4.png\" alt=\"\" class=\"wp-image-194\" srcset=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-4.png 899w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-4-300x227.png 300w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-4-768x581.png 768w\" sizes=\"(max-width: 899px) 100vw, 899px\" \/><figcaption> <em>Set the permissions for the credentials.<\/em> <\/figcaption><\/figure>\n\n\n\n<p>Download the credentials as a .json. Upload the .json file to a secure directory on Google drive separate from your code. Keep this private.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Results<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"771\" src=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-5-1024x771.png\" alt=\"\" class=\"wp-image-196\" srcset=\"https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-5-1024x771.png 1024w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-5-300x226.png 300w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-5-768x578.png 768w, https:\/\/insidecrane.utoronto.ca\/wp-content\/uploads\/2021\/03\/image-5.png 1284w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption> <em>Tile from the map. The red text represents what Google Cloud Vision was able to transcribe.<\/em> <\/figcaption><\/figure>\n\n\n\n<p>The program I used to do this is here:&nbsp;<a href=\"https:\/\/github.com\/jeffblackadar\/CRANE-CCAD-maps\/blob\/main\/maps_ocr_google_cloud_vision_1_tile.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/jeffblackadar\/CRANE-CCAD-maps\/blob\/main\/maps_ocr_google_cloud_vision_1_tile.ipynb<\/a><\/p>\n\n\n\n<p>The above has errors and some transcriptions are missing. Still, this looks promising.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Notes about the program<\/h3>\n\n\n\n<p>In Google Colab I need to install google-cloud-vision to transcribe text and the other 3 packages to plot Arabic text.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!pip install --upgrade google-cloud-vision\n!pip install arabic_reshaper\n!pip install bidi\n!pip install python-bidi<\/code><\/pre>\n\n\n\n<p>To transcribe Arabic text, Cloud Vision uses language_hints = \u201car\u201d. See&nbsp;<a href=\"https:\/\/cloud.google.com\/vision\/docs\/languages\">https:\/\/cloud.google.com\/vision\/docs\/languages<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    client = vision.ImageAnnotatorClient()\n\n    with io.open(path, 'rb') as image_file:\n        content = image_file.read()\n\n    image = vision.Image(content=content)\n    response = client.text_detection(\n    image=image,\n    image_context={\"language_hints\": [\"ar\"]},  \n     )<\/code><\/pre>\n\n\n\n<p>To plot Arabic text, I used a font and the code below. Thanks to Stack Overflow.&nbsp;<a href=\"https:\/\/stackoverflow.com\/questions\/59896297\/how-to-draw-arabic-text-on-the-image-using-cv2-puttextcorrectly-pythonopenc\">https:\/\/stackoverflow.com\/questions\/59896297\/how-to-draw-arabic-text-on-the-image-using-cv2-puttextcorrectly-pythonopenc<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fontpath = \"\/content\/drive\/MyDrive\/crane_font\/arial.ttf\" # &lt;== https:\/\/www.freefontspro.com\/14454\/arial.ttf  \nfont_pil = ImageFont.truetype(fontpath, 32)\n\nimg_pil = Image.fromarray(img)\ndraw = ImageDraw.Draw(img_pil)\n\nfor l in lines_of_text:\n    print(l[0])\n    pts = l[1]\n    #This is needed to handle the Arabic text\n    reshaped_text = arabic_reshaper.reshape(l[0])\n    bidi_text = get_display(reshaped_text)\n    draw.text((int(pts[0]), int(pts[1])),bidi_text, font = font_pil,fill=(255,0,0,255))\n<\/code><\/pre>\n\n\n\n<p>The next steps are process all of the tiles on the map. I also intend to process the tiles to remove some of the non-text elements on the map that confuse transcription.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By Jeff Blackadar Reposted from Digital History Learning Journal, February 28, 2021. I would like to work with Arabic language maps and this post sets up transcription of one map tile using Google Cloud Vision. I am grateful to Dr Kristen Hopper and Dr. Dan Lawrence of Durham University and Dr. Hector Orengo of the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":196,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-189","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-uncategorized","8":"entry"},"_links":{"self":[{"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":4,"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"predecessor-version":[{"id":198,"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions\/198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=\/wp\/v2\/media\/196"}],"wp:attachment":[{"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/insidecrane.utoronto.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}