-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechToText.py
executable file
·41 lines (27 loc) · 992 Bytes
/
speechToText.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import library
import speech_recognition as sr
# SETTINGS
# File name, the input audio
file_name = "test.wav"
# Language, as in "fr-FR", or "it-IT". Defaults to Italian
lang = "it-IT"
# Output text file. Where should it write the transcribed text
out = "output.txt"
# Initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()
# Reading Audio file as source
# listening the audio file and store in audio_text variable
with sr.AudioFile(file_name) as source:
audio_text = r.listen(source)
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:
# using google speech recognition
text = r.recognize_google(audio_text, language = lang)
print('Converting audio transcripts into text ...')
f = open(out, "w")
f.write(text)
f.close()
except:
print('Sorry.. run again...')