-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.py
143 lines (116 loc) · 3.14 KB
/
1.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
131
132
133
134
135
136
137
138
139
140
141
142
143
import numpy as np
import cv2
from mss import mss
import time
from keras.models import load_model
from directkeys import PressKey, ReleaseKey, W, A, S, D
from getkeys import key_check
bbox = {'top': 0, 'left': 0, 'width': 700, 'height': 480}
sct = mss()
ft = time.time()
count = 0
t_time = 0.06
def straight():
PressKey(W)
ReleaseKey(A)
ReleaseKey(D)
ReleaseKey(S)
time.sleep(0.09)
ReleaseKey(W)
def left():
PressKey(W)
PressKey(A)
ReleaseKey(D)
ReleaseKey(S)
time.sleep(t_time)
ReleaseKey(W)
ReleaseKey(A)
def right():
PressKey(W)
PressKey(D)
ReleaseKey(A)
ReleaseKey(S)
time.sleep(t_time)
ReleaseKey(W)
ReleaseKey(D)
def reverse():
PressKey(S)
ReleaseKey(W)
ReleaseKey(A)
ReleaseKey(D)
time.sleep(t_time)
ReleaseKey(S)
# def reverseleft():
# PressKey(S)
# PressKey(A)
# ReleaseKey(W)
# ReleaseKey(D)
# time.sleep(t_time)
# ReleaseKey(S)
# ReleaseKey(A)
# def reverseright():
# PressKey(S)
# PressKey(D)
# ReleaseKey(W)
# ReleaseKey(A)
# time.sleep(t_time)
# ReleaseKey(S)
# ReleaseKey(D)
# Function to define a region of interest (not used in current implementation)
def roi(img, vertices):
mask = np.zeros_like(img)
cv2.fillPoly(mask, vertices, 255)
masked = cv2.bitwise_and(img, mask)
return masked
# Function to process the image (not used in current implementation)
def process_img(original_image):
processed_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
processed_img = cv2.Canny(processed_img, 100, 200)
return processed_img
paused = False
model = load_model('5elatermodel_202403191238.keras')
while True:
frames = []
for _ in range(10):
screen = np.array(sct.grab(bbox))
screen = cv2.cvtColor(screen, cv2.COLOR_RGBA2RGB)
screen = cv2.resize(screen, (160, 120))
frames.append(screen)
# Stack the frames along a new axis to create a batch of 10 frames
batch = np.stack(frames, axis=0)
# Add a batch dimension of size 1 to match the model's input shape
resized_batch = np.expand_dims(batch, axis=0)
# Preprocess the batch (you might need additional preprocessing here)
resized_batch = resized_batch.astype(np.uint8)
# Make prediction
prediction = model.predict(resized_batch)
idx = int(np.argmax(prediction[0]))
turn_thresh = 0.2
if idx == 1:
left()
print("left")
elif idx == 0:
straight()
print("Straight")
elif idx == 2:
reverse()
print("reverse")
elif idx == 3:
right()
print("right")
else:
print("Something went wrong")
keys = key_check()
if 'T' in keys:
if paused:
paused = False
time.sleep(1)
else:
paused = True
ReleaseKey(A)
ReleaseKey(W)
ReleaseKey(D)
time.sleep(10)
if time.time() - ft >= 1000:
print(f"We have {count} FPS")
count = 0