Pytesseract or Python-tesseract is an Optical Character Recognition (OCR) tool for python. It will read and recognize the text in images, license plates, etc. Here, we will use the tesseract package to read the text from the given image.
Mainly, 3 simple steps are involved here as shown below:-
- Loading an Image saved from the computer or download it using a browser and then loading the same. (Any Image with Text).
- Binarizing the Image (Converting Image to Binary).
- We will then Pass the Image through the OCR system.
Implementation:
The following python code represents the Localizing of the Text and correctly guessing the text written in the image.
# We import the necessary packages #import the needed packages import cv2
import os,argparse
import pytesseract
from PIL import Image
#We then Construct an Argument Parser ap = argparse.ArgumentParser()
ap.add_argument( "-i" , "--image" ,
required = True ,
help = "Path to the image folder" )
ap.add_argument( "-p" , "--pre_processor" ,
default = "thresh" ,
help = "the preprocessor usage" )
args = vars (ap.parse_args())
#We then read the image with text images = cv2.imread(args[ "image" ])
#convert to grayscale image gray = cv2.cvtColor(images, cv2.COLOR_BGR2GRAY)
#checking whether thresh or blur if args[ "pre_processor" ] = = "thresh" :
cv2.threshold(gray, 0 , 255 ,cv2.THRESH_BINARY| cv2.THRESH_OTSU)[ 1 ]
if args[ "pre_processor" ] = = "blur" :
cv2.medianBlur(gray, 3 )
#memory usage with image i.e. adding image to memory filename = "{}.jpg" . format (os.getpid())
cv2.imwrite(filename, gray) text = pytesseract.image_to_string(Image. open (filename))
os.remove(filename) print (text)
# show the output images cv2.imshow( "Image Input" , images)
cv2.imshow( "Output In Grayscale" , gray)
cv2.waitKey( 0 )
|
Now, follow the below steps to successfully Read Text from an image:
- Save the code and the image from which you want to read the text in the same file.
- Open Command Prompt.Go to the location where the code file and image is saved.
- Execute the command below to view the Output.
Example 1:
Execute the command below to view the Output.
python tesseract.py --image Images/title.png
We have The Original Image displayed.
We have the GrayScale Image Displayed. (p.png)
Output:
Example 2:
Execute the command below to view the Output.
python tesseract.py --image Images/OCR.png
We have The Original Image displayed.
We have the GrayScale Image Displayed. (p.png)
Output: