-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_to_text_BLOCK.py
48 lines (35 loc) · 1.16 KB
/
speech_to_text_BLOCK.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
'''
Rishabh Panda
1904215 (ETC3)
KIIT University, Bhubaneswar
'''
import speech_recognition as sr
import pyttsx3
# Initializing the recognizer
recognizer = sr.Recognizer()
# Function for text to speech conversion
def Text2Speech(command):
# Initializing the engine
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()
# Handling exceptions at the runtime
try:
# using microphone as input source.
with sr.Microphone() as source2:
# Adjusting the energy threshold based on the surrounding noise level
print("Calibrating background noise...")
recognizer.adjust_for_ambient_noise(source2, duration=0.2)
print("Calibration successful. Prescribe!")
# listening user's input
audio2 = recognizer.listen(source2)
# Using Google Web Speech API to recognize audio
MyText = recognizer.recognize_google(audio2)
MyText = MyText.lower()
# Confirmation using text and audio
print(MyText)
Text2Speech(MyText)
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occurred")