如果我使用现场麦克风,SpeechRecognition不会给出响应。
我已经成功地安装了SpeechRecognition库和PyAudio使用python3-m pip安装。。。我正在虚拟环境中使用Raspberry Pi Zero:Python 3.7.3pip 21.0.1
我正在使用Adafruit I2S MEMS麦克风突破,并能够使用成功录制
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
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
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)