-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAIC Chatbot part 1 speech recognition.py
42 lines (32 loc) · 1.18 KB
/
AIC Chatbot part 1 speech 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
# Speech recognition
# packages needed: numpy, SpeechRecognition, gTTS
import speech_recognition as sr
class Chatbot:
def __init__(self, name):
print("----- Starting up", name, "-----")
self.name = name
self.audioText = ""
self.status = "Sleep"
def speech_to_text(self):
recognizer = sr.Recognizer()
# Represents the energy level threshold for sounds.
# Values below this threshold are considered silence, and values above this threshold are considered speech.
recognizer.energy_threshold = 300
# Represents the minimum length of silence (in seconds) that will register as the end of a phrase.
recognizer.pause_threshold = 0.8;
with sr.Microphone() as mic:
print("listening...")
audio = recognizer.listen(mic)
try:
self.audioText = recognizer.recognize_google(audio, language="en-US",)
print("You :", self.audioText)
except Exception:
pass
# Boot the AI
if __name__ == "__main__":
ai = Chatbot("John")
ex = True
while ex:
res = False
ai.speech_to_text()
print("----- Closing down chatbot -----")