Title: Correcting Text Orientation with Tesseract and Python - PyImageSearch

URL Source: https://pyimagesearch.com/2022/01/31/correcting-text-orientation-with-tesseract-and-python/

Published Time: 2022-01-31T14:00:00+00:00

Markdown Content:
An essential component of any OCR system is **image preprocessing** — the higher the quality input image you present to the OCR engine, the better your OCR output will be. To be successful in OCR, you need to review arguably _the most important_ pre-processing step: **text orientation.**

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

**To learn how to perform text orientation with 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)

Text orientation refers to the rotation angle of a piece of text in an image. A given word, sentence, or paragraph will look like gibberish to an OCR engine if the text is significantly rotated. OCR engines are intelligent, but like humans, they are not trained to read upside-down!

Therefore, a critical first step in preparing your image data for OCR is to detect text orientation (if any) and then correct the text orientation. From there, you can present the corrected image to your OCR engine (and ideally obtain higher OCR accuracy).

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

In this tutorial, you will learn:

1.  The concept of orientation and script detection (OSD)
2.  How to detect text script (i.e., writing system) with Tesseract
3.  How to detect text orientation using Tesseract
4.  How to automatically correct text orientation with OpenCV

### **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%20600%20401'%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!

**What Is Orientation and Script Detection?**
---------------------------------------------

Before we automatically detect and correct text orientation with Tesseract, we first need to discuss the concept of **orientation and script detection (OSD).** Tesseract has several different modes that you can use when automatically detecting and OCR’ing text. Some of these modes perform a full-blown OCR of the input image, while others output meta-data such as text information, orientation, etc. (i.e., your OSD modes). Tesseract’s OSD mode is going to give you two output values:

*   **Text orientation:** The estimated rotation angle (in degrees) of the text in the input image.
*   **Script:** The predicted “writing system” of the text in the image.

**Figure 2** shows an example of varying text orientations. When in OSD mode, Tesseract will detect these orientations and tell us how to correct the orientation.

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

**Figure 2.** In OSD mode, Tesseract can detect text orientation and script type. From there, we can rotate the text back to 0**_°_** with OpenCV.

**A writing system is a visual method of communicating information, but unlike speech, a writing system also includes the concept of “storage” and “knowledge transfer.”**

When we put pen to paper, the characters we utilize are part of a script/writing system. These characters can be read by us and others, thereby imparting and transferring knowledge from the writer. Additionally, this knowledge is “stored” on the paper, meaning that if we were to die, the knowledge left on that paper could be imparted to others who could read our script/writing system.

**Figure 2** also provides examples of various scripts and writing systems, including Latin (the script used in English and other languages) and Abjad (the script for Hebrew amid other languages). When placed in OSD mode, Tesseract automatically detects the text’s writing system in the input image.

If you’re new to the concept of scripts/writing systems, I would strongly recommend reading [Wikipedia’s excellent article](https://en.wikipedia.org/wiki/List_of_writing_systems) on the topic. It’s a great read which covers the history of writing systems and how they’ve evolved.

**Detecting and Correcting Text Orientation with Tesseract**
------------------------------------------------------------

Now that we understand OSD’s basics, let’s move on to detecting and correcting text orientation with Tesseract. We’ll start with a quick review of our project directory structure. From there, I’ll show you how to implement text orientation correction. We’ll wrap up this tutorial with a discussion of our results.

### **Project Structure**

Let’s dive into the directory structure for this project:

|-- images
|   |-- normal.png
|   |-- rotated\_180.png
|   |-- rotated\_90\_clockwise.png
|   |-- rotated\_90\_counter\_clockwise.png
|   |-- rotated\_90\_counter\_clockwise\_hebrew.png
|-- detect\_orientation.py

All the code to detect and correct text orientation is contained within the `detect_orientation.py` Python script and implemented in less than 35 lines of code, including comments. We’ll test the code using a selection of `images/` included in the project folder.

### **Implementing Our Text Orientation and Correction Script**

Let’s get started implementing our text orientation corrector with Tesseract and OpenCV.

Open a new file, name it `detect_orientation.py`, and insert the following code:

\# import the necessary packages
from pytesseract import Output
import pytesseract
import argparse
import imutils
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")
args = vars(ap.parse\_args())

An import you might not recognize at first is PyTesseract’s `Output` class ([https://github.com/madmaze/pytesseract](https://github.com/madmaze/pytesseract)). This class simply specifies four datatypes including `DICT` which we will take advantage of.

Our lone command line argument is our input `--image` to be OCR’d. Let’s load the input now:

\# load the input image, convert it from BGR to RGB channel ordering,
# and use Tesseract to determine the text orientation
image = cv2.imread(args\["image"\])
rgb = cv2.cvtColor(image, cv2.COLOR\_BGR2RGB)
results = pytesseract.image\_to\_osd(rgb, output\_type=Output.DICT)

# display the orientation information
print("\[INFO\] detected orientation: {}".format(
	results\["orientation"\]))
print("\[INFO\] rotate by {} degrees to correct".format(
	results\["rotate"\]))
print("\[INFO\] detected script: {}".format(results\["script"\]))

**Lines 16 and 17** load our input `--image` and swap color channels so that it is compatible with Tesseract.

From there, we apply **orientation and script detection (OSD)** to the `rgb` image while specifying our `output_type=Output.DICT` (**Line 18**). We then display the orientation and script information in the terminal (contained in the `results` dictionary) including:

*   The current orientation
*   How many degrees to rotate the image to correct its orientation
*   The type of script detected, such as Latin or Arabic

Given this information, next, we’ll correct the text orientation:

\# rotate the image to correct the orientation
rotated = imutils.rotate\_bound(image, angle=results\["rotate"\])

# show the original image and output image after orientation
# correction
cv2.imshow("Original", image)
cv2.imshow("Output", rotated)
cv2.waitKey(0)

Using my imutils `rotate_bound` method ([http://pyimg.co/vebvn](http://pyimg.co/vebvn)), we rotate the image ensuring that the entire image stays fully visible in the results (**Line 28**). Had we used OpenCV’s generic `cv2.rotate` method, the corners of the image would have been cut off. Finally, we display both the original and rotated images until a key is pressed (**Lines 32-34**).

### **Text Orientation and Correction Results**

We are now ready to apply text OSD! Open a terminal and execute the following command:

$ python detect\_orientation.py --image images/normal.png
\[INFO\] detected orientation: 0
\[INFO\] rotate by 0 degrees to correct
\[INFO\] detected script: Latin

**Figure 3** displays the results of our script and orientation detection. Notice that the input image has _not_ been rotated, implying the orientation is 0**_°_**. No rotation correction is required. The script is then detected as “Latin.”

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

**Figure 3.** This screenshot from [one of my autoencoder blog posts](https://pyimagesearch.com/2020/03/30/autoencoders-for-content-based-image-retrieval-with-keras-and-TensorFlow/) is already properly oriented. Thus, the input is the same as the output after correcting for text orientation with Tesseract.

Let’s try another image, this one with rotated text:

$ python detect\_orientation.py --image images/rotated\_90\_clockwise.png
\[INFO\] detected orientation: 90
\[INFO\] rotate by 270 degrees to correct
\[INFO\] detected script: Latin

**Figure 4** shows the original input image, which contains rotated text. Using Tesseract in OSD mode, we can detect that the text in the input image has an orientation of 90**_°_** — we can correct this orientation by rotating the image 270**_°_** (i.e., _−_90**_°_**). And once again, the detected script is Latin.

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

**Figure 4.** As you can see, the input is not oriented in the way that we read side-to-side. Tesseract and OSD detect that the image is rotated 90**_°_**. From there, we use OpenCV to rotate the image 90**_°_** to counteract and re-orient the image.

We’ll wrap up this tutorial with one final example, this one of non-Latin text:

$ python detect\_orientation.py \\
    --image images/rotated\_90\_counter\_clockwise\_hebrew.png
\[INFO\] detected orientation: 270
\[INFO\] rotate by 90 degrees to correct
\[INFO\] detected script: Hebrew

**Figure 5** shows our input text image. We then detect the script (Hebrew) and correct its orientation by rotating the text 90**_°_**.

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

**Figure 5.** Tesseract can detect that this Hebrew input image is rotated 270**_°_** with OSD. We use OpenCV to rotate the image by 90**_°_** to correct this orientation problem.

As you can see, Tesseract makes text OSD easy!

### 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 perform automatic text orientation detection and correction using Tesseract’s orientation and script detection (OSD) mode.

The OSD mode provides us with meta-data of the text in the image, including both **estimated text orientation** and **script/writing system detection.** The text orientation refers to the angle (in degrees) of the text in the image. When performing OCR, we can obtain higher accuracy by correcting for the text orientation. Script detection, on the other hand, refers to the writing system of the text, which could be Latin, Hanzi, Arabic, Hebrew, etc.

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

### Unleash the potential of computer vision with Roboflow - Free!

*   Step into the realm of the future by [signing up or logging into your Roboflow account](https://roboflow.com/?ref=pyimagesearch). Unlock a wealth of innovative dataset libraries and revolutionize your computer vision operations.
*   Jumpstart your journey by choosing from our broad array of datasets, or benefit from PyimageSearch’s comprehensive library, crafted to cater to a wide range of requirements.
*   Transfer your data to Roboflow in any of the 40+ compatible formats. Leverage cutting-edge model architectures for training, and deploy seamlessly across diverse platforms, including API, NVIDIA, browser, iOS, and beyond. Integrate our platform effortlessly with your applications or your favorite third-party tools.
*   Equip yourself with the ability to train a potent computer vision model in a mere afternoon. With a few images, you can import data from any source via API, annotate images using our superior cloud-hosted tool, kickstart model training with a single click, and deploy the model via a hosted API endpoint. Tailor your process by opting for a code-centric approach, leveraging our intuitive, cloud-based UI, or combining both to fit your unique needs.
*   Embark on your journey today with absolutely no credit card required. Step into the future with Roboflow.

[Join Roboflow Now](https://roboflow.com/?ref=pyimagesearch)

**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 9](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!