-
Notifications
You must be signed in to change notification settings - Fork 0
/
hear.py
46 lines (37 loc) · 1.25 KB
/
hear.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
#Import the required packages
import speech_recognition as sr
#Initialize the recognizer
rec = sr.Recognizer()
#Record the user command; convert to text and store it in a variable
def record_text():
#Loop in case of errors
while(1):
try:
#use the microphone as source for input.
with sr.Microphone() as source:
# Prepare recognizer to receive input
rec.adjust_for_ambient_noise(source, duration=0.2)
#listens for the user's input
audio = rec.listen(source)
#Using google to recognize audio
MyText = rec.recognize_google(audio)
return MyText
except sr.RequestError as e:
print("Could not request results; (0)".format(e))
except sr.UnknownValueError:
print("Unknown error occurred")
#Store the converted text into a .txt file
def output_text(text):
file = open("output.txt", "a")
file.write(text)
file.write("\n")
file.close()
#Main block
text="start"
while(text != "stop"):
print("Started Recording")
text = record_text()
output_text(text)
print(f"The converted text is {text}")
print("Text converted and stored")
print("Stopped Recording")