|
@@ -1,15 +1,16 @@
|
|
|
-import cv2
|
|
|
|
|
import time
|
|
import time
|
|
|
import datetime
|
|
import datetime
|
|
|
import numpy as np
|
|
import numpy as np
|
|
|
import io
|
|
import io
|
|
|
-from PIL import Image
|
|
|
|
|
-from io import BytesIO
|
|
|
|
|
from ppadb.client import Client as AdbClient
|
|
from ppadb.client import Client as AdbClient
|
|
|
from PIL import Image
|
|
from PIL import Image
|
|
|
-
|
|
|
|
|
-client = AdbClient(host="127.0.0.1", port=5037)
|
|
|
|
|
-device = client.device("192.168.178.32:5555")
|
|
|
|
|
|
|
+from helper import (
|
|
|
|
|
+ cv2,
|
|
|
|
|
+ find_template,
|
|
|
|
|
+ tap as tap_helper,
|
|
|
|
|
+ swipe as swipe_helper,
|
|
|
|
|
+ capture_current_screen,
|
|
|
|
|
+)
|
|
|
|
|
|
|
|
# templates
|
|
# templates
|
|
|
close_sub_fights = cv2.imread("templates/close_sub_fights.jpg")
|
|
close_sub_fights = cv2.imread("templates/close_sub_fights.jpg")
|
|
@@ -78,31 +79,9 @@ def non_max_suppression(boxes, overlapThresh):
|
|
|
return boxes[pick].astype("int")
|
|
return boxes[pick].astype("int")
|
|
|
|
|
|
|
|
|
|
|
|
|
-def screen_has_changed(prev_screenshot, threshold=0.01):
|
|
|
|
|
- # Take a new screenshot
|
|
|
|
|
- current_screenshot = device.screencap()
|
|
|
|
|
-
|
|
|
|
|
- # Convert to NumPy arrays
|
|
|
|
|
- prev_img = np.frombuffer(prev_screenshot, dtype=np.uint8)
|
|
|
|
|
- current_img = np.frombuffer(current_screenshot, dtype=np.uint8)
|
|
|
|
|
-
|
|
|
|
|
- # Load images
|
|
|
|
|
- prev_img = cv2.imdecode(prev_img, cv2.IMREAD_COLOR)
|
|
|
|
|
- current_img = cv2.imdecode(current_img, cv2.IMREAD_COLOR)
|
|
|
|
|
-
|
|
|
|
|
- # Calculate absolute difference
|
|
|
|
|
- diff = cv2.absdiff(prev_img, current_img)
|
|
|
|
|
- non_zero_count = np.count_nonzero(diff)
|
|
|
|
|
-
|
|
|
|
|
- # print(f"diff: {non_zero_count} > {threshold * diff.size} = {non_zero_count > threshold * diff.size}")
|
|
|
|
|
-
|
|
|
|
|
- # Check if the difference is greater than the threshold
|
|
|
|
|
- return non_zero_count > threshold * diff.size
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
def save_screenshot():
|
|
def save_screenshot():
|
|
|
# Take a screenshot
|
|
# Take a screenshot
|
|
|
- result = device.screencap()
|
|
|
|
|
|
|
+ result = capture_current_screen()
|
|
|
|
|
|
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
image = Image.open(io.BytesIO(result))
|
|
image = Image.open(io.BytesIO(result))
|
|
@@ -111,25 +90,17 @@ def save_screenshot():
|
|
|
image = image.convert("RGB") # Convert to RGB mode for JPEG
|
|
image = image.convert("RGB") # Convert to RGB mode for JPEG
|
|
|
with open(jpeg_filename, "wb") as fp:
|
|
with open(jpeg_filename, "wb") as fp:
|
|
|
image.save(fp, format="JPEG", quality=85) # Adjust quality as needed
|
|
image.save(fp, format="JPEG", quality=85) # Adjust quality as needed
|
|
|
-
|
|
|
|
|
|
|
+ print(f"snap: {jpeg_filename}")
|
|
|
time.sleep(0.5)
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
-def wait_for_screen_change():
|
|
|
|
|
- # Usage example
|
|
|
|
|
- prev_screenshot = device.screencap()
|
|
|
|
|
-
|
|
|
|
|
- while not screen_has_changed(prev_screenshot):
|
|
|
|
|
- time.sleep(0.1) # Polling interval
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
def tap(location):
|
|
def tap(location):
|
|
|
- device.shell(f"input tap {location}")
|
|
|
|
|
|
|
+ tap_helper(location)
|
|
|
time.sleep(1)
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
-def swipe(start, end):
|
|
|
|
|
- device.shell(f"input swipe {start} {end} 1000")
|
|
|
|
|
|
|
+def swipe(start, end, duration=1000):
|
|
|
|
|
+ swipe_helper(start, end, duration)
|
|
|
time.sleep(0.5)
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
|
|
@@ -153,47 +124,6 @@ def is_end_of_log():
|
|
|
return result
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
-def find_templates(template_image):
|
|
|
|
|
- screenshot = device.screencap()
|
|
|
|
|
- target_image = Image.open(BytesIO(screenshot))
|
|
|
|
|
-
|
|
|
|
|
- # Convert the image to a NumPy array and then to BGR format (which OpenCV uses)
|
|
|
|
|
- target_image = np.array(target_image)
|
|
|
|
|
- target_image = cv2.cvtColor(target_image, cv2.COLOR_RGB2BGR)
|
|
|
|
|
-
|
|
|
|
|
- w, h = template_image.shape[:-1]
|
|
|
|
|
-
|
|
|
|
|
- # Template matching
|
|
|
|
|
- result = cv2.matchTemplate(target_image, template_image, cv2.TM_CCOEFF_NORMED)
|
|
|
|
|
-
|
|
|
|
|
- # Define a threshold
|
|
|
|
|
- threshold = 0.9 # Adjust this threshold based on your requirements
|
|
|
|
|
-
|
|
|
|
|
- # Finding all locations where match exceeds threshold
|
|
|
|
|
- locations = np.where(result >= threshold)
|
|
|
|
|
- locations = list(zip(*locations[::-1]))
|
|
|
|
|
-
|
|
|
|
|
- # Create list of rectangles
|
|
|
|
|
- rectangles = [(*loc, loc[0] + w, loc[1] + h) for loc in locations]
|
|
|
|
|
-
|
|
|
|
|
- # Apply non-maximum suppression to remove overlaps
|
|
|
|
|
- rectangles = non_max_suppression(rectangles, 0.3)
|
|
|
|
|
-
|
|
|
|
|
- # Initialize an empty list to store coordinates
|
|
|
|
|
- coordinates = []
|
|
|
|
|
-
|
|
|
|
|
- for startX, startY, endX, endY in rectangles:
|
|
|
|
|
- # Calculate the center coordinates
|
|
|
|
|
- centerX = round(startX + (endX - startX) / 2)
|
|
|
|
|
- centerY = round(startY + (endY - startY) / 2)
|
|
|
|
|
-
|
|
|
|
|
- # Append the coordinate pair to the list
|
|
|
|
|
- coordinates.append((centerX, centerY))
|
|
|
|
|
-
|
|
|
|
|
- # Sort the coordinates by y value in ascending order
|
|
|
|
|
- return sorted(coordinates, key=lambda x: x[1])
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
def find_max_y_pair(coordinates):
|
|
def find_max_y_pair(coordinates):
|
|
|
# find the coordinate pair with the maximum y value
|
|
# find the coordinate pair with the maximum y value
|
|
|
result = max(coordinates, key=lambda x: x[1])
|
|
result = max(coordinates, key=lambda x: x[1])
|
|
@@ -212,6 +142,11 @@ def take_fight_screenshots():
|
|
|
time.sleep(1)
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def find_templates(template):
|
|
|
|
|
+ capture_current_screen()
|
|
|
|
|
+ return find_template(template)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def process_war_log():
|
|
def process_war_log():
|
|
|
buttons = find_templates(fight_button)
|
|
buttons = find_templates(fight_button)
|
|
|
|
|
|