[Python] Speech recognition on docker image

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am able to build an image from the file. However, when I try to use the speech recognition I receive an error.
    This is my dockerfile

    FROM python:3.9
    ## user creation and application dir
    RUN apt-get update
    RUN apt-get install apt-utils
    RUN apt-get install -y libasound-dev libportaudio2 libportaudiocpp0 portaudio19-dev libsndfile1
    RUN apt-get install -y alsa-utils
    RUN mkdir /app
    RUN useradd -m worker
    RUN chown worker /app

    #modifying python path and upgrading pip
    USER worker
    ENV PATH="/home/worker/.local/bin:${PATH}"
    RUN python -m pip install --upgrade pip
    WORKDIR /app

    #adding main files
    ADD speech.py /app
    ADD requirements.txt /app

    #installing requirements
    RUN pip install -r requirements.txt
    RUN pip install SpeechRecognition
    RUN pip install pyaudio
    ADD templates /app/templates


    EXPOSE 5000
    CMD ["python", "speech.py"]


    speech.py script

    import speech_recognition as sr
    from flask import Flask, render_template, redirect, session

    app = Flask(__name__)
    app.secret_key = 'command'


    @app.route('/spee')
    def home():
    return render_template('speech.html')


    @app.route('/speech', methods=['POST', 'GET'])
    def speech():
    try:
    li = []
    listen(li)
    session['command'] = li[0]
    ht = '''<!DOCTYPE html>
    <html lang="en"><head>
    </head>
    <meta charset="UTF-8">
    '''
    ht+= f'<h1> did you mean: "{li[0]}"'
    ht+='''
    <form action="/search">
    <button type="submit">Yes</button>
    </form>
    <form action="/">
    <button type="submit">No</button>
    </form>


    </html>'''
    return ht
    except:
    return 'speech not recognized'


    @app.route('/search', methods=['POST'])
    def search():
    command = session['command']
    url = f'https://www.google.com/search?q={command}'
    return redirect(url)


    def listen(li):
    import speech_recognition as sr

    r = sr.Recognizer()
    with sr.Microphone() as source:
    audio = r.listen(source)
    command = r.recognize_google(audio)
    li.append(command)


    if __name__ == '__main__':
    app.run(host='0.0.0.0', )


    Here is the error, I tried installing alsa-utils. I tried to run the docker with privileged flag. And I tried removing the user and performing all the commands as root. None of those worked

    ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
    ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
    ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
    ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM sysdefault
    ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
    ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
    ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
    ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM sysdefault
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
    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.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
    ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
    ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
    ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
    ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM default
    ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
    ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
    ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM default
    ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
    ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
    ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
    ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
    ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
    ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM dmix


    Ran lspci shows nothing

    Continue reading...

Compartilhe esta Página