-
Notifications
You must be signed in to change notification settings - Fork 26
/
demo.py
108 lines (75 loc) · 2.77 KB
/
demo.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
import socket
import binascii
import pyautogui
import joblib
import threading
import speech_recognition as SR
UDP_IP = 'IP'
UDP_PORT = 'PORT'
model = joblib.load('svm_classifier')
scaler = joblib.load('scaler_svm')
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
pyautogui.FAILSAFE = True
commands = [ '',
'pyautogui.keyUp("w")',
'pyautogui.click(button="right")',
'pyautogui.keyDown("w")',
'pyautogui.drag(-470, 0, 0.5,button="left")',
'pyautogui.drag(470, 0, 0.5,button="left")'
]
RECOG = SR.Recognizer()
def phone_controller():
temp = 1
while True:
buffer_size = 1024
data, addr = sock.recvfrom(buffer_size)
val = binascii.hexlify(data).decode('utf-8')
val = [val[i:i+2] for i in range(0, len(val), 2)]
measures = val[36:48]
#print(measures)
w = [int(x , 16) for x in measures]
row_ = []
for v in [w[0:2] , w[4:6] , w[8:10]] :
for k in v : row_.append(k)
p = scaler.transform([row_])
m = model.predict(p)[0]
#print(m)
if abs(m - temp) !=0 :
#pyautogui.click(button='right')
try :
if m == 2 and temp == 3:
pyautogui.keyUp("w")
if m == 3 and temp == 2 :
pyautogui.click(button="right")
if m == 1 and temp == 2 :
pyautogui.click(button="right")
#exec(commands[m])
except: c__ = 0
if m in (4,5) and temp == 3:
#pyautogui.keyUp("w")
#pyautogui.keyDown("z")
exec(commands[m])
#pyautogui.keyUp("z")
else:
exec(commands[m])
if m in (4 , 5):
pyautogui.moveTo(950 , 570)
temp = m
#print(m)
def voice_controls():
while True:
with SR.Microphone() as source :
RECOG.adjust_for_ambient_noise(source)
audio = RECOG.listen(source)
try :
movement = RECOG.recognize_google(audio)
if movement == 'reload' :
pyautogui.press('r')
if movement == 'jump' :
pyautogui.press('space')
except Exception as e : pass
phone_controller_thread = threading.Thread(target = phone_controller)
voice_control_thread = threading.Thread(target = voice_controls)
phone_controller_thread.start()
voice_control_thread.start()