-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
62 lines (41 loc) · 1.39 KB
/
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 27 00:28:59 2020
@author: berk
"""
#------------------------------------------------------
#This section for reduce some errors related to GPU allocation on my system.
#it may not neccesary for yours. If it is, removing this part may increase the performance.
from tensorflow import Session,ConfigProto
from keras.backend.tensorflow_backend import set_session
config = ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(Session(config=config))
#--------------------------------------------------------
from pynput.keyboard import Key, Controller
import os
from mss import mss
from PIL import Image
from tensorflow.keras.models import load_model
import numpy as np
from tensorflow.keras.preprocessing.image import img_to_array
keyboard = Controller()
def grabscreen():
im = mss().grab({'top': 350, 'left': 625, 'width': 750, 'height': 130})
im = Image.frombytes('RGB', im.size, im.rgb)
im = img_to_array(im)
return im
model = load_model('model.h5')
while True:
im = grabscreen()
result = model.predict(im[None])
if np.argmax(result)== 1:
os.system('clear')
print("up")
keyboard.press(Key.space)
keyboard.release(Key.space)
else:
os.system('clear')
print("straight")
continue