Title: Language Translation and OCR with Tesseract and Python - PyImageSearch

URL Source: https://pyimagesearch.com/2021/09/20/language-translation-and-ocr-with-tesseract-and-python/

Published Time: 2021-09-20T14:00:00+00:00

Markdown Content:
Given that we can detect the writing system of the text, it raises the question:

> _Is it possible to_ **_translate_** _text from one language to another using OCR and Tesseract?_

![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20500%20281'%3E%3C/svg%3E)

**To learn how to translate languages using Tesseract and Python,** **_just keep reading._**

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

The short answer is _yes_, it is possible — but we’ll need a bit of help from the `textblob` library, a popular Python package for text processing ([TextBlob: Simplified Text Processing](https://textblob.readthedocs.io/en/dev/)). By the end of this tutorial, you will automatically translate OCR’d text from one language to another.

### **Configuring your development environment**

To follow this guide, you need to have the OpenCV library installed on your system.

Luckily, OpenCV is pip-installable:

$ pip install opencv-contrib-python

**If you need help configuring your development environment for OpenCV, I _highly recommend_ that you read my** [**_pip install OpenCV_ guide**](https://pyimagesearch.com/2018/09/19/pip-install-opencv/) — it will have you up and running in a matter of minutes.

### **Having problems configuring your development environment?**

[![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20650%20434'%3E%3C/svg%3E)](https://pyimagesearch.com/pyimagesearch-university/)

**Figure 1:** Having trouble configuring your dev environment? Want access to pre-configured Jupyter Notebooks running on Google Colab? Be sure to join [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/) — you’ll be up and running with this tutorial in a matter of minutes.

All that said, are you:

*   Short on time?
*   Learning on your employer’s administratively locked system?
*   Wanting to skip the hassle of fighting with the command line, package managers, and virtual environments?
*   **Ready to run the code** **_right now_** **on your Windows, macOS, or Linux system?**

Then join [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/) today!

**Gain access to Jupyter Notebooks for this tutorial and other PyImageSearch guides that are** **_pre-configured_** **to run on Google Colab’s ecosystem right in your web browser!** No installation required.

And best of all, these Jupyter Notebooks will run on Windows, macOS, and Linux!

**Learning Objectives**
-----------------------

In this tutorial, you will:

1.  Learn how to translate text using the `TextBlob` Python package
2.  Implement a Python script that OCRs text and then translates it
3.  Review the results of the text translation

**OCR and Language Translation**
--------------------------------

In the first part of this tutorial, we’ll briefly discuss the `textblob` package and how it can be used to translate text. From there, we’ll review our project directory structure and implement our OCR and text translation Python script. We’ll wrap up the tutorial with a discussion of our OCR and text translation results.

### **Translating Text to Different Languages with TextBlob**

To translate text from one language to another, we’ll use the `textblob` Python package ([https://textblob.readthedocs.io/en/dev/](https://textblob.readthedocs.io/en/dev/)). If you’ve followed the development environment configuration instructions from [an earlier tutorial](https://pyimagesearch.com/2021/08/09/what-is-optical-character-recognition-ocr/), then you should already have `textblob` installed on your system. If not, you can install it with `pip`:

$ pip install textblob

Once `textblob` is installed, you should run the following command to download the Natural Language Toolkit (NLTK) corpora that `textblob` uses to automatically analyze text:

$ python -m textblob.download\_corpora

Next, you should familiarize yourself with the library by opening a Python shell:

$ python
>>> from textblob import TextBlob
>>>

Notice how we are importing the `TextBlob` class — this class enables us to automatically analyze a piece of text for tags, noun phrases, and yes, even language translation. Once instantiated, we can call the `translate()` method of the `TextBlob` class and perform the automatic text translation. Let’s use `TextBlob` to do that now: UTF8ipxm

\>>> text = u"おはようございます。"
>>> tb = TextBlob(text)
>>> translated = tb.translate(to="en")
>>> print(translated)
Good morning.
>>>

Notice how I have successfully translated the Japanese phrase for _“Good morning”_ into English.

### **Project Structure**

Let’s start by reviewing the project directory structure for this tutorial:

|-- comic.png
|-- ocr\_translate.py

Our project consists of a funny cartoon image that I generated with a comic tool called [Explosm](http://explosm.net/). Our `textblob` based OCR translator is housed in the `ocr_translate.py` script.

### **Implementing Our OCR and Language Translation Script**

We are now ready to implement our Python script, which will automatically OCR text and translate it into our chosen language. Open the `ocr_translate.py` in our project directory structure, and insert the following code:

\# import the necessary packages
from textblob import TextBlob
import pytesseract
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-i", "--image", required=True,
	help="path to input image to be OCR'd")
ap.add\_argument("-l", "--lang", type=str, default="es",
	help="language to translate OCR'd text to (default is Spanish)")
args = vars(ap.parse\_args())

We begin with our imports, where `TextBlob` is the most notable for this script. From there, we dive into our command line argument parsing procedure. We have two command line arguments:

*   `--image`: The path to our input image to be OCR’d _and_ translated
*   `--lang`: The language to translate the OCR’d text into — by default, it is Spanish (`es`)

Using `pytesseract`, we’ll OCR our input image:

\# load the input image and convert it from BGR to RGB channel
# ordering
image = cv2.imread(args\["image"\])
rgb = cv2.cvtColor(image, cv2.COLOR\_BGR2RGB)

# use Tesseract to OCR the image, then replace newline characters
# with a single space
text = pytesseract.image\_to\_string(rgb)
text = text.replace("\\n", " ")

# show the original OCR'd text
print("ORIGINAL")
print("========")
print(text)
print("")

Upon loading and converting our `--image` to RGB format (**Lines 17 and 18**), we send it through the Tesseract engine via `pytesseract` (**Line 22**). Our `textblob` package won’t know what to do with newline characters present in `text`, so we `replace` them with spaces (**Line 23**).

After printing out our original OCR’d `text`, we’ll go ahead and **translate the string into our desired language:**

\# translate the text to a different language
tb = TextBlob(text)
translated = tb.translate(to=args\["lang"\])

# show the translated text
print("TRANSLATED")
print("==========")
print(translated)

**Line 32** constructs a `TextBlob` object, passing the original `text` to the constructor. From there, **Line 33** translates the `tb` into our desired `--lang`. And finally, we print out the `translated` result in our terminal (**Lines 36-38**).

That’s all there is to it. Just keep in mind the complexities of translation engines. The `TextBlob` engine under the hood is akin to services such as [Google Translate](https://translate.google.com/), though maybe less powerful. When [Google Translate came out in the mid-2000s](https://en.wikipedia.org/wiki/Google_Translate), it wasn’t nearly as polished and accurate as today. Some may argue that Google Translate is the gold standard. Depending on your OCR translation needs, you could swap in an API call to the Google Translate REST API if you find that `textblob` is not suitable for you.

### **OCR Language Translation Results**

We are now ready to OCR our input image with Tesseract, and then translate the text using `textblob`. To test our automatic OCR and translation script, open a terminal and execute the commands shown in **Figure 2** (_right_). Here, our input image on the _left_, contains the English exclamation, _“You told me learning OCR would be easy!”_ This image was generated using the [Explosm](http://explosm.net/) comic generator. As our terminal output shows, we successfully translated the text to Spanish, German, and Arabic (a _right-to-left_ language).

![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20679%20500'%3E%3C/svg%3E)

**Figure 2.** The results of Tesseract for OCR and `textblob` for translation of the exclamation _“You told me learning OCR would be easy!”_ into its Spanish, German, and Arabic equivalents.

OCR’ing and translating text is quite easy once you use the `textblob` package!

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

**Summary**
-----------

In this tutorial, you learned how to automatically OCR and translate text using Tesseract, Python, and the `textblob` library. Using `textblob`, translating the text was as easy as a single function call.

In our next tutorial, you’ll learn how to use Tesseract to _automatically_ OCR non-English languages, including non-Latin writing systems (e.g., Arabic, Chinese, etc.).

**To download the source code to this post (and be notified when future tutorials are published here on PyImageSearch), _simply enter your email address in the form below!_**

![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!