我正在创建一个程序,可以从https://play2048.co/捕获2048游戏,并使用pytesseract打印数字。我的代码使用cv2来创建数字的黑白蒙版:
当我运行程序时,窗口会正确显示数字,但tesseracct无法识别数字。由于某种原因,它只能识别此image中的数字,但只能识别几个数字,包括字母。这是我的完整代码:
import numpy as np
import cv2
from PIL import ImageGrab, Image
import time
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def process_img(original_image):
hsv = cv2.cvtColor(original_image, cv2.COLOR_BGR2HSV)
lower_grey = np.array([100, 30, 110])
upper_grey = np.array([110, 50, 120])
lower_white = np.array([0, 0, 235])
upper_white = np.array([110, 10, 255])
mask1 = cv2.inRange(hsv, lower_grey, upper_grey)
mask2 = cv2.inRange(hsv, lower_white, upper_white)
mask = cv2.bitwise_or(mask1, mask2)
return mask
def main():
last_time = time.time()
numList = []
while True:
screen = np.array(ImageGrab.grab(bbox=(1194,386, 1694, 886)))
new_screen = process_img(screen)
new_screen = cv2.bitwise_not(new_screen)
frame = cv2.cvtColor(new_screen, cv2.COLOR_RGB2BGR)
print('Loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
cv2.imshow('window', new_screen)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
cv2.imwrite("./frame.png", frame)
print(pytesseract.image_to_string('frame.png'))
main()