Text Detection from Images using EasyOCR: The Ultimate Guide

Optical Character Recognition (OCR) is a powerful technology that enables computers to detect and extract text from images and documents. OCR has become an essential tool in today‘s digital age, with applications spanning document processing, data entry, image indexing, and more.

One of the most popular and user-friendly OCR libraries available today is EasyOCR. Developed by Jaided AI, EasyOCR leverages state-of-the-art deep learning models to provide fast and accurate text detection in over 80 languages. In this comprehensive guide, we‘ll explore what makes EasyOCR stand out and walk through a step-by-step tutorial on using it to extract text from any image.

What is OCR?

OCR technology has been around since the 1970s, but has seen rapid advancement in recent years thanks to breakthroughs in artificial intelligence and deep learning. In a nutshell, OCR involves training a computer model to recognize the shapes and patterns of typed, handwritten, or printed characters, and convert them into machine-encoded text.

This allows physical documents to be digitized into searchable, editable formats. Some common use cases of OCR include:

  • Digitizing paper documents and records
  • Automating data entry from forms and invoices
  • Making text within images searchable
  • Extracting text from scanned book pages
  • License plate recognition

Leading OCR engines today, like EasyOCR, are able to detect and transcribe text with a high degree of accuracy, even from low resolution or skewed images. They can handle different fonts, styles, and languages, making OCR a truly versatile tool.

Introducing EasyOCR

Released in 2020, EasyOCR is a Python library that aims to make the OCR process as simple and accessible as possible. It supports over 80 languages, including many Asian languages like Chinese, Japanese, Korean, and Thai, as well as right-to-left languages like Arabic and Hebrew.

Some key features and advantages of EasyOCR are:

  • High accuracy on par with Google‘s Tesseract OCR engine
  • Simple and intuitive API for ease of use
  • Ability to run on CPU or GPU
  • Automatic text alignment and orientation detection
  • Support for rotated and irregular text
  • Modular design allowing easy integration of custom models

EasyOCR‘s text detection pipeline consists of two main steps. First, a deep learning model detects the presence and location of text within the input image. This model is trained on a large synthetic dataset to recognize text in highly variable conditions. Next, a text recognition model takes the cropped text regions and transcribes them into the output strings.

By decoupling detection and recognition, EasyOCR is able to achieve high efficiency and flexibly swap in different models. The default recognition model is a CRNN (Convolutional Recurrent Neural Network) trained on over 10 million text images.

Now that we have a high-level understanding of how EasyOCR works, let‘s dive into how to use it in practice.

Step 1: Install dependencies

Before we can start using EasyOCR, we need to make sure we have all the necessary dependencies installed. EasyOCR requires:

  • Python 3.6+
  • PyTorch 1.3+
  • OpenCV (cv2)

If you don‘t have PyTorch installed, head over to the official PyTorch website, select your preferences (OS, package manager, CUDA version if using GPU), and run the provided command. For example, to install PyTorch 1.8.1 with pip on Windows:

pip install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

With PyTorch installed, we can now install EasyOCR itself:

pip install easyocr

We‘ll also need OpenCV for working with images:

pip install opencv-python

Step 2: Import libraries

With our environment set up, create a new Python file and import the required libraries:

import easyocr
import cv2
from matplotlib import pyplot as plt
import numpy as np

Step 3: Load image

Next, we need to load the image we want to perform text detection on. You can either provide a file path to a local image, or a URL to an image on the web.

# Load image from file path
IMAGE_PATH = ‘path/to/image.jpg‘
reader = easyocr.Reader([‘en‘])
result = reader.readtext(IMAGE_PATH)

# Load image from URL 
IMAGE_PATH = ‘https://example.com/image.jpg‘
reader = easyocr.Reader([‘en‘])
result = reader.readtext(IMAGE_PATH)

Here we initialise an EasyOCR Reader for English language detection, passing [‘en‘] as an argument. To detect text in multiple languages, simply pass a list of language codes, e.g. [‘en‘, ‘fr‘, ‘de‘] for English, French and German.

The main method in EasyOCR is readtext(), which takes an image path and returns a list of tuples, one for each text detection. Each tuple contains the bounding box coordinates, the transcribed text, and a confidence score between 0 and 1.

Step 4: Extract text

We can inspect the detection results returned by readtext():

print(result)

Output:

[([[189, 75], [469, 75], [469, 165], [189, 165]], ‘DEEP LEARNING‘, 0.8753607832487852), 
([[78, 169], [586, 169], [586, 261], [78, 261]], ‘FOR COMPUTER VISION‘, 0.9504105724045384)]

Each detection is a tuple of the format (box, text, confidence):

  • box: Coordinates of the bounding box as a list of four corner points
  • text: The transcribed text
  • confidence: Confidence score between 0 and 1

To extract just the text itself:

text = [det[1] for det in result]
print(text)

Output:

[‘DEEP LEARNING‘, ‘FOR COMPUTER VISION‘]

Step 5: Draw detections on image

For visualization purposes, we can draw the detection bounding boxes and text on the original input image using OpenCV:

img = cv2.imread(IMAGE_PATH)

for (box, text, conf) in result:
    # Extract corner coordinates
    (tl, tr, br, bl) = box
    tl = (int(tl[0]), int(tl[1]))
    tr = (int(tr[0]), int(tr[1]))
    br = (int(br[0]), int(br[1]))
    bl = (int(bl[0]), int(bl[1]))

    # Draw bounding box and text
    cv2.rectangle(img, tl, br, (0, 255, 0), 2)
    cv2.putText(img, text, (tl[0], tl[1] - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)

plt.imshow(img)
plt.show()

This will display the image with green bounding boxes around the detected text regions, with the transcribed text in blue above each box.

Some things to note:

  • To draw on the image we first load it with cv2.imread()
  • The bounding box coordinates are accessed through unpacking the box element
  • cv2.rectangle() draws the actual box given the top-left and bottom-right coordinates
  • cv2.putText() draws the text at a specified location, here 10 pixels above the top-left corner

Advanced Usage

Those are the basics of performing text detection with EasyOCR! Some more advanced things to be aware of:

Specifying target language(s)

We‘ve seen that you can pass a list of language codes when initializing the Reader. This allows you to do multi-language detection by passing all the desired languages:

reader = easyocr.Reader([‘en‘, ‘fr‘, ‘de‘, ‘es‘])

However, specifying only the expected languages can greatly improve speed and accuracy, as the model can ignore irrelevant characters. EasyOCR will autodetect which of the given languages is present per text line.

Setting GPU flag

If you have a CUDA-enabled GPU, you can enable it when initializing the Reader to greatly speed up detection:

reader = easyocr.Reader([‘en‘], gpu=True) 

The GPU flag defaults to False if not specified.

Detecting non-horizontal text

EasyOCR has special models for detecting text at irregular angles. To enable this, pass detect_orientation=True to readtext():

result = reader.readtext(IMAGE_PATH, detect_orientation=True)

This will automatically correct the orientation of the bounding boxes and transcribed text.

Paragraph detection

By default, EasyOCR returns individual text lines. To combine adjacent lines into paragraphs, pass paragraph=True to readtext():

result = reader.readtext(IMAGE_PATH, paragraph=True)

The output will be have text grouped into complete paragraphs instead of separate lines.

When to use EasyOCR

With its extensive language support, high accuracy, and ease of use, EasyOCR is an excellent choice for a wide range of OCR tasks. Some ideal use cases are:

  • Extracting text from scanned documents or images
  • Digitizing receipts, invoices, or forms
  • Indexing text information within large image datasets
  • Performing OCR on mobile devices (e.g. as part of a translation app)
  • Automating data entry workflows

While EasyOCR works well out-of-the-box for most applications, the underlying models can also be fine-tuned on custom datasets to improve accuracy on specific text domains.

Comparisons to Other OCR Tools

There are a number of open-source and commercial OCR tools available, each with their own strengths and use cases. Some leading alternatives to EasyOCR are:

  • Google Tesseract: Arguably the most well-known open source OCR engine, with support for over 100 languages. Tesseract is highly accurate but can be more complex to configure and use compared to EasyOCR.

  • Cloud Vision API: Google‘s cloud OCR offering, which provides both text detection and document parsing via a web API. Provides high accuracy and ease of use, but requires an internet connection and usage is charged based on requests.

  • ABBYY FineReader: A commercial desktop OCR application with support for 190 languages. Provides a graphical user interface and support for more complex document layouts, but requires a paid license.

  • Amazon Textract: Amazon‘s cloud OCR service that provides text detection, form extraction, and table parsing. Has prebuilt models for specific domains like receipts and invoices.

Compared to cloud APIs, EasyOCR has the advantage of being free, private, and usable offline. It also offers more customization through swappable detection and recognition models.

Overall, EasyOCR provides an excellent balance of simplicity, flexibility, and performance, making it one of the top choices for general purpose OCR.

Conclusion

In this guide, we‘ve seen how to leverage the EasyOCR Python library to perform accurate text detection and extraction in just a few lines of code. OCR is a key technology that helps bridge the gap between the physical and digital world, unlocking text information from unstructured image data.

EasyOCR stands out for its extensive language support, simple API, modularity, and ability to handle text in the wild. By following the steps outlined, you‘re now equipped to apply OCR to your own projects and datasets.

Some potential next steps to further enhance your OCR pipeline could be:

  • Fine-tuning the detection and recognition models on your own data
  • Combining OCR with other computer vision techniques like object detection or document classification
  • Optimizing performance through GPU acceleration or cropping irrelevant image regions
  • Building an end-to-end application that ingests documents and extracts structured information

The field of OCR is rapidly evolving, with new approaches using transformers and self-supervised learning helping push accuracy and robustness to new heights. With tools like EasyOCR putting state-of-the-art models at your fingertips, it‘s an exciting time to be working with text recognition.

I hope this guide has been helpful in getting you started with EasyOCR and OCR in general. For more computer vision tutorials and guides, be sure to check out the Analytics Vidhya blog. Happy text extracting!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts