Python programming blog

Tuesday, June 22, 2021

OCR using Pytesseract library in python ( 20 lines )



This blog contains code for the text detection using open-cv and Pytesseract
 libraries in python


STEP1: Installation of libraries 

*pip install OpenCV-python

*pip install pillow

*pip install Pytesseract


STEP2: Import the libraries 

import cv2
from PIL import Image
from pytesseract import pytesseract


STEP3: Accessing webcam

camera=cv2.VideoCapture(0)


STEP4: The Loop

while True:
    _,image=camera.read()
    cv2.imshow('image',image)
    if cv2.waitKey(1)& 0xFF==ord('s'):
        cv2.imwrite('test1.jpg',image)
        break
camera.release()
cv2.destroyAllWindows()


STEP5: Converting Text to speech

def tesseract():
    path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    image_path = "test1.jpg"
    pytesseract.tesseract_cmd = path_to_tesseract
    text = pytesseract.image_to_string(Image.open(image_path))
    print(text[:-1])
tesseract()

Full code :GIthub




Thanks for reading this blog..!

No comments:

Post a Comment