尝试在虚拟机上使用Google声音识别API从音频文件中提取文本。线性代码花费太多时间,因此需要多线程。该代码可在几行输出中正常运行,但以speech_recognition.UnknownValueError结尾。建议解决方法
import speech_recognition as sr
from pydub import AudioSegment
from multiprocessing import Pool
import os
def convert(audio_segment):
r = sr.Recognizer()
strx = os.getpid()
strx = str(strx) + ".wav"
audio_segment.export(strx, format="wav")
with sr.AudioFile(strx) as source:
audio = r.record(source)
command = r.recognize_google(audio)
print command
if __name__ == '__main__':
p = Pool(10)
sound = AudioSegment.from_file('test.wav')
ts = 30*1000
audio_segment_array = []
length = len(sound)
step = ts
start = 0
while step < length:
audio_segment_array.append(sound[start:step])
start = step
step = step + ts
audio_segment_array.append(sound[step:length])
p.map(convert,audio_segment_array)