【问题标题】:How to increase listen time in google speech api?如何增加谷歌语音api中的收听时间?
【发布时间】:2019-01-08 17:56:38
【问题描述】:

我已经使用 google Speech to text api 制作了一个工作语音到文本程序,它记录语音并将其复制到 .txt 但是,Google Speech api 不会听很长时间(大约 9 秒)有什么办法增加这个,或者在 python 中使用更好的 api 可以边听边写?

import time
import speech_recognition as sr
import sys
import fileinput
r=sr.Recognizer()
#tells the program to use a mic and to listen
with sr.Microphone() as source:
    audio=r.listen(source)
#asking the program to try to listen
try:
    spoken = r.recognize_google(audio)

    print("I heard:"+spoken)

except Exception:
    print ("Somthing went wrong")
#writing what was recorded by the mic into a .txt
with open("name-of-file.txt", "a") as f:
    f.write("\n")
    f.write(time.strftime("%H:%M:%S") + " " + time.strftime("%d/%m/%Y"))
    f.write("\n")
    f.write(spoken)

预期结果: 程序边听边写 或者 该程序可以收听直到关闭。 实际结果: 程序监听大约 9 秒,然后打印到 .txt

【问题讨论】:

    标签: python google-speech-api


    【解决方案1】:

    语音识别是一个非常好的库,但我也不得不与录音长度作斗争。以下是我解决此问题的方法:

    将音频保存到磁盘

    with sr.AudioFile('path/to/audiofile.wav') as source:
        audio = r.record(source)
    

    优点:与流式传输相比,录制到音频文件然后将更长的块发送到 google 使我的录制长度更加一致。

    缺点:根据音频文件的大小,这可能会带来将响应时间延长到几秒钟的缺点,这在您的情况下可能无法使用。

    尽量减少本底噪声

    您可能已经非常清楚,更好的信噪比将提供更好的 STT 准确性 - 但我也发现它对于语音识别库的良好块大小至关重要。

    仔细检查您的本底噪声是否易于与您的来源区分开来。录制音频还可以帮助您解决此问题。有时,使用语音识别库时音频可能会过早中断,因为它无法清楚地检测到您在说话。

    如果无法提高麦克风的质量或接近度,则库中包含一个工具,可校准音频电平以实现最佳的信噪区分。

    要激活此功能,而不是行:

    audio=r.listen(source)
    

    尝试使用:

    audio=r.adjust_for_ambient_noise(source)
    

    请注意,此功能在某些情况下会增加少量延迟。在其他情况下,如果你给它输入嘈杂的音频,它会无限期地继续收听。

    结合起来

    with sr.AudioFile('path/to/audiofile.wav') as source:
        audio = r.adjust_for_ambient_noise(source)
    

    这是这个库的一个很好的指南 - The Ultimate Guide To Speech Recognition With Python

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多