首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用语音识别python时出错

使用语音识别python时出错
EN

Stack Overflow用户
提问于 2020-07-22 14:00:04
回答 1查看 283关注 0票数 0
代码语言:javascript
运行
复制
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()'''

这是返回错误,如

“”“

代码语言:javascript
运行
复制
/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一样的事情,但通过重启笔记本电脑解决了这个问题,但这不起作用。

EN

回答 1

Stack Overflow用户

发布于 2021-04-03 02:12:52

您的代码应如下所示:

代码语言:javascript
运行
复制
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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63027846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档