Optical Character Recognition (OCR) is a technology used to extract text from images which is used in applications like document digitization, license plate recognition and automated data entry. In this article, we explore how to detect and extract text from images usingOpenCVfor image processing andTesseract OCRfor text recognition.
Before we start we need to install required libraries using following commands:
- !pip install opencv-python
- !pip install pytesseract
- !sudo apt-get install tesseract-ocr
Step 1: Importing Required Packages
Import the required Python libraries likeOpenCV,pytesseractandmatplotlib.
Pythonimportcv2importpytesseractfrommatplotlibimportpyplotasplt
Step 2: Loading Image
Now we will load the image usingcv2.imread()function. OpenCV loads images in BGR format but matplotlib expects RGB format so we convert it usingcv2.cvtColor().
Pythonimage_path="/mnt/data/08d702e9-fb88-4a5b-98ce-9ad0bc19afd9.png"image=cv2.imread(image_path)image_rgb=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
Step 3: Converting Image to Grayscale
Convert the input image to grayscale to simplify the image and remove color information.
Pythongray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)print("Grayscale Image:")cv2_imshow(gray)
Step 4:Displaying Original Image
Let's see original image with the help of matplotlib.
Pythonplt.figure(figsize=(10,6))plt.imshow(image_rgb)plt.title("Original Image")plt.axis("off")plt.show()
Output:
Sample ImageStep 5: Extracting Text from Image
In this step we extract text from the image with the help ofpytesseract.image_to_string().
Pythonextracted_text=pytesseract.image_to_string(image_rgb)print(" Extracted Text:\n")print(extracted_text)
Output:
extracted textStep 6: Drawing Bounding Boxes Around Detected Text
Now we loop through each detected word and draw ablue box around it usingcv2.reactangle.
Pythondata=pytesseract.image_to_data(image_rgb,output_type=pytesseract.Output.DICT)n_boxes=len(data['level'])foriinrange(n_boxes):(x,y,w,h)=(data['left'][i],data['top'][i],data['width'][i],data['height'][i])cv2.rectangle(image_rgb,(x,y),(x+w,y+h),(255,0,0),2)
Step 7: Showing Image with Bounding Boxes
Finally this shows the processed image with bounding boxes around the detected text.
Pythonplt.figure(figsize=(10,6))plt.imshow(image_rgb)plt.title("Image with Text Bounding Boxes")plt.axis("off")plt.show()
Output:
Output ImageGet complete notebook link here :
Notebook:click here.
Sample Image:click here.
Text Detection and Extraction using OpenCV and OCR