I am writing a program aimed at recognizing numbers on a screenshot and filling an array with them. Generally, the program works well, but sometimes problems occur: ones either are not recognized or are recognized as fours. I am attaching the program code and the script execution result, where you can see that the number 100,000 was recognized as 00000. I have tried using different –oem and –psm parameters, but the errors persist in any case.
import cv2
import pytesseract
import re
import pyautogui
import numpy as np
pytesseract.pytesseract.tesseract_cmd = r'C:Program FilesTesseract-OCRtesseract.exe'
def screen():
screen_for_array = pyautogui.screenshot()
screen = np.array(screen_for_array)
screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
return screen
def recognize_numbers(image, x, y, w, h):
roi = image[y:y+h, x:x+w]
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
cv2.imshow('Image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789'
text = pytesseract.image_to_string(gray, config=custom_config)
numbers = re.findall(r'd+', text)
return numbers
image_path = screen()
x, y, w, h = 333, 380, 90, 344
numbers = recognize_numbers(image_path, x, y, w, h)
print("number recognition:", numbers)
enter image description here
The recognition result of this screenshot is as follows: [‘95000’, ‘97000’, ‘98111’, ‘98123’, ‘99999’, ‘00000’, ‘110000’, ‘120000’, ‘125000’, ‘149990’]
By the way, this image is the final version that the program uses for recognition.
I have tried using different –oem and –psm parameters, but the errors persist in any case. I used both the color image of the screenshot and the black-and-white one (which you can see in the attached screenshot).
Wlad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.