-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict.py
50 lines (45 loc) · 1.44 KB
/
predict.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
import tensorflow as tf
import cv2
import numpy as np
from createData import screenshot, preprocess
from time import sleep
import keyboard
save_path="savedmodel"
class_names = ['left', 'none', 'right']
model = tf.keras.models.load_model(save_path)
# used to predict the key to press from an image
def predict(img):
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
prediction = model.predict(img_array)
score = tf.nn.softmax(prediction[0])
return class_names[np.argmax(score)], 100 * np.max(score)
# main loop
# takes screenshots and predicts the needed keypress
def play():
print("press 's' to start")
keyboard.wait("s")
sleep(1)
print("started")
current = "none"
while True:
if keyboard.is_pressed("s"):
if current != 'none':
keyboard.release(current)
break
img = screenshot("car (DEBUG)")
img = preprocess(img)
prediction, acc = predict(img)
print(prediction, acc)
if prediction != 'none':
if prediction != current:
if current != 'none':
keyboard.release(current)
keyboard.press(prediction)
else:
if current != 'none':
keyboard.release(current)
current = prediction
print("stopped")
if __name__ == '__main__':
play()