-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.py
39 lines (27 loc) · 1.07 KB
/
voice.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
import speech_recognition
import pyttsx3
# Initialize the recognizer and the engine for text to speech
recognizer = speech_recognition.Recognizer()
# open a file to write the voice transcript, append any new text to the end of the file
f = open("voice_transcript.txt", "a")
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
# Create a while loop to keep the program running
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio)
print(f"I detected: {text}")
f.write(text + "\n")
engine.say(text)
engine.runAndWait()
# If the user says "stop" or no speech is detected, break the loop
except speech_recognition.UnknownValueError():
recognizer = speech_recognition.Recognizer()
print("No speech detected - terminating")
# Close the file
f.close()
continue