add function to check position and find all matches on screen
This commit is contained in:
+25
-2
@@ -16,7 +16,7 @@ except ImportError as e:
|
|||||||
class Command:
|
class Command:
|
||||||
"""Dataclass for commands"""
|
"""Dataclass for commands"""
|
||||||
__AUTHOR__ = 'anima'
|
__AUTHOR__ = 'anima'
|
||||||
__VERSION__ = '1.0.0'
|
__VERSION__ = '1.1.3'
|
||||||
|
|
||||||
image_dir: Path = Path('images/')
|
image_dir: Path = Path('images/')
|
||||||
supported_extensions = {".png", ".jpg", ".jpeg", ".bmp"}
|
supported_extensions = {".png", ".jpg", ".jpeg", ".bmp"}
|
||||||
@@ -63,4 +63,27 @@ class Command:
|
|||||||
|
|
||||||
return self.valid
|
return self.valid
|
||||||
|
|
||||||
def check_position(self, )
|
def check_position(self, position: tuple) -> bool:
|
||||||
|
"""check if image on expected position
|
||||||
|
|
||||||
|
Args:
|
||||||
|
position (tuple): expected x, y position
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if image on expected position
|
||||||
|
"""
|
||||||
|
if self.position is None:
|
||||||
|
logging.debug('no expected position set, skip check')
|
||||||
|
return True
|
||||||
|
if not isinstance(position, tuple):
|
||||||
|
logging.debug(f'position check get wrong value {position=} ({type(position)})')
|
||||||
|
return False
|
||||||
|
i_x, i_y = self.position
|
||||||
|
p_x, p_y = position
|
||||||
|
|
||||||
|
if (i_x - self.pos_dif) < int(p_x) < (i_x + self.pos_dif) and \
|
||||||
|
(i_y - self.pos_dif) < int(p_y) < (i_y + self.pos_dif):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logging.debug(f'image is not on expected {position=}')
|
||||||
|
return False
|
||||||
+15
-8
@@ -21,7 +21,7 @@ from .command import Command
|
|||||||
|
|
||||||
class Window:
|
class Window:
|
||||||
__AUTHOR__ = 'anima'
|
__AUTHOR__ = 'anima'
|
||||||
__VERSION__ = '1.0.0'
|
__VERSION__ = '1.1.6'
|
||||||
|
|
||||||
key_press_duration = 0.1
|
key_press_duration = 0.1
|
||||||
supported_keys = ['return', 'Up', 'Down', 'Right', 'Left']
|
supported_keys = ['return', 'Up', 'Down', 'Right', 'Left']
|
||||||
@@ -95,13 +95,20 @@ class Window:
|
|||||||
|
|
||||||
img_gray = cv2.cvtColor(command.image, cv2.COLOR_BGR2GRAY)
|
img_gray = cv2.cvtColor(command.image, cv2.COLOR_BGR2GRAY)
|
||||||
result = cv2.matchTemplate(screen_gray, img_gray, cv2.TM_CCOEFF_NORMED)
|
result = cv2.matchTemplate(screen_gray, img_gray, cv2.TM_CCOEFF_NORMED)
|
||||||
_, match_value, _, match_pos = cv2.minMaxLoc(result)
|
|
||||||
|
locations = numpy.where(result >= command.threshold)
|
||||||
if match_value >= command.threshold and match_value > best_value:
|
matches = list(zip(*locations[::-1])) # [(x1,y1), (x2,y2), ...]
|
||||||
logging.debug(f'new best match: {command.name} ({match_value=}%, {match_pos=})')
|
|
||||||
best_match = command
|
for match in matches:
|
||||||
best_value = match_value
|
if command.check_position(match):
|
||||||
best_pos = match_pos
|
print('match!')
|
||||||
|
match_value = float(result[match[1], match[0]])
|
||||||
|
if match_value > best_value:
|
||||||
|
best_match = command
|
||||||
|
best_value = match_value
|
||||||
|
best_pos = match
|
||||||
|
logging.debug(f'new best match: {command.name} ({match_value=}%, {match=})')
|
||||||
|
break
|
||||||
|
|
||||||
if best_match:
|
if best_match:
|
||||||
return best_match
|
return best_match
|
||||||
|
|||||||
Reference in New Issue
Block a user