Text Recognition in Images with OpenCV and EasyOCR
This Python script demonstrates how to perform text recognition in image files using the combined power of OpenCV, EasyOCR, and Matplotlib.
The process begins by loading an image with text recognition OpenCV techniques, then detecting and extracting text using EasyOCR’s pre-trained English model.
The detected text is displayed with bounding boxes and annotations directly on the image, filtered by a minimum confidence threshold.
Finally, the script saves the processed image and displays it for visual verification, making it an ideal starting point for anyone exploring computer vision and OCR applications.
You can find more similar tutorials in my blog posts page here : https://eranfeit.net/blog/
Check out our video here : https://youtu.be/DycbnT_pWKw&list=UULFTiWJJhaH6BviSWKLJUM9sg
Full Code for this video : https://ko-fi.com/s/39a68c8806
Step 1 — Setting Up the Environment for Text Recognition with OpenCV and EasyOCR :
This setup guide walks you through creating a dedicated Conda environment for running text recognition projects using OpenCV and EasyOCR.
It starts by installing Python 3.9, checking your CUDA version, and configuring PyTorch with GPU acceleration.
The process continues with installing essential libraries such as OpenCV for image processing, EasyOCR for optical character recognition, and Matplotlib for visualizing results.
This environment ensures smooth performance and compatibility when building and running text detection applications.
/
conda create -n chrRecog-39 python=3.9 conda activate chrRecog-39 # What is my Cuda version nvcc --version # https://pytorch.org/ conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia pip install opencv-python==4.5.4.60 pip install easyocr==1.6.2 pip install matplotlib
Full Code for this video : https://ko-fi.com/s/39a68c8806
Step 2 -Text Recognition in Images Using OpenCV and EasyOCR :
This Python code demonstrates a complete workflow for performing text recognition in images using text recognition OpenCV techniques combined with EasyOCR.
The script loads an image, applies EasyOCR’s pre-trained English model to detect text, and filters results based on a confidence threshold.
Detected text is highlighted with bounding boxes and overlaid annotations directly on the image.
The final processed image is saved for later use and displayed using Matplotlib, providing a clear visualization of the recognition results.
This approach is ideal for beginners and professionals exploring optical character recognition in Python.
# Importing the required libraries import cv2 # OpenCV library for image processing import matplotlib.pyplot as plt # Matplotlib for displaying images import easyocr # EasyOCR for Optical Character Recognition # Define the path to the image file path = "Open-CV/Optical character recognition/sign.jpg" # Read the image from the specified path using OpenCV img = cv2.imread(path) # Create an EasyOCR reader object for English language text detection reader = easyocr.Reader(['en']) # Detect text on the image using EasyOCR # The readtext() method returns a list of detected text information myText = reader.readtext(img) # Print the list of detected text details print(myText) # Set the minimum confidence score for displaying detected text minThresholdForDisplay = 0.25 # Loop through each detected text item and its index for numerator, detectedText in enumerate(myText): # Print the details of the detected text print(detectedText) # Extract the bounding box coordinates, the detected text string, and the confidence score bbox, text, score = detectedText pos1 = bbox[0] # Top-left corner of the bounding box pos2 = bbox[2] # Bottom-right corner of the bounding box # If the confidence score is above the minimum threshold, draw the bounding box and text if score > minThresholdForDisplay: # Draw a rectangle around the detected text on the image cv2.rectangle(img, pos1, pos2, (0, 0, 0), 5) # Overlay the detected text on the image cv2.putText(img, text, pos1, cv2.FONT_HERSHEY_PLAIN, 5, (255, 0, 0), 5) # Save the modified image with the detected text annotations to a file cv2.imwrite("c:/temp/opticalRecog.png", img) # Convert the image from BGR (OpenCV default) to RGB for correct color display # Then, display the image using Matplotlib plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.show()
Full Code for this video : https://ko-fi.com/s/39a68c8806
Here is the result :
Connect :
You can find more similar tutorials in my blog posts page here : https://eranfeit.net/blog/
☕ Buy me a coffee — https://ko-fi.com/eranfeit
🖥️ Email : feitgemel@gmail.com
🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb
Enjoy,
Eran