python - 语音识别python出错
问题描述
import SpeechRecognition as sp
def takeCommand():
r = sp.Recognizer()
with sp.Microphone() as source:
print("Listening....")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognising...")
query = r.recognize_google(audio,language="en-in")
print(query)
except Exception as e:
print(e)
speak("say that again please")
return None
return query
takeCommand()'''
这是返回错误,例如
'''
/usr/bin/python3 /home/shanu/Documents/Pygame/Sierra_AI_Assistant/sierra.py
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
aplay: main:830: audio open error: Device or resource busy
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
aplay: main:830: audio open error: Device or resource busy
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
aplay: main:830: audio open error: Device or resource busy
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
aplay: main:830: audio open error: Device or resource busy
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
aplay: main:830: audio open error: Device or resource busy
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:869:(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 pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
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
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
Listening....
python3: src/hostapi/alsa/pa_linux_alsa.c:3641: PaAlsaStreamComponent_BeginPolling: Assertion `ret == self->nfds' failed.
我安装了正确的库,并尝试理解 pyaudio 等任何东西来摆脱这个,昨天我在 pysttx3 上遇到了同样的事情,但它通过重新启动笔记本电脑得到了解决,但这不起作用。
解决方案
您的代码应如下所示:
import speechRecognition as sr
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in') #Using google for voice recognition.
print(f"User said: {query}\n") #User query will be printed.
except Exception as e:
# print(e)
print("Say that again please...") #Say that again will be printed in case of improper voice
return "None" #None string will be returned
return query
首先,通过在终端中输入以下内容来导入库 -
pip install speechRecognition
推荐阅读
- javascript - 在 setState() 中使用 Math.pow() 方法
- ruby - 如何定义接收可变参数的方法生成
- notifications - 使用 Graylog 监控资源+通知
- rest - Maximo REST 查询参数 _uw
- react-native - 在 React Native 中,我如何访问本机相机应用程序?
- git - git不让我推到master
- c# - 从列表到图形界面
- excel - 缺少对 Outlook 的引用,导致 Excel 错误:无法在 Excel 2010 中加载 DLL
- javascript - AngularJS 1.x 中的 Ag-grid 服务器端行模型无限 getRows 循环
- javascript - 应用程序进入后台,10 分钟后停止。*$间隔函数*