简体   繁体   中英

Getting error with speech recognition 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()'''

This is returning error like

'''

/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.

I installed proper lib and also tried understanding things like pyaudio etc Anything to get rid of this,Yesterday i faced same thing with pysttx3 but it got resolved by rebooting laptop but this isnt working.

Your code should look like this:

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

First, import the library by typing this in your terminal-

pip install speechRecognition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM