-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhearing.py
45 lines (35 loc) · 1.28 KB
/
hearing.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
import speech_recognition as sr
import socket
import os
from dotenv import load_dotenv
# Initialize the recognizer
load_dotenv()
recognizer = sr.Recognizer()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((socket.gethostname(), int(os.getenv("HEARING_PORT"))))
server.listen(1)
print("Server is listening...")
client_socket, address = server.accept()
print(f"Connection from {address} has been established!")
def listen():
while True:
audio_command = "--silence--"
with sr.Microphone() as source:
print("Listening... Speak something:")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
print("Recognition complete. Transcribing...")
try:
text = recognizer.recognize_google(audio)
print("You said: " + text)
audio_command = text
except sr.UnknownValueError:
print("Google Web Speech API could not understand audio")
except sr.RequestError as e:
print(f"Could not request results from Google Web Speech API; {e}")
client_socket.send(audio_command.encode('utf-8'))
if audio_command == "exit":
break
client_socket.close()
server.close()
listen()