Refactor with pyautogui

This commit is contained in:
Timothyxxx
2023-12-02 17:52:00 +08:00
parent 3aebeffec3
commit 992d8f8fce
11 changed files with 144 additions and 515 deletions

63
main.py
View File

@@ -1,55 +1,31 @@
from pprint import pprint
from desktop_env.envs.desktop_env import DesktopEnv, Action, MouseClick
def get_human_action():
"""
Prompts the human player for an action and returns a structured action.
"""
print("\nAvailable actions:", [action.name for action in Action])
action_type = None
while action_type not in [action.value for action in Action]:
action_type = Action[input("Enter the type of action: ".strip())].value
action = {"action_type": action_type}
if action_type == Action.CLICK.value or action_type == Action.MOUSE_DOWN.value or action_type == Action.MOUSE_UP.value:
print("\n Available clicks:", [action.name for action in MouseClick])
click_type = input("Enter click type: ")
action["click_type"] = MouseClick[click_type].value
if action_type == Action.MOUSE_MOVE.value:
x = int(input("Enter x-coordinate for mouse move: "))
y = int(input("Enter y-coordinate for mouse move: "))
action["x"] = x
action["y"] = y
if action_type == Action.KEY.value:
key = input("Enter the key to press: ")
action["key"] = [ord(c) for c in key]
if action_type == Action.TYPE.value:
text = input("Enter the text to type: ")
action["text"] = [ord(c) for c in text]
return action
from desktop_env.envs.desktop_env import DesktopEnv
def human_agent():
"""
Runs the Gym environment with human input.
"""
env = DesktopEnv(path_to_vm="/home/yuri/vmware/Windows 10 x64/Windows 10 x64.vmx",
# path_to_vm="/home/yuri/vmware/Ubuntu 64-bit/Ubuntu 64-bit.vmx",
username="user",
password="password",
# host="192.168.7.128",
host="http://192.168.7.129:5000",
vm_os="windows")
observation = env.reset()
env = DesktopEnv(
path_to_vm=r"""C:\Users\tianbaox\Documents\Virtual Machines\Win10\Win10.vmx""",
# path_to_vm="/home/yuri/vmware/Ubuntu 64-bit/Ubuntu 64-bit.vmx",
# host="192.168.7.128",
host="http://192.168.13.128:5000",
)
# reset the environment to certain snapshot
# observation = env.reset()
done = False
while not done:
action = get_human_action()
# action = get_human_action()
# action = {
# "action_type": 0,
# "click_type": 3,
# }
action = "pyautogui.dragTo(100, 200, button='left')"
observation, reward, done, info = env.step(action)
print("Observation:", observation)
print("Reward:", reward)
@@ -64,5 +40,6 @@ def human_agent():
env.close()
print("Environment closed.")
if __name__ == "__main__":
human_agent()