-
Notifications
You must be signed in to change notification settings - Fork 5
/
play.py
127 lines (103 loc) · 3.36 KB
/
play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import platform
import cv2
import mss
import time
import torch
import argparse
import numpy as np
from utils import (NeuralNet, BRAIN,
check_device, get_driver_path, get_model_weights)
from control_scripts_lib import SurvivAgent, Game
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str,
default="supporting_files/model_weights.pth")
parser.add_argument('--chrome_driver_path', type=str,
default="supporting_files/chromedriver")
parser.add_argument('--chrome_adblock', type=str,
default="supporting_files/uBlockOrigin.crx")
parser.add_argument('--device', type=str, default='cpu')
parser.add_argument('--n_iter', type=int, default=500)
parser.add_argument('--print_metrics', type=bool, default=False)
parser.add_argument('--plot_states', type=bool, default=False)
parser.add_argument('--save_video', type=bool, default=False)
parser.add_argument('--video_output_path', type=str, default='game.mp4')
return parser.parse_args()
def load_model(model_path: str, device: str):
''' loading action model '''
model = NeuralNet()
model.load_state_dict(
torch.load(model_path, map_location=torch.device(device))
)
model.eval()
return model
def play_game(agent, agent_control, game, monitor,
n_iter) -> (list, float, float):
screen_shots = list()
start_time = time.time()
agent.update_state()
for i in range(n_iter):
with mss.mss() as sct:
screen = sct.grab(monitor)
p = np.array(screen)[:, :, :3]
screen_shots.append(p)
agent.update_state()
actions = agent_control.choose_action(p)
agent.do_all_choosen_actions(direction=actions + 1)
end_time = time.time()
game.close_current_tab()
return screen_shots, start_time, end_time
def save_video(output_file: str, screen_shots, fps: int = 60,
width: int = 1280, height: int = 814) -> None:
''' save gameplay video '''
#h, w, _ = screen_shots[0].shape
# out = cv2.VideoWriter(
# output_file,
# cv2.VideoWriter_fourcc(*"DIVX"), fps, (width, height))
# cv2.VideoWriter_fourcc(*"MPEG"), fps, (w, h))
# for shot in screen_shots:
# out.write(shot)
# out.release()
# print(screen_shots[-1].shape)
#cv2.imshow('image', screen_shots[-1])
# cv2.waitKey(0)
raise NotImplementedError()
def main():
args = get_args()
args.model_path = get_model_weights(args.model_path)
args.device = check_device(args.device)
args.chrome_driver_path = get_driver_path(args.chrome_driver_path)
model = load_model(args.model_path, args.device)
game_env = Game(args.chrome_driver_path, args.chrome_adblock)
agent = SurvivAgent(game_env)
agent_control = BRAIN(
agent=agent,
model=model,
device=args.device,
plot_state=args.plot_states
)
agent.start_playing()
screen_position, screen_dim = game_env.get_window_size()
monitor = {
"top": screen_position['y'],
"left": screen_position['x'],
"width": screen_dim['width'],
"height": screen_dim['height']
}
screen_shots, start_time, end_time = play_game(
agent, agent_control, game_env, monitor, args.n_iter)
if args.print_metrics:
print('AVG fps: ',
round(args.n_iter / (end_time - start_time), 2))
print('AVG 1 circle: ',
round((end_time - start_time) / args.n_iter, 3))
if args.save_video:
save_video(
args.video_output_path,
screen_shots,
round(args.n_iter / (end_time - start_time), 2),
screen_dim['width'],
screen_dim['height']
)
if __name__ == '__main__':
main()