我正在使用pytesseract和opencv能够相当实时地检测游戏中的数字。但是,这是非常不一致的。
我已经更改了许多设置,并对图像进行了很多改进,但是即使图像与以前一样,90%的时间仍然无法正常工作。
from PIL import ImageGrab, Image
import cv2
import time
import argparse
import os
import pytesseract
from ctypes import windll
user32 = windll.user32
user32.SetProcessDPIAware()
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def process_img(screen):
global filename
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--preprocess", type=str, default="thresh",
help="type of preprocessing to be done")
args = vars(ap.parse_args())
b = screen.copy()
resizedimg = cv2.resize(b, (750,500))
ret, thresh = cv2.threshold(resizedimg, 127, 255, cv2.THRESH_BINARY)
kernel = np.ones((3, 3), np.uint8)
img = cv2.erode(thresh, kernel, iterations=1)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if args["preprocess"] == "thresh":
img_gray = cv2.threshold(img_gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
elif args["preprocess"] == "blur":
img_gray = cv2.medianBlur(img_gray, 3)
blurred = cv2.GaussianBlur(img_gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200, 255)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, edged)
return edged
prevtext = int(100)
while True:
screen = np.array(ImageGrab.grab(bbox=(790, 1315, 839, 1345)))
# print('Frame took {} seconds'.format(time.time()-last_time))
last_time = time.time()
new_screen = process_img(screen)
cv2.imshow('window', new_screen)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
time.sleep(.5)
text = pytesseract.image_to_string(Image.open(filename), lang='eng',
config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789')
os.remove(filename)
print(text)
try:
int(text)
except:
text = prevtext
print("health is " + str(text))
print("previous health was " + str(prevtext))
if int(prevtext) > int(text):
print("Got hit")
prevtext = text
是否有办法使它变得更好(成功率至少75%)?