-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.py
131 lines (119 loc) · 3.15 KB
/
drive.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
128
129
130
from keras.models import load_model
import grabscreen
from gettrainingdata import imageprocessing
import numpy as np
from sendkey import PressKey
from sendkey import ReleaseKey
import time
import cv2
IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS = 150, 400, 1
model = load_model("model1.h5")
W = 0x11
A = 0x1E
S = 0x1F
D = 0x20
for j in range(5):
print(j + 1)
time.sleep(1)
k = np.array([[0,0,0]])
while True:
image1 = grabscreen.grab_screen([0, 350, 800, 650])
image1 = imageprocessing(image1)
image1 = cv2.resize(image1, (400, 150))
#new_img = imageprocessing(new_img)
new1 = image1
image1 = np.array(image1, dtype=np.uint8)
image1 = image1.reshape(1, IMAGE_HEIGHT, IMAGE_WIDTH, 1)
image1 = image1.astype('float32')
image1 /= 255
output = (model.predict(image1,batch_size=1))
#output = np.abs(output)
print(output)
if output[0][0] > output[0][1] and output[0][0] > output[0][2]:
op = output[0][0]
if op > 0.8:
PressKey(A)
ReleaseKey(W)
ReleaseKey(D)
print("A")
else:
print("AW")
PressKey(A)
PressKey(W)
ReleaseKey(D)
elif output[0][2] > output[0][1]:
op = output[0][2]
if op > 0.8:
print("D")
PressKey(D)
ReleaseKey(W)
ReleaseKey(A)
else:
print("DW")
PressKey(D)
PressKey(W)
ReleaseKey(A)
else:
PressKey(W)
ReleaseKey(A)
ReleaseKey(D)
print('W')
#time.sleep(0.2)
'''out = (output-k)
out[0][0] = (out[0][0]/k[0][0])*100000
out[0][1] = (out[0][1] / k[0][1]) * 100000
out[0][2] = (out[0][2] / k[0][2]) * 100000
#print(out)
if out[0][0] > out[0][1] and out[0][0] > out[0][2] or (out[0][0] < 50 and out[0][0] > 0):
op = out[0][0]
if op > 10:
PressKey(A)
ReleaseKey(W)
ReleaseKey(D)
print("AW")
else:
print("A")
PressKey(A)
PressKey(W)
ReleaseKey(D)
elif out[0][2] > out[0][1] or (out[0][2] < 50 and out[0][2] > 0):
op = out[0][2]
if op < -10:
print("DW")
PressKey(D)
ReleaseKey(W)
ReleaseKey(A)
else:
print("D")
PressKey(D)
PressKey(W)
ReleaseKey(A)
else:
PressKey(W)
ReleaseKey(A)
ReleaseKey(D)
print("W")
k = output
#output = [x for x in output[0]]
#print(output)
#output = [round(x) for x in output[0]]
#cv2.imshow("window1", new1)
#if cv2.waitKey(255) & 0xFF == ord('q'):
# cv2.destroyAllWindows()
# break
time.sleep(0.4)'''
'''if output[0][0] > output[0][1] and output[0][0] > output[0][2]:
PressKey(A)
ReleaseKey(W)
ReleaseKey(D)
print('A')
elif output[0][2] > output[0][1] and output[0][2] > output[0][0]:
PressKey(D)
ReleaseKey(A)
ReleaseKey(W)
print('D')
else:
PressKey(W)
ReleaseKey(A)
ReleaseKey(D)
print('W')'''