-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_reg.py
46 lines (36 loc) · 1.41 KB
/
speech_reg.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
import speech_recognition as sr
import serial
import websocket
import time
import os
r = sr.Recognizer()
def send_command_to_esp32(command):
websocket_url = "ws://172.20.10.6:81" # Replace with your ESP32 WebSocket server URL
ws = websocket.create_connection(websocket_url)
ws.send(command)
if "exit" in command:
ws.close()
def recognize_speech():
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
# text = r.recognize_sphinx(audio)
print("You said: " + text)
if "on" in text:
# ser.write(b'1') # Sending '1' to turn on the LED
send_command_to_esp32("on")
print('Sent 1')
elif "off" in text:
# ser.write(b'0') # Sending '0' to turn off the LED
send_command_to_esp32("off")
print('Sent 0')
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
# Continuously listen for speech
send_command_to_esp32("off")
while True:
recognize_speech()