Search code examples
pythonnumpyocropencvpython-tesseract

Python Real Time OCR With OpenCV and pytesseract


I am just starting out on python and I am attempting to create a code that does real-time OCR on a portion of my screen. I was certain this code would work, but it just throws me a bunch of tesseract errors. Does the image need to be saved for Tesseract to work? Is there a better OCR library for this task? The OpenCV part works perfectly and displays the image.

import numpy as np
import cv2
from PIL import ImageGrab
import pytesseract

while True:
    orig_img = ImageGrab.grab(box)

    np_im = np.array(orig_img)

    img = cv2.cvtColor(np_im, cv2.COLOR_BGR2GRAY)

    text = pytesseract.image_to_string(img)

    cv2.imshow('window',img)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        print(text)

Solution

  • I fixed it. I was not aware that I needed to install tesseract in my pc. I also added

    im = Image.fromarray(img)
    
    im.save("img.png")
    

    to save the image