Skip to content

Commit

Permalink
Use threads to compare images. Add a few images for different resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWithMa committed Nov 16, 2024
1 parent 3d152d2 commit 699cb94
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 5 deletions.
Binary file added images/claim_rewards_button_text2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/max_number_of_games_played_text2.en.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/select_league_limited.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/start_button_text6.en.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ def run():
elif next_action.action == GameActions.exit_program:
turn_screen_off()
logging.info("Max number of games played. Exit program.")
sys.exit(1)
sys.exit()

time.sleep(2)
2 changes: 1 addition & 1 deletion src/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ATTACK_TAP_POSITION = (500, 1400)
SCREENSHOT_FILE_NAME = "screenshot.png"
SCREENSHOT_FILE_NAME = "/tmp/screenshot.png"
25 changes: 22 additions & 3 deletions src/image_decision_maker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import cv2
import os
import threading

from src import constants
from src import image_service
Expand All @@ -27,13 +28,31 @@ def make_decision(template_images: dict[str, cv2.Mat], image_name: str) -> GameA

# Check if any of the image files match the screenshot
find_image_results: list[tuple[str, FindImageResult]] = []
for image_file, img_template in template_images.items():

# Compare each image in its own thread
def compare_image(image_file, img_screenshot, img_template):
result = image_service.find_image(img_screenshot, img_template)
if result:
logging.debug(f"Image {image_file} matches with {result.val * 100}%")
if result.val > 0.90:
find_image_results.append((image_file, result))

threads = []
for image_file, img_template in template_images.items():
thread = threading.Thread(target=compare_image, args=(image_file, img_screenshot, img_template))
thread.start()
threads.append(thread)

for thread in threads:
thread.join()

# for image_file, img_template in template_images.items():
# result = image_service.find_image(img_screenshot, img_template)
# if result:
# logging.debug(f"Image {image_file} matches with {result.val * 100}%")
# if result.val > 0.90:
# find_image_results.append((image_file, result))

logging.debug("Found images over threshold:")
logging.debug(find_image_results)
return analyze_results_and_return_action_with_priority(find_image_results)
Expand All @@ -47,7 +66,7 @@ def analyze_results_and_return_action_with_priority(
return GameAction()

priority_list = [
"max_number_of_games_played_text.",
"max_number_of_games_played_text",
"reward_",
"start_button_text",
"select_master",
Expand All @@ -73,7 +92,7 @@ def analyze_results_and_return_action(
) -> GameAction:
logging.info(f"Image {image_file} matches with {find_image_result.val * 100}%")

if image_file.startswith("max_number_of_games_played_text."):
if image_file.startswith("max_number_of_games_played_text"):
return GameAction(action=GameActions.exit_program)

# If ingame return is_ingame with true
Expand Down
1 change: 1 addition & 0 deletions src/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ def capture_screenshot(filename: str) -> bool:
else:
adb_command = f"adb exec-out screencap -p > {filename} 2> /dev/null"
error_code = os.system(adb_command)
# os.system(f"mogrify -resize 25% {filename}")
return error_code == 0

0 comments on commit 699cb94

Please sign in to comment.