-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (47 loc) · 1.75 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import speech_recognition as speak
import pyttsx3
# Initialise the bot for speech recognition
listening_bot = speak.Recognizer()
# Initialising the bot for text to speech conversion
reading_bot = pyttsx3.init()
# Setting the properties for reading_bot
voices = reading_bot.getProperty('voices')
# male voice - 0 ; female voice - 1
reading_bot.setProperty('voice', voices[1].id)
# reading speed
reading_bot.setProperty('rate', 130)
# Function to convert speech into text
def speech_to_text():
# loop to listen for speech
while True:
try:
# Accessing the microphone for getting input
with speak.Microphone() as mic:
# Prepare recognizer to get the input
listening_bot.adjust_for_ambient_noise(mic, duration=0.2)
# recognizer listening for user's input
audio = listening_bot.listen(mic)
# Use the Google recognize module to recognize audio
heard = listening_bot.recognize_google(audio)
return heard
except speak.RequestError as error:
print("Could not request response: {0}".format(error))
except speak.UnknownValueError:
print("Unknown Error occurred")
return
# Function to convert text into speech
def text_to_speech(text):
# read the text
reading_bot.say(text)
reading_bot.runAndWait()
print("Speak I'm listening...")
while True:
# Function call for speech to text conversion
text = speech_to_text()
# Function call for text to speech Conversion
text_to_speech(text)
# This print statement will verify what the recognizer heard
print("I heard: "+text)
# Condition statement for termination
if text == "all done":
break