-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainedAgent.py
44 lines (31 loc) · 984 Bytes
/
trainedAgent.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
from environment import Environment
from fastai.vision.all import *
import keyboard
import cv2
import pathlib
def label_func(x): return x.parent.name
def load(path): # loads trained model
learner = load_learner(path)
print('Model loaded.')
return(learner)
def run(learner):
env = Environment()
image = env.reset()
while not keyboard.is_pressed("e"):
cv2.imshow('Bot View', image)
cv2.waitKey(1)
result = learner.predict(image)
action = result[0]
if action == 'dup84':
keyToPress = 0.0
elif action == 'ddown84':
keyToPress = 1.0
else:
keyToPress = 2.0
image, reward, done = env.step(keyToPress)
if __name__ == '__main__':
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
path = os.path.join(Path(__file__).parent.resolve() / 'models/model_resnet-18_5_30k.pkl')
learner = load(path)
run(learner)