【问题标题】:Python code for Speech Recognition not working用于语音识别的 Python 代码不起作用
【发布时间】:2020-09-12 16:37:27
【问题描述】:

我解决了关于这个主题的所有类似问题并尝试了所有方法,但没有任何效果。

我尝试了以下链接的解决方案: speech recognition python stopped in listen SpeechRecognition producing OSError: No Default Input Device Available Python, Speech Recognition stuck at 'Listening...' speech recognition python code not working

import speech_recognition as sr

def get_audio():
    r =  sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source, duration=5)
        print("listening... ")
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return said.lower()         

print(get_audio())

我得到的错误是:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

以下命令卡在“Say Something..”,什么也不做。

 python -m speech_recognition 

我还尝试了以下代码来检查默认音频设备:

import pyaudio
print(pyaudio.pa.get_default_input_device())

输出:

OSError: No Default Input Device Available

我在这里做错了什么?

【问题讨论】:

  • 你能告诉你的电脑规格
  • 顺便说一句,你没有可用的麦克风
  • PC 规格:Windows 8.1,8 GB RAM。但我可以录制音频并使用 pyaudio 将其保存在文件中。
  • 能否提供截图
  • 什么截图?

标签: python python-3.x google-speech-api


【解决方案1】:

正如上面的 cmets 所说,您似乎没有任何可用的麦克风。

在使用语音识别时,我通常会在听之前询问用户使用哪个设备。

例如,使用这个实现:

# Identify the microphone device to use
def get_microphone_device():
    microphone_list = []
    for index, name in enumerate(sr.Microphone.list_microphone_names()):
        microphone_list.append("Microphone {1} `(device_index={0})`".format(index, name))

    questions = [
    inquirer.List('microphone',
            message = "What Microphone will you use?",
            choices = microphone_list,
        ),
    ]
    answers = inquirer.prompt(questions)
    microphone = answers["microphone"]
    microphone = re.search("(?=`)(.*)", microphone).group(0)
    device = re.search("[0-9]", microphone).group()
    return device

然后,要使用设备并获取消息,您可以照常关注,例如:

# Listen to the speaker through the microphone device
def get_speech(device):
    rec = sr.Recognizer()
    with sr.Microphone(device_index=int(device)) as source:
        print("Speak, we are listening:")
        audio = rec.listen(source)
        try:
            text = rec.recognize_google(audio)
            print("You said: \"{}\"".format(text))
        except:
            print("Sorry, we couldn't recognize what you said")
    return text

Here is an example of the full implementation

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多