I'm trying to read some value displayed in a game. My script seems to only be able to recognize the number when it has at least 3 digits. If it has 2 or only 1 digit (which rarely happens so it would be okay if that won't work) it doesn't give an output. Here are some examples of numbers pytesseract can read 131, 148 . And here are some numbers pytesseract can not read 144, 86, 36, 24. For whatever reason it seems to have a problem with 144 as well tho it's a 3-digit number.
How can I improve my script so it can detect two digit numbers as well?
Thats my code:
import PIL.Image
import numpy
import pytesseract
import cv2
from PIL import ImageGrab
def optImage(image, showimage=False):
# apply grayscale
gry = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2GRAY)
# slim down digits
erd = cv2.erode(gry, None, iterations=3)
# apply threshold
ret, thr = cv2.threshold(erd, 160, 255, cv2.THRESH_BINARY_INV)
if showimage:
PIL.Image.fromarray(thr).show()
return gry
def imgToString(npArrayImg):
# Path of tesseract executable
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
tesstr = pytesseract.image_to_string(numpy.array(npArrayImg), lang='eng',
config='digits tessedit_char_whitelist=0123456789')
print(tesstr)
img = cv2.imread('train_tesseract\\train data\\power\\131.png')
img = cv2.resize(img, None, fx=4, fy=4, interpolation=cv2.INTER_CUBIC)
imgToString(optImage(img, True))