我正在尝试在 ubuntu 21.10 上使用 SpeechRecognition 将语音转换为文本
以下是代码
import speech_recognition as sr
def listen():
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
with mic as source:
audio = r.listen(source, timeout=10)
return r.recognize_google(audio)
print(listen())
这将引发错误
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
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_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
Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1514
Expression 'ValidateParameters( inputParameters, hostApi, StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2818
Traceback (most recent call last):
File "/home/harsha/PycharmProjects/playground/audio.py", line 29, in <module>
assitant.listen()
File "/home/harsha/PycharmProjects/playground/audio.py", line 23, in listen
with mic as source:
File "/home/harsha/PycharmProjects/playground/sand/lib/python3.9/site-packages/speech_recognition/__init__.py", line 138, in __enter__
self.audio.open(
File "/home/harsha/PycharmProjects/playground/sand/lib/python3.9/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/home/harsha/PycharmProjects/playground/sand/lib/python3.9/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels
我正在使用内置麦克风的笔记本电脑进行此操作。