HonorNet_v1 / inference /game_controller.py
clarenceleo's picture
Upload 5 files
e012066 verified
# game_controller.py
import subprocess
from PIL import Image
import io
class GameController:
def __init__(self, device_id="emulator-5554"):
self.device_id = device_id
def screenshot(self):
"""截图并返回PIL Image对象"""
cmd = f"adb -s {self.device_id} exec-out screencap -p"
output = subprocess.check_output(cmd, shell=True)
img = Image.open(io.BytesIO(output))
return img
def tap(self, x, y):
"""点击指定坐标"""
cmd = f"adb -s {self.device_id} shell input tap {x} {y}"
subprocess.run(cmd, shell=True)
def swipe(self, x1, y1, x2, y2, duration=50):
"""滑动操作"""
cmd = f"adb -s {self.device_id} shell input swipe {x1} {y1} {x2} {y2} {duration}"
subprocess.run(cmd, shell=True)
def get_screen_size(self):
"""获取屏幕分辨率"""
cmd = f"adb -s {self.device_id} shell wm size"
output = subprocess.check_output(cmd, shell=True).decode()
size_str = output.split(":")[1].strip()
w, h = map(int, size_str.split("x"))
return w, h