V
Vanessa
Guest
Vanessa Asks: pytesseract won't recognize numbers with less than 3 digits
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:
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:
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))
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.