-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.py
More file actions
61 lines (48 loc) · 2.02 KB
/
task3.py
File metadata and controls
61 lines (48 loc) · 2.02 KB
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
import speech_recognition as sr
import threading
assistant_name = 'luck'
listener = sr.Recognizer()
# Shared variable to signal when to stop monitoring
stop_Speaking = True
stop_monitoring = False
# Shared variable to signal when to stop monitoring
def monitor_audio(source):
# Continuously monitor the audio while it's being received
silent = True
while not stop_monitoring:
try:
with sr.Microphone() as monitor:
# Listen to a short segment of audio to check if speech is present
audio = listener.listen(monitor, phrase_time_limit=0.5)
if audio:
if silent:
silent = False
print(">> listening ...", end="")
else:
print(" ...", end="")
except KeyboardInterrupt:
break
while True:
if stop_Speaking:
try:
with sr.Microphone() as source:
listener.adjust_for_ambient_noise(source)
print("\n[" + assistant_name.upper() + ' is ready, your majesty!]')
# Create a separate thread to monitor the audio
stop_monitoring = False
audio_thread = threading.Thread(target=monitor_audio, args=(source,))
audio_thread.start()
# Listen for the user's speech
voice = listener.listen(source)
# Stop monitoring thread
stop_monitoring = True
if voice:
print("\n")
translated_voice = listener.recognize_google(voice).lower()
print('You said: "' + translated_voice + '".')
else:
continue
except sr.UnknownValueError:
print('You didn’t say anything or Unable to recognize your speech.')
except sr.RequestError as e:
print('Error occurred during speech recognition:', str(e))