如何在 Python 的 SpeechRecognition 中使用音频文件作为音频源?

How can I use audio file as audio source in SpeechRecognition in Python?

我在 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

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

或者您的脚本的文件名是 speech_recognition.py?有人遇到了这个问题:

改为尝试输入更少的命令

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

然后删除 AUDIO_FILE

并输入:

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

#!/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