Search code examples
pythonocrtesseract

Tesseract doesn't recognize certain pictures. Python


Tesseract works fine when I use other pictures but whenever I use this picture it doesn't recognize the picture. Can someone explain me why please?

import cv2
import pytesseract
import time
import random
from pynput.keyboard import Controller

keyboard = Controller()  # Create the controller

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

img = cv2.imread("capture5.png")
#img = cv2.resize(img, (300, 300))
cv2.imshow("capture5", img)
text = pytesseract.image_to_string(img)
print(text)

cv2.waitKey(0)

cv2.destroyAllWindows()

Solution

  • I fixed my problem, all I needed to do was add this code to my script.

    text = pytesseract.image_to_string(
     img, config=("-c tessedit"
                  "_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                  " --psm 10"
                  " "))