语音识别python代码无法正常工作

时间:2015-08-14 08:13:02

标签: python speech-recognition

我在Python 2.7中运行以下代码并安装了pyAudio。

import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:                # use the default microphone as the audio source
    audio = r.listen(source)                   # listen for the first phrase and extract it into audio data

try:
    print("You said " + r.recognize(audio))    # recognize speech using Google Speech Recognition
except LookupError:                           # speech is unintelligible
    print("Could not understand audio")

输出指示闪烁。而已。请帮助,因为我是新手。

14 个答案:

答案 0 :(得分:4)

可能的原因可能是recognizer_instance.energy_threshold属性可能设置为一个太高而无法启动的值。您应该降低此阈值,或致电recognizer_instance.adjust_for_ambient_noise(source, duration = 1)。您可以在Speech Recognition

了解有关它的更多信息

答案 1 :(得分:2)

您尝试更换

    print("You said " + r.recognize(audio))
    except LookupError:                          
    print("Could not understand audio")

使用

    text = r.recognize_google(audio)
    print("You said : {}".format(text))
    text = r.recognize_google(audio)
    except:
        print("Sorry could not recognize your voice")

通过运行以下命令确保pyaudio.h已安装

    sudo apt-get install portaudio19-dev python-pyaudio python3-pyaudio

答案 2 :(得分:1)

检查麦克风的输入音量。在ubuntu中默认设置为0(在我的例子中)。由于您的程序卡在audio = r.listen(source)行,这只是意味着麦克风无法收听任何语音输入。希望这会有所帮助。

答案 3 :(得分:0)

除了Tushar的回答,我建议尝试使用更好的外置USB麦克风。 PyAudio可能会出现内置笔记本电脑麦克风的问题。

答案 4 :(得分:0)

将Try和Except放在缩进中。

这是我的工作代码: -

`while True:
    r=sr.Recognizer()
    with sr.Microphone() as source:
        print("Say Something")
        audio=r.listen(source)
    try:    
        print(r.recognize_google(audio),"\n")
    except:
        pass`

答案 5 :(得分:0)

您可能必须先安装这些东西:

  1. pip install pyaudio
  2. pip install --upgrade pyaudio
  3. pip install wheel
  4. pip install google-api-python-client
  5. sudo apt-get install flac
  6. pip install monotonic
  7. pip install SpeechRecognition

之后,请参考站点(https://realpython.com/python-speech-recognition/) 它会清楚地说明您想要什么。


在这里,我附上我从该站点编辑的代码。由于我是新手,所以它并不完美,但是我已经尝试过了。这是为了检查天气,语音输入与我输入的文字相似,并且还会打印您所说的内容。


#!/usr/bin/ python
import time

import speech_recognition as sr


def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.

    Returns a dictionary with three keys:
    "success": a boolean indicating whether or not the API request was
           successful
    "error":   `None` if no error occured, otherwise a string containing
           an error message if the API could not be reached or
           speech was unrecognizable
    "transcription": `None` if speech could not be transcribed,
           otherwise a string containing the transcribed text
    """
    # check that recognizer and microphone arguments are appropriate type
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    # adjust the recognizer sensitivity to ambient noise and record audio
    # from the microphone
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    # set up the response object
    response = {
        "success": True,
        "error": None,
        "transcription": None
    }

    try:
        response["transcription"] =    recognizer.recognize_google(audio)
    except sr.RequestError:
        # API was unreachable or unresponsive
        response["success"] = False
        response["error"] = "API unavailable"
    except sr.UnknownValueError:
        # speech was unintelligible
        response["error"] = "Unable to recognize speech"

    return response


if __name__ == "__main__":

    NUM_GUESSES = 1
    PROMPT_LIMIT = 2
    # create recognizer and mic instances
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()

    word = "hello world"

    time.sleep(3)

    for i in range(NUM_GUESSES):
        for j in range(PROMPT_LIMIT):
            print('Guess {}. Speak!'.format(i+1))
            guess = recognize_speech_from_mic(recognizer, microphone)
            if guess["transcription"]:
                break
            if not guess["success"]:
                break
            print("I didn't catch that")

        # if there was an error, stop the game
        if guess["error"]:
            print("ERROR: {}".format(guess["error"]))
            break

        # show the user the transcription
        print("You said: {}".format(guess["transcription"]))

        # determine if guess is correct and if any attempts remain
        guess_is_correct = guess["transcription"].lower() == word.lower()
        user_has_more_attempts = i < NUM_GUESSES - 1

        if guess_is_correct:
            print("Correct!".format(word))
            break
        elif user_has_more_attempts:
            print("Incorrect. Try again.\n")
        else:
            print("Sorry, output is not similar to '{}'.".format(word))
            break

答案 6 :(得分:0)

我想问题出在持续时间上。如果您为语音识别引擎设置了一定的持续时间,则该问题将得到解决。 尝试以下代码:

import speech_recognition as sr
r = sr.Recognizer()                                                                                   
with sr.Microphone() as source:                                                                      
    speak(sen)
    print("listening...")
    audio = r.record(source,duration=3)
    try:
        str=r.recognize_google(audio)
        print(str)
    except:
        print("some error occurred!")

答案 7 :(得分:0)

尝试添加

r.adjust_for_ambient_noise(source,duration=1)

其中r是识别器实例,像这样

import speech_recognition as sr

r=sr.Recognizer()
print(sr.Microphone.list_microphone_names())
with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source,duration=1)
    # r.energy_threshold()
    print("say anything : ")
    audio= r.listen(source)
    try:
        text = r.recognize_google(audio)
        print(text)
    except:
        print("sorry, could not recognise")

答案 8 :(得分:0)

尝试一下;

pip install sounddevice

有效。

答案 9 :(得分:0)

set minimum threshold

运行python -m speech_recognition命令后。设置显示的最小能量阈值。

Setting procedure:Ctrl,然后单击鼠标Recognizer()。现在设置能量阈值。

答案 10 :(得分:0)

尝试此代码:

r = sr.Recognizer()

with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source=source)
        audio = r.listen(source,timeout=3)
        

    
    data = ''
    try :
        data = r.recognize_google(audio)
        print(data)

    except sr.UnknownValueError:
        print(" Error")
        
    except sr.RequestError as e:
        print("Request Error")

或如上所述将超时和r.adjust_for_ambient_noise(source=source)添加到您的代码中。任何人都可以帮助我this

答案 11 :(得分:0)

我的问题是:程序运行没有任何错误,但没有显示任何输出

这是有问题的代码:

import googletrans
import speech_recognition as sr

recognizer = sr.Recognizer()
translator = googletrans.Translator()

try:
    with sr.Microphone() as source:
        print('Speak Now')
        voice= recognizer.listen(source)
        text= recognizer.recognize_google(voice)
        print(text)

except:
     pass

translated = translator.translate(text, dest='es')
print(translated.text)

解决这个问题: 基本上这个问题是由背景噪音或环境噪音引起的 所以只需在您的代码中添加一行,即: recognizer.adjust_for_ambient_noise(source)#recognizer is on line 4 from above code

######## 改进和解决的问题代码: ########

import googletrans
import speech_recognition as sr

recognizer = sr.Recognizer()
translator = googletrans.Translator()

try:
    with sr.Microphone() as source:
        print('Speak Now')
        recognizer.adjust_for_ambient_noise(source)#(Problem Solved)
        voice= recognizer.listen(source)
        text= recognizer.recognize_google(voice)
        print(text)

except:
     pass

translated = translator.translate(text, dest='es')
print(translated.text)

答案 12 :(得分:0)

尝试使用以下方法更改麦克风索引:

with sr.Microphone(device_index = 0) as source:

要了解您的麦克风索引使用情况:

print((sr.Microphone.list_microphone_names()))

如果这不起作用,请使用:

with sr.Microphone(device_index = 0) as source:                # use the default microphone as the audio source
   r.adjust_for_ambient_noise(source, duration = 1)
   audio = r.listen(source)

答案 13 :(得分:-2)

在您的python提示符中安装(Anaconda提示符) 1. pip安装pyaudio 2. pip install --upgrade pyaudio 3.点安装轮 4.点子安装google-api-python-client 5.点安装单调 6. pip安装SpeechRecognition

将语音识别输入为sr r = sr.Recognizer()
带有sr.Microphone()作为来源:
打印(“正在听...”) 音频= r.record(源,持续时间= 3) 尝试: str = r.recognize_google(音频) 打印(str) 除: 打印(“发生一些错误!”)