-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeechRecognition.py
More file actions
33 lines (24 loc) · 873 Bytes
/
speechRecognition.py
File metadata and controls
33 lines (24 loc) · 873 Bytes
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
''' Speech recognition from an audio file '''
import speech_recognition as sr
from os import path
import sys
def recognize(fileName):
AUDIO = 'Converted_Audio/' + fileName
r = sr.Recognizer()
print('speechRecognition.py: ')
print('Opening ' + fileName + '...')
with sr.AudioFile(AUDIO) as source:
f = open('Transcripts/' + fileName[:-4] + ".txt", "w+")
r.adjust_for_ambient_noise(source)
print("Listening")
for i in range(int(source.DURATION/5)):
try:
audio = r.listen(source)
transcript = r.recognize_google(audio, show_all=False) # Can set show_all=True for alternative readings
f.write(transcript+'\n')
except Exception as e:
print(e)
f.close()
print('\n'*3)
if __name__ == '__main__':
recognize(sys.argv[1])