Nelson Izah
Nelson Izah
All writing
4 min readCOMPUTER VISION & AI

Text Detection in Images with EasyOCR in Python

Text Detection in Images with EasyOCR in Python

Optical character recognition allows computers to identify text in images and convert it into machine-readable format. How to use EasyOCR in Python to detect and annotate text regions with OpenCV bounding boxes.

Optical character recognition (OCR) is an important technology that allows computers to identify text in images and convert it into machine-readable text. This enables the extraction of text from scanned documents, photos, screenshots, and more for further natural language processing.

In this article, we will use the easyocr Python library to detect and recognize text in images. easyocr provides a simple API for OCR that does not require training a model. It is built on top of PyTorch and TensorFlow and can detect text in over 80 languages out-of-the-box.


Importing Libraries

We first import the necessary libraries:

PYTHON
import cv2
import easyocr
import matplotlib.pyplot as plt
  • cv2: OpenCV library for image processing and computer vision
  • easyocr: Library for optical character recognition
  • matplotlib.pyplot: Library for visualization and plotting

Defining Util Functions

We define a helper function to draw bounding boxes around detected text and display the image:

PYTHON
def draw_bounding_boxes(image, detections, threshold=0.25):
    for bbox, text, score in detections:
        if score > threshold:
            cv2.rectangle(
                image, 
                tuple(map(int, bbox[0])), 
                tuple(map(int, bbox[2])), 
                (0, 255, 0), 
                5
            )
            cv2.putText(
                image, 
                text, 
                tuple(map(int, bbox[0])), 
                cv2.FONT_HERSHEY_COMPLEX_SMALL, 
                0.65, 
                (255, 0, 0), 
                2
            )

The draw_bounding_boxes function takes the input image, the detected text from EasyOCR, and an optional threshold score as arguments:

  • It loops through each detected text region returned by EasyOCR, which contains the bounding box coordinates, the recognized text, and confidence score for that detection.
  • For each detection, it checks if the confidence score exceeds the threshold we defined (default 0.25). This filters out weak or dubious detections.
  • For detections that pass the threshold:
    • Draws a green bounding box on the input image using cv2.rectangle() to highlight the text region.
    • Annotates the bounding box with the detected text in red color using cv2.putText().
    • Converts the floating-point bbox coordinates to integers using map(int, bbox[0]) and bbox[2] so they can be processed by OpenCV.

Loading the Image

We load the input image to run OCR on:

PYTHON
image_path = "image/preview.jpg"
img = cv2.imread(image_path)

if img is None:
    raise ValueError("Error loading the image. Please check the file path.")
  • First, we define the path to the input image file (image/preview.jpg).
  • We use OpenCV’s imread() function to load the image. imread() loads the image in BGR format by default.
  • We add a validation check: if img is None, we raise a ValueError with a descriptive error message to ensure the program exits gracefully rather than crashing downstream.

Running Text Detection

We instantiate the EasyOCR reader to detect English text and set gpu=False for CPU inference (you can set gpu=True if you have a CUDA-enabled GPU for faster batch throughput):

PYTHON
reader = easyocr.Reader([en], gpu=False)

Then we call .readtext() to run text detection and recognition on the image:

PYTHON
text_detections = reader.readtext(img)

This returns a list of tuples for each detected text region containing:

  1. Four bounding box coordinate pairs [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
  2. The recognized text string
  3. The detection confidence score (0.0 to 1.0)

Visualizing the Output

Finally, we call our draw bounding boxes function and display the image with Matplotlib:

PYTHON
threshold = 0.25
draw_bounding_boxes(img, text_detections, threshold)

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGBA))
plt.show()

EasyOCR Text Detection Output

The green bounding boxes illustrate the detected text regions, with the recognized text overlaid above each segment.

And that’s it! With just a few lines of code, we can run performant text detection and OCR on images using EasyOCR in Python. The library handles model architecture and neural network inference behind the scenes while providing a straightforward API for any computer vision pipeline.


Alternative for .NET: IronOCR

For developers working in the .NET ecosystem, IronOCR offers a similar experience with bounding box coordinates and confidence scores:

CSHARP
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("image.png");

var result = ocr.Read(input);

foreach (var word in result.Words)
{
    Console.WriteLine($"Text: {word.Text}, Confidence: {word.Confidence}");
    Console.WriteLine($"Bounding Box: {word.X}, {word.Y}, {word.Width}, {word.Height}");
}

The library returns text regions with coordinates and confidence scores, making it easy to filter weak detections and draw bounding boxes without needing PyTorch or TensorFlow dependencies.

Nelson Izah

Written by

Nelson Izah

I write about geology, software engineering, and the spaces where they intersect. When I'm not writing or building, you can find me exploring the outdoors or reading a good book.

Share this piece

Optical character recognition allows computers to identify text in images and convert it into machine-readable format. How to use EasyOCR in Python to detect and annotate text regions with OpenCV bounding boxes.

Loading...

Enjoyed this piece?

Leave a reaction to let the author know what resonated with you.

Share Your Thoughts

What resonated with you? I'd love to hear your perspective.

Be kind. Be thoughtful.