【发布时间】:2016-10-06 07:09:46
【问题描述】:
我目前正在从一个目录中获取单个音频文件的输入,并且我将我的输出保存在 CSV 文件中,带有文件名并将语音转换为文本输出,但我在该目录中有 100 个文件(即 001.wav ,002.wav,003.wav.......100.wav)
我想编写一个循环或函数,将语音自动保存为 CSV 中的文本输出,并在不同的行中使用相应的文件名。
代码如下:
import speech_recognition as sr
import csv
import os
AUDIO_FILE =path.join(path.dirname('C:/path/to/directory'), "001.wav")
file_name = os.path.basename(AUDIO_FILE)
name = os.path.basename(AUDIO_FILE)
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.record(source) # read the entire audio file
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
a = r.recognize_google(audio)
except sr.UnknownValueError:
a = "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
a = "Could not request results from Google Speech Recognition service; {0}".format(e)
try:
b = r.recognize_sphinx(audio)
except sr.UnknownValueError:
b = "Sphinx could not understand audio"
except sr.RequestError as e:
b = "Sphinx error; {0}".format(e)
with open('speech_output.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(['file_name','google',sphinx])
writer.writerow([file_name,a,b])
对代码的引用。 https://github.com/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py
【问题讨论】:
标签: python python-2.7 python-3.x