-
Notifications
You must be signed in to change notification settings - Fork 0
/
sppech.py
45 lines (31 loc) · 1.08 KB
/
sppech.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
import speech_recognition as sr , os,time
def speech_to_text(file_name):
r = sr.Recognizer()
list_audio= os.listdir("//home//pi//Desktop//RasPA//audio_split//")
list_audio=[x[0:len(x)-4] for x in list_audio ]
list_audio.sort(key=int)
for audio in list_audio:
audio_path=os.path.join(os.getcwd()+"//audio_split//"+audio+".wav")
print(audio_path)
AUDIO_FILE = (audio_path)
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source)
try:
print("Transcribing file -" + AUDIO_FILE)
line_counter=1
text=r.recognize_google(audio).split()
with open(file_name[:-4]+"Results","a+") as f:
for words in text:
if (line_counter%25)==0:
f.write(words)
f.write("\n")
line_counter+=1
continue
f.write(words)
f.write(" ")
line_counter+=1
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
time.sleep(2)