-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_voice_recognition.py
82 lines (72 loc) · 2.87 KB
/
stream_voice_recognition.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
import speech_recognition as sr
import pygame
import os
def initialize_audio():
"""
Initialize the audio system using pygame.
"""
pygame.mixer.init()
def play_sound(file_path):
"""
Play a sound file.
Args:
file_path (str): Path to the sound file.
"""
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def play_error():
"""
Play an error sound.
"""
play_sound(os.path.join("sounds", "error.wav"))
def listen_for_wake_word():
"""
Listen for a wake word and then listen for commands.
Yields:
str: Recognized command.
"""
recognizer = sr.Recognizer()
initialized = False
with sr.Microphone() as source:
print("Adjusting for ambient noise...")
recognizer.adjust_for_ambient_noise(source, duration=1) # Adjust for ambient noise once
while True:
recognizer.adjust_for_ambient_noise(source, duration=0.5)
print("adjust ambient noise")# Adjust for ambient noise once
print("Listening...")
try:
initialize_audio()
audio = recognizer.listen(source, timeout=None) # No timeout for this part
if not initialized:
wake_word = recognizer.recognize_google_cloud(audio).lower()
if wake_word == "wake up":
play_sound(os.path.join("sounds", "start-test.wav"))
print("Wake word detected. Listening for commands")
initialized = True
elif initialized:
command = recognizer.recognize_google_cloud(audio).lower()
print("Command detected: ", command)
yield command # Return the command as a generator
except sr.UnknownValueError:
if initialized:
print("Failed to detect command. Retrying...")
play_error()
# Optionally, prompt the user to repeat the command
print("Please repeat the command.")
except sr.RequestError as e:
if initialized:
print(f"Error making the request; {e}")
play_error()
# Optionally, prompt the user to repeat the command
print("Please repeat the command.")
if __name__ == "__main__":
initialize_audio() # Initialize audio system before starting
for command in listen_for_wake_word():
if command == "move forward":
# Your code to execute the "move forward" action
print("Moving forward!")
play_sound(os.path.join("sounds", "moving_forward.wav"))
elif command == "shut down":
print("Shutting down...")
play_sound(os.path.join("sounds", "shut-down.wav"))
break # Exit the loop to stop the program