D
D
Deleting Account2020-11-22 20:55:10
Python
Deleting Account, 2020-11-22 20:55:10

How to solve the problem with the speech_recognition library when recognizing speech on Ubuntu OS?

The code

import speech_recognition as sr


# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

# recognize speech using Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))


Startup messages
spoiler

ALSA lib pcm_dsnoop.c:641:(snd_pcm_dsnoop_open) unable to open slave
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
Cannot lock down 82280346 byte memory area (Cannot allocate memory)
Cannot lock down 82280346 byte memory area (Cannot allocate memory)
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
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
Cannot lock down 82280346 byte memory area (Cannot allocate memory)


When called r.listen(source), the program should receive sound from the microphone, but does not.
The microphone is working. I tried to run from all devices through
sr.Microphone(device_index=int)  # вместо int указывал индекс звукового устройства

Documentation: https://github.com/Uberi/speech_recognition
Tried reinstalling pulseaudio. I should note that when listening in the background , speech is recognized, but some of the messages above (the ones in the spoiler) are printed to the console.

Fixed after adding r.adjust_for_ambient_noise(source)
Notes: the message indicated in the spoiler - remained.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-11-22
@hottabxp

Look here - Linux: ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open...
Or try running the script with sudo, maybe you don't have any rights.
Or try adding your user to the audio group, or whatever else is there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question