微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python SpeechRecognition 没有响应

如何解决Python SpeechRecognition 没有响应

如果我使用现场麦克风,SpeechRecognition 不会给出响应。

我已经使用 python3 -m pip install 成功安装了 SpeechRecognition 库和 PyAudio ... 我正在虚拟环境中使用 RaspBerry Pi Zero: 蟒蛇 3.7.3 点子 21.0.1

我正在使用 Adafruit I2S MEMS Microphone Breakout 并且能够使用以下方法成功录制:

arecord -D plughw:1 -c1 -r 48000 -f S32_LE -t wav -V mono -v file.wav

当我将其保存在文件 (file.wav) 中并使用以下代码时,语音识别器就可以工作了:

import speech_recognition as sr

r = sr.Recognizer()
with sr.AudioFile('file.wav') as source:
        audio = r.record(source)

try:
        print("You said: " + r.recognize_google(audio))
except sr.UnkNowValueError:
        print("Sorry,Could not understand audio")
except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

但是当我想使用以下代码进行实时语音识别时:

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone(1) as source:
        print("Say something!")
        audio = r.listen(source)

try:
        print("You said: " + r.recognize_google(audio))
except sr.UnkNowValueError:
        print("Sorry,Could not understand audio")
except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

它给出以下输出并一直说:“说点什么”而不显示任何成绩单:

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.front
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.side
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.surround40
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.surround41
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.surround50
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.surround51
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.surround71
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.phoneline
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM cards.pcm.phoneline
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1,skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1,skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1,skipping unlock
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) UnkNown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) UnkNown field port
ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib conf.c:5014:(snd_config_expand) UnkNown parameters {AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2  CARD 0}
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) UnkNown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2  CARD 0}
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1,skipping unlock
Say something!

当我在一段时间后按 ^C 时,因为没有发生任何事情,会添加以下位:

^CTraceback (most recent call last):
  File "srwm.py",line 6,in <module>
    audio = r.listen(source)
  File "/home/pi/.virtualenvs/pythonyt/lib/python3.7/site-packages/speech_recognition/__init__.py",line 620,in listen
    buffer = source.stream.read(source.CHUNK)
  File "/home/pi/.virtualenvs/pythonyt/lib/python3.7/site-packages/speech_recognition/__init__.py",line 161,in read
    return self.pyaudio_stream.read(size,exception_on_overflow=False)
  File "/home/pi/.virtualenvs/pythonyt/lib/python3.7/site-packages/pyaudio.py",line 608,in read
    return pa.read_stream(self._stream,num_frames,exception_on_overflow)
KeyboardInterrupt

如果需要更多信息,请告诉我。 我希望有真正聪明的人可以帮助我,在此先感谢!

第一次回答后的新代码

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone(1) as source:
        print("Say something!")
        audio = r.listen(source)

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

return data 

给出返回在函数外的错误

解决方法

我猜问题出在将语音翻译成文本的部分...试试这个:

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

return data

我猜这是可以工作的完整代码:

import speech_recognition as sr

def Recognize():
    r = sr.Recognizer()
    with sr.Microphone(1) as source:
        print("Say something!")
        audio = r.listen(source)

    data = ""
    try:
        data = r.recognize_google(audio)
        print("You said: " + data)
    except Exception as e:
        print("Sorry,could not understand audio")
        print(e)

    return data 

if __name__=="__main__":
    data = Recognize().lower()
    print(data)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。