diff --git a/aoe_recognition.py b/aoe_recognition.py index 33931ef..c833e19 100644 --- a/aoe_recognition.py +++ b/aoe_recognition.py @@ -6,15 +6,32 @@ import random import win32api, win32con import time from PIL import Image, ImageDraw +import aoe_recognition_ocr import aoe_commands DELTA = 1 +class InfoHolder: + def __init__(self): + self.data = {} + +INFO = InfoHolder() + +MAPSIZES = { + 'WINZIG': 120, + 'KLEIN': 144, + 'MITTEL': 168, + 'NORMAL': 200, + 'GROSS': 220, + 'RIESIG': 240 +} + def wait_for_start(): start_started = False while 1: if pyautogui.locateOnScreen('images/startscreen.png', confidence=0.9) != None: print("Startscreen found") + eval_startscreen() start_started = True time.sleep(0.5) elif start_started: @@ -24,6 +41,17 @@ def wait_for_start(): print("Startscreen not found") time.sleep(0.5) +def eval_startscreen(): + start_info = aoe_recognition_ocr.recognize_text(pyautogui.screenshot()) + + + mapsizerec = re.findall(r'KARTENGROSSE +(\w+) ', start_info) + if len(mapsizerec) == 1 and mapsizerec[0] in MAPSIZES.keys(): + INFO.data['mapsize'] = MAPSIZES[mapsizerec[0]] + print("Kartengroeße erkannt:", INFO.data['mapsize']) + else: + print("Startscreen text: ", start_info) + def find_top(image,x,y,classifyer): left = (x,y) print(f"xy: {x},{y}") @@ -121,6 +149,10 @@ def find_town_center_in_map(): return (left + 1, top + 1) return (0,0) +def scale_to_minimap_size(pixels): + "Use MITTEL sized maps for inputs" + return int(pixels / (INFO.data["mapsize"] / (MAPSIZES["MITTEL"]))) + def initial_scout_trace(): walk_further_out = 3 outer_circle = [(1730, 1072), (1570,986),(1730, 898), (1890, 984), (1730, 1072)] @@ -138,7 +170,7 @@ def initial_scout_trace(): by = find_bottom(image, x, y, not_black) print(f"Town center x: {x}, y:{y} gave us minimal and maximal x values to watch: lx: {lx}, rx={rx}") scout_trace = [] - delta = 4 + delta = scale_to_minimap_size(4) for yy in range(by, ty, -5): xx = find_left(image, x, yy, not_black) - delta scout_trace.append((xx,yy)) @@ -152,7 +184,7 @@ def initial_scout_trace(): yy = find_bottom(image, xx, y, not_black) + delta scout_trace.append((xx,yy)) - delta = 12 + delta = scale_to_minimap_size(12) for yy in range(by, ty, -5): xx = find_left(image, x, yy, not_black) - delta scout_trace.append((xx,yy)) @@ -166,7 +198,7 @@ def initial_scout_trace(): yy = find_bottom(image, xx, y, not_black) + delta scout_trace.append((xx,yy)) - delta = 20 + delta = scale_to_minimap_size(20) for yy in range(by, ty, -5): xx = find_left(image, x, yy, not_black) - delta scout_trace.append((xx,yy)) diff --git a/aoe_recognition_ocr.py b/aoe_recognition_ocr.py new file mode 100644 index 0000000..fab61df --- /dev/null +++ b/aoe_recognition_ocr.py @@ -0,0 +1,28 @@ +# C:\Program Files\Tesseract-OCR\tesseract.exe +import os +os.environ['path'] += ";C:\\Program Files\\Tesseract-OCR" + +import cv2 +import pytesseract +import numpy as np +import re + +# https://towardsdatascience.com/optical-character-recognition-ocr-with-less-than-12-lines-of-code-using-python-48404218cccb +def recognize_text(img_pil): + #img = cv2.imread('ocr_test.png') + img = np.asarray(img_pil) + gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) + gray, img_bin = cv2.threshold(gray,128,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU) + gray = cv2.bitwise_not(img_bin) + + kernel = np.ones((2, 1), np.uint8) + img = cv2.erode(gray, kernel, iterations=1) + img = cv2.dilate(img, kernel, iterations=1) + out_below = pytesseract.image_to_string(img) + out_below = re.sub(r'[^a-zA-Z0-9_\s]', '', out_below) + out_below = re.sub(r'[\s]', ' ', out_below).strip() + + print("OCR OUTPUT:", out_below) + return out_below + +#recognize_text(cv2.imread('ocr_test.png')) \ No newline at end of file diff --git a/readme.md b/readme.md index 45b37cb..a73f95c 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,7 @@ Preparation - pip install keyboard - pip install pyautogui - pip install opencv-python + - pip install pytesseract Start @@ -33,3 +34,4 @@ Interesting Links - [Draw Image Example](http://effbot.org/imagingbook/imagedraw.htm) - [AoE Hotykey editor](https://aokhotkeys.appspot.com/editor) - [Structureing your python project](https://python-docs.readthedocs.io/en/latest/writing/structure.html) +- [Download Tesseract for Windows](https://github.com/UB-Mannheim/tesseract/wiki)