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

有些话、适合烂在心里 提交于 2021-02-04 16:36:12

问题


I used speech_recognition.AudioFile in Python 3.6 , but this error was indicated:

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

This is my code:

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

In addition I am using speech_recognition 3.1.3.


回答1:


Can you change to a newer version of SpeechRecognition? Your code works smoothly using the latest version, but version 3.1.3 doesn't seem to have that feature yet and triggers the error for me aswell.

Alternatively is the filename of your script called speech_recognition.py? Someone had that problem: Speech Recognition: AttributeError: module 'speech_recognition' has no attribute 'Recognizer'




回答2:


Instead, try typing fewer commands

Paste the .wav file in the current working directory

then remove AUDIO_FILE

and type:

with sr.AudioFile("english.wav") as source




回答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


来源:https://stackoverflow.com/questions/59175253/how-can-i-use-audio-file-as-audio-source-in-speechrecognition-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!