-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (34 loc) · 1.09 KB
/
main.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
import speech_recognition as sr
from gtts import gTTS
import pyttsx3
import os
# Create a recognizer object
r = sr.Recognizer()
engine = pyttsx3.init()
engine.say("Could you please tell me your name?")
def listen():
with sr.Microphone() as source:
print("Listening...")
r.adjust_for_ambient_noise(source)
audio = r.listen(source, timeout=10, phrase_time_limit=5)
try:
text = r.recognize_google(audio)
return text
except sr.UnknownValueError:
print("Sorry, I didn't understand that.")
except sr.RequestError as e:
print("Sorry, I couldn't request results from the Google Speech Recognition service; {0}".format(e))
def speak(text):
tts = gTTS(text=text, lang='en')
engine.say(tts)
tts.save("output.mp3")
# os.system("mpg321 output.mp3")
# Example usage
while True:
input_text = listen()
if input_text:
print("User: ", input_text)
speak("Okay, so your name is " + input_text)
engine.runAndWait()
else:
speak("Sorry, I didn't hear anything. Can you please repeat?")