简体   繁体   English

语音识别的 Python 代码不起作用

[英]Python code for Speech Recognition not working

I went through all the similar questions on this topic and tried everything, but nothing's working.我浏览了有关此主题的所有类似问题并尝试了所有方法,但没有任何效果。

I tried solutions from the following links : 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 etc.我尝试了以下链接中的解决方案: 语音识别 python 停止监听SpeechRecognition 产生 OSError: No Default Input Device Available Python,Speech Recognition 卡在“Listening...” 语音识别 python 代码不起作用等。

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())

Error that I am getting is:我得到的错误是:

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

Following command gets stuck at 'Say Something..' and does nothing.以下命令卡在“说点什么……”并且什么也不做。

 python -m speech_recognition 

I also tried following code to check the default audio device :我还尝试了以下代码来检查默认音频设备:

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

Ouput :输出:

OSError: No Default Input Device Available

What am I doing wrong here?我在这里做错了什么?

As said in the comments above, you don't seem to have any microphone available.正如上面的评论中所说,您似乎没有任何可用的麦克风。

When using Speech Recognition, I usually ask the user which device to use before listening.在使用语音识别时,我通常会在听之前询问用户使用哪种设备。

For example, using this implementation:例如,使用这个实现:

# 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

And then, to use the device and get the message, you can follow as you did, for example:然后,要使用设备并获取消息,您可以按照您的方式进行操作,例如:

# 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 这是完整实现的示例

I hope it helped!我希望它有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM