How to Recognize Optical Characters in Images in Python?
Prerequisite: Pytesseract, OpenCV
In this article, we are going to recognize the character from the images and get text data out of an image. Let’s take a quick introduction to the required module.
- OpenCV: It is a Python module in which we can do image processing, video capture, and some analysis tools like face detection or object detection. It has C++, Python, Java, and MATLAB interfaces and supports Windows, Linux, Android, and Mac OS.
- Pytesseract: Python-tesseract is an optical character recognition (OCR) tool for Python. That is, it will recognize and “read” the text embedded in images. Python-tesseract is a wrapper for Google’s Tesseract-OCR Engine. It is also useful as a stand-alone invocation script to tesseract, as it can read all image types supported by the Pillow and Leptonica imaging libraries, including jpeg, png, gif, BMP, tiff, and others. Additionally, if used as a script, Python-tesseract will print the recognized text instead of writing it to a file.
Approach:
- Import required module
- Setting the path of pytesseract.
- Read the image with cv2.imread()
- Convert image in RGB format.
- Extract text from image using pytesseract.image_to_string(img).
We are going to use this image for a demonstration:
Below is the full implementation:
Python3
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
img = cv2.imread( 'photo.jpg' )
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print (pytesseract.image_to_string(img))
cv2.imshow( "result" , img)
cv2.waitKey( 0 )
|
Output:
GeeksforGeeks
Last Updated :
08 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...