0
0
Computer-visionHow-ToBeginner ยท 3 min read

How to Use EasyOCR Python for Text Recognition in Computer Vision

Use easyocr.Reader to create a text recognition model and call readtext() on images to extract text. EasyOCR supports multiple languages and works well for computer vision tasks involving text detection and recognition.
๐Ÿ“

Syntax

The basic syntax involves creating a Reader object with the languages you want to recognize, then calling readtext() on an image path or image array.

  • easyocr.Reader(['en']): Initializes the OCR reader for English.
  • reader.readtext(image_path): Reads text from the image file.
  • The output is a list of tuples with bounding box, text, and confidence score.
python
import easyocr

# Initialize reader with English language
reader = easyocr.Reader(['en'])

# Read text from image file
results = reader.readtext('path_to_image.jpg')

# results example: [([[x1, y1], [x2, y2], [x3, y3], [x4, y4]], 'detected text', confidence_score)]
๐Ÿ’ป

Example

This example shows how to load an image, run EasyOCR to detect text, and print the results with confidence scores.

python
import easyocr

# Create reader for English
reader = easyocr.Reader(['en'])

# Path to your image file
image_path = 'sample_text_image.jpg'

# Perform OCR
results = reader.readtext(image_path)

# Print detected text and confidence
for bbox, text, conf in results:
    print(f'Text: {text}, Confidence: {conf:.2f}')
Output
Text: EasyOCR, Confidence: 0.98 Text: Python, Confidence: 0.95 Text: OCR, Confidence: 0.93
โš ๏ธ

Common Pitfalls

  • Not installing the required dependencies like torch and opencv-python causes errors.
  • Using incorrect language codes or missing languages in Reader initialization leads to poor results.
  • Passing invalid image paths or unsupported image formats causes failures.
  • Ignoring confidence scores can lead to trusting wrong text detections.

Always verify image path and install dependencies with pip install easyocr torch torchvision opencv-python.

python
import easyocr

# Wrong: Missing language or wrong language code
# reader = easyocr.Reader([])  # This will cause an error

# Right: Specify at least one valid language
reader = easyocr.Reader(['en'])
๐Ÿ“Š

Quick Reference

FunctionDescription
easyocr.Reader(['en'])Create OCR reader for English language
reader.readtext(image_path)Detect and extract text from image
results outputList of (bbox, text, confidence) tuples
confidence scoreFloat between 0 and 1 indicating detection certainty
โœ…

Key Takeaways

Initialize EasyOCR Reader with the correct language codes before reading images.
Use readtext() method to extract text and get bounding boxes with confidence scores.
Ensure all dependencies like torch and OpenCV are installed to avoid errors.
Check confidence scores to filter out unreliable text detections.
Pass valid image paths and supported image formats for successful OCR.