Optical Character Recognition (OCR)

Optical Character Recognition (OCR)

Optical Character Recognition (OCR)

A guide to converting text from images into machine-encoded text using an OCR engine like Tesseract.

Libraries and Tools Used

This project uses a combination of image processing and a dedicated OCR engine:

  • pytesseract: A Python wrapper for Google's Tesseract OCR engine.
  • cv2 (OpenCV): For loading and preprocessing the image to improve OCR accuracy.
  • Pillow (PIL): A library for handling image files, often used by `pytesseract`.

1. Image Preprocessing

  • What the code does: It loads an image, converts it to grayscale, and applies a binary threshold to make the text stand out from the background.
  • Behind the scenes: OCR engines work best on clean, high-contrast images. Grayscale conversion simplifies the image data, and thresholding creates a black-and-white image, making it easier for Tesseract to distinguish between text and non-text pixels.
  • Abstraction: OpenCV's `cv2.cvtColor` and `cv2.threshold` functions abstract these complex image processing operations into a few simple lines.
# Install Tesseract engine on your system first, then:
# pip install pytesseract opencv-python Pillow
import cv2
import pytesseract
from PIL import Image

# Set the path to the Tesseract executable
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# Load the image
image_path = 'sample_text_image.png'
image = cv2.imread(image_path)

# Convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply a binary threshold
# This is a simple threshold. More advanced methods may be needed.
_, binary_image = cv2.threshold(gray_image, 150, 255, cv2.THRESH_BINARY)

print("Image loaded and preprocessed.")

2. Extracting Text from the Image

  • What the code does: It uses `pytesseract` to extract all the text from the preprocessed image.
  • Behind the scenes: Tesseract's engine takes the image and applies its internal algorithms to find text, recognize characters, and assemble them into words and sentences.
  • Abstraction: `pytesseract.image_to_string` is a powerful function that abstracts the entire OCR process, returning a simple string of the recognized text.
# Use pytesseract to extract text from the binary image
extracted_text = pytesseract.image_to_string(binary_image)

print("\nExtracted text:")
print(extracted_text)

3. Drawing Bounding Boxes Around Text

  • What the code does: It gets more detailed data from Tesseract, including the bounding box coordinates for each word, and then uses OpenCV to draw these boxes on the original image for visualization.
  • Behind the scenes: `pytesseract.image_to_data` returns a pandas DataFrame with detailed information about each detected word, including its position, confidence, and text content.
  • Abstraction: The `image_to_data` function provides structured output, and OpenCV's `cv2.rectangle` function makes it easy to visualize these results on the image.
import pandas as pd

# Get detailed box and text data
data = pytesseract.image_to_data(binary_image, output_type=pytesseract.Output.DATAFRAME)

# Iterate through the data and draw bounding boxes
for i, row in data.iterrows():
    if row['conf'] > 80: # Only draw boxes for high confidence words
        x, y, w, h = row['left'], row['top'], row['width'], row['height']
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display the image with the bounding boxes
cv2.imshow('OCR Bounding Boxes', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Comments

Popular posts from this blog

Simple Augmented Reality (AR) App

Image Captioning

Pose Estimation