【问题标题】:How can I use audio file as audio source in SpeechRecognition in Python?如何在 Python 的 SpeechRecognition 中使用音频文件作为音频源?
【发布时间】:2019-12-04 11:43:09
【问题描述】:

我在 Python 3.6 中使用了speech_recognition.AudioFile,但是提示了这个错误:

AttributeError: module 'speech_recognition' has no attribute 'AudioFile'

这是我的代码:

#!/usr/bin/env python3

import speech_recognition as sr

# obtain path to "english.wav" in the same folder as this script
from os import path
AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "english.wav")
# AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
# AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")

# 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

另外我正在使用speech_recognition 3.1.3

【问题讨论】:

    标签: python signal-processing speech-recognition avaudiofile audio-source


    【解决方案1】:

    你能换成新版本的 SpeechRecognition 吗?您的代码使用最新版本可以顺利运行,但 3.1.3 版本似乎还没有该功能,并且也触发了我的错误。

    或者你的脚本文件名为speech_recognition.py?有人遇到了这个问题: Speech Recognition: AttributeError: module 'speech_recognition' has no attribute 'Recognizer'

    【讨论】:

      【解决方案2】:

      请尝试输入更少的命令

      将 .wav 文件粘贴到当前工作目录中

      然后删除 AUDIO_FILE

      然后输入:

      以 sr.AudioFile("english.wav") 作为源

      【讨论】:

        【解决方案3】:
        #!/usr/bin/env python3
        
        import speech_recognition as sr
        
        # obtain path to "english.wav" in the same folder as this script
        #from os import path
        #AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "english.wav")
        # AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
        # AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")
        
        #  use the audio file as the audio source
        # paste the .wav file int current working directory
        r = sr.Recognizer()
        with sr.AudioFile("english.wav") as source:
             audio = r.record(source)  # read the entire audio file
        

        【讨论】:

        • 嗨,您应该提供代码解释以及 sn-p 以改进您的答案
        猜你喜欢
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 2014-08-14
        • 2013-10-13
        • 2020-04-29
        • 2022-07-31
        相关资源
        最近更新 更多