-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrex_play.py
129 lines (74 loc) · 2.25 KB
/
trex_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 28 22:32:38 2021
@author: buzun
"""
from keras.models import model_from_json
import numpy as np
from PIL import Image
import keyboard
import time
from mss import mss
# game map
limits = {"top":375, "left": 740, "width":250, "height": 100 }
sct = mss()
# img size
width = 250
height = 100
# model yükle
model = model_from_json(open("/home/buzun/Workspace/Trex-CNN/model.json","r").read())
model.load_weights("trex_weight.h5")
# down = 0, right = 1, up = 2
labels = ["Down", "Right", "Up"]
frameRateTime = time.time()
counter = 0
i = 0
delay = 0.32
key_down_pressed = False
keyboard.press(keyboard.KEY_UP)
keyboard.release(keyboard.KEY_UP)
while True:
img = sct.grab(limits)
img1 = Image.frombytes("RGB", img.size, img.rgb)
img2 = np.array(img1.convert("L").resize((width, height))) / 255
X = np.array([img2])
X = X.reshape(X.shape[0], width, height, 1)
results = model.predict(X)
result = np.argmax(results)
print("---------------------")
# print("Down: {} \nRight:{} \nUp: {} \n".format(results[0][0]
# ,results[0][1]
# ,results[0][2]))
print("action : ", result, "\n frame : ", i)
# down = 0
if result == 0:
keyboard.press(keyboard.KEY_DOWN)
key_down_pressed = True
# up = 2
elif result == 2:
if key_down_pressed:
keyboard.release(keyboard.KEY_DOWN)
time.sleep(delay)
keyboard.press(keyboard.KEY_UP)
keyboard.release(keyboard.KEY_UP)
print("pressed up\n")
if i < 1500:
time.sleep(0.3)
elif 1500 < i and i < 5000:
time.sleep(0.2)
else:
time.sleep(0.17)
#keyboard.press(keyboard.KEY_DOWN)
#keyboard.release(keyboard.KEY_DOWN)
counter += 1
if (time.time() - frameRateTime) > 1:
counter = 0
frameRateTime = time.time()
if i <= 1500:
delay -= 0.003
else:
delay -= 0.005
if delay < 0:
delay = 0
i += 1