无法在 Docker 上托管语音识别和声音播放应用程序

Posted

技术标签:

【中文标题】无法在 Docker 上托管语音识别和声音播放应用程序【英文标题】:Unable to host Speech Recognition and Sound Playing Application on Docker 【发布时间】:2019-11-27 21:15:36 【问题描述】:

我有一个应用程序,它从麦克风获取语音输入、执行语音识别并根据识别的提示词播放某些音乐文件。这个应用程序是用 Python 编写的,并实现了几个 Python 模块,如 PyAudio、Playsound 和 Websocket-client。我正在尝试构建此应用程序的 Docker 映像。

我创建了一个包含以下内容的 Dockerfile:

FROM ubuntu
ADD . /app

FROM python:3
ADD transcribe.py /
ADD speech.cfg /
ADD setup.cfg / 
ADD setup.py /

RUN apt-get update
RUN apt-get --assume-yes install libasound-dev portaudio19-dev     libportaudio2 libportaudiocpp0
RUN pip3 install pyaudio
RUN pip3 install websocket-client
RUN pip3 install playsound
CMD [ "python3", "./transcribe.py" ]

在尝试构建它时,我得到如下成功的构建响应:

ABCDEFG:random-folder User$ docker build -t storyassist .
Sending build context to Docker daemon  3.494MB
Step 1/13 : FROM ubuntu
 ---> 4c108a37151f
Step 2/13 : ADD . /app
 ---> e520eb6d11e8
Step 3/13 : FROM python:3
 ---> 42d620af35be
Step 4/13 : ADD transcribe.py /
 ---> 6c76aee20537
Step 5/13 : ADD speech.cfg /
 ---> 74c3221f2324
Step 6/13 : ADD setup.cfg /
 ---> 2c415c19a4fb
Step 7/13 : ADD setup.py /
 ---> c6382077cdcf
Step 8/13 : RUN apt-get update
 ---> Running in ace9adab6a44
Get:1 http://deb.debian.org/debian buster InRelease [118 kB]
Get:2 http://deb.debian.org/debian buster-updates InRelease [46.8 kB]
Get:3 http://deb.debian.org/debian buster/main amd64 Packages [7897 kB]
Get:4 http://security.debian.org/debian-security buster/updates     InRelease [39.1 kB]
Get:5 http://security.debian.org/debian-security     buster/updates/main amd64 Packages [42.5 kB]
Fetched 8143 kB in 3s (2493 kB/s)
Reading package lists...
Removing intermediate container ace9adab6a44
 ---> d46db350b95f
Step 9/13 : RUN apt-get --assume-yes install libasound-dev     portaudio19-dev libportaudio2 libportaudiocpp0
 ---> Running in 8d772b088b15
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libasound2 libasound2-data libjack-dev libjack0
Suggested packages:
  libasound2-plugins alsa-utils libasound2-doc jackd1 portaudio19-doc
The following NEW packages will be installed:
  libasound2 libasound2-data libasound2-dev libjack-dev libjack0     libportaudio2
  libportaudiocpp0 portaudio19-dev
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 1094 kB of archives.
After this operation, 5886 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian buster/main amd64 libasound2-    data all 1.1.8-1 [59.6 kB]
Get:2 http://deb.debian.org/debian buster/main amd64 libasound2     amd64 1.1.8-1 [361 kB]
Get:3 http://deb.debian.org/debian buster/main amd64 libasound2-dev     amd64 1.1.8-1 [145 kB]
Get:4 http://deb.debian.org/debian buster/main amd64 libjack0 amd64     1:0.125.0-3 [109 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 libjack-dev     amd64 1:0.125.0-3 [227 kB]
Get:6 http://deb.debian.org/debian buster/main amd64 libportaudio2     amd64 19.6.0-1 [66.6 kB]
Get:7 http://deb.debian.org/debian buster/main amd64     libportaudiocpp0 amd64 19.6.0-1 [18.8 kB]
Get:8 http://deb.debian.org/debian buster/main amd64 portaudio19-    dev amd64 19.6.0-1 [108 kB]
debconf: delaying package configuration, since apt-utils is not     installed
Fetched 1094 kB in 1s (1381 kB/s)
Selecting previously unselected package libasound2-data.
(Reading database ... 24544 files and directories currently     installed.)
Preparing to unpack .../0-libasound2-data_1.1.8-1_all.deb ...
Unpacking libasound2-data (1.1.8-1) ...
Selecting previously unselected package libasound2:amd64.
Preparing to unpack .../1-libasound2_1.1.8-1_amd64.deb ...
Unpacking libasound2:amd64 (1.1.8-1) ...
Selecting previously unselected package libasound2-dev:amd64.
Preparing to unpack .../2-libasound2-dev_1.1.8-1_amd64.deb ...
Unpacking libasound2-dev:amd64 (1.1.8-1) ...
Selecting previously unselected package libjack0:amd64.
Preparing to unpack .../3-libjack0_1%3a0.125.0-3_amd64.deb ...
Unpacking libjack0:amd64 (1:0.125.0-3) ...
Selecting previously unselected package libjack-dev.
Preparing to unpack .../4-libjack-dev_1%3a0.125.0-3_amd64.deb ...
Unpacking libjack-dev (1:0.125.0-3) ...
Selecting previously unselected package libportaudio2:amd64.
Preparing to unpack .../5-libportaudio2_19.6.0-1_amd64.deb ...
Unpacking libportaudio2:amd64 (19.6.0-1) ...
Selecting previously unselected package libportaudiocpp0:amd64.
Preparing to unpack .../6-libportaudiocpp0_19.6.0-1_amd64.deb ...
Unpacking libportaudiocpp0:amd64 (19.6.0-1) ...
Selecting previously unselected package portaudio19-dev:amd64.
Preparing to unpack .../7-portaudio19-dev_19.6.0-1_amd64.deb ...
Unpacking portaudio19-dev:amd64 (19.6.0-1) ...
Setting up libasound2-data (1.1.8-1) ...
Setting up libjack0:amd64 (1:0.125.0-3) ...
Setting up libasound2:amd64 (1.1.8-1) ...
Setting up libjack-dev (1:0.125.0-3) ...
Setting up libasound2-dev:amd64 (1.1.8-1) ...
Setting up libportaudio2:amd64 (19.6.0-1) ...
Setting up libportaudiocpp0:amd64 (19.6.0-1) ...
Setting up portaudio19-dev:amd64 (19.6.0-1) ...
Processing triggers for libc-bin (2.28-10) ...
Removing intermediate container 8d772b088b15
 ---> d2bc72b4654c
Step 10/13 : RUN pip3 install pyaudio
 ---> Running in 654227a7b259
Collecting pyaudio
  Downloading         https://files.pythonhosted.org/packages/ab/42/b4f04721c5c5bfc196ce156b3    c768998ef8c0ae3654ed29ea5020c749a6b/PyAudio-0.2.11.tar.gz
Building wheels for collected packages: pyaudio
  Building wheel for pyaudio (setup.py): started
  Building wheel for pyaudio (setup.py): finished with status     'done'
  Stored in directory:     /root/.cache/pip/wheels/f4/a8/a4/292214166c2917890f85b2f72a8e5f13e1ffa5    27c4200dcede
Successfully built pyaudio
Installing collected packages: pyaudio
Successfully installed pyaudio-0.2.11
Removing intermediate container 654227a7b259
 ---> 1336a7cbfb0f
Step 11/13 : RUN pip3 install websocket-client
 ---> Running in dfa7c4d9f195
Collecting websocket-client
  Downloading         https://files.pythonhosted.org/packages/29/19/44753eab1fdb50770ac696055    27e8859468f3c0fd7dc5a76dd9c4dbd7906/websocket_client-0.56.0-py2.py3-    none-any.whl (200kB)
Collecting six (from websocket-client)
  Downloading     https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238    ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Installing collected packages: six, websocket-client
Successfully installed six-1.12.0 websocket-client-0.56.0
Removing intermediate container dfa7c4d9f195
 ---> e82fd88cd21a
Step 12/13 : RUN pip3 install playsound
 ---> Running in 2f2af7ce7bdc
Collecting playsound
  Downloading         https://files.pythonhosted.org/packages/f5/16/10d897b0a83fb4b05b03a63d7    a2667ab75f857f67f7062fd447dd3f49bf7/playsound-1.2.2-py2.py3-none-any.whl
Installing collected packages: playsound
Successfully installed playsound-1.2.2
Removing intermediate container 2f2af7ce7bdc
 ---> ac6c2e571e3b
Step 13/13 : CMD [ "python3", "./transcribe.py" ]
 ---> Running in 11291eaff8f4
Removing intermediate container 11291eaff8f4
 ---> 2d8cd222b2bb
Successfully built 2d8cd222b2bb
Successfully tagged storyassist:latest

但是当我尝试运行他的镜像时,我得到了一堆错误,我的 docker 镜像找不到指定的驱动程序。虽然可以解决虚拟机中的类似问题,但我不确定是否有一种简单的方法可以在 docker 容器中解决同样的问题。

ABCDEFG:random-folder User$ docker run storyassist
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_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:4568:(_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:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_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:4568:(_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:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_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:4568:(_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:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_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:4568:(_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:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_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:4568:(_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:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM dmix
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/threading.py", line 926, in     _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "./transcribe.py", line 83, in read_audio
    RATE = int(p.get_default_input_device_info()    ['defaultSampleRate'])
  File "/usr/local/lib/python3.7/site-packages/pyaudio.py", line     949, in get_default_input_device_info
    device_index = pa.get_default_input_device()
OSError: No Default Input Device Available

【问题讨论】:

【参考方案1】:

我确信当docker run 命令中缺少必需的标志时会出现问题。

您只需将主机设备添加到您的容器中。

将主机设备添加到容器(--device)

$ docker run --device=/dev/sdc:/dev/xvdc \
             --device=/dev/sdd --device=/dev/zero:/dev/nulo \
             -i -t \
             ubuntu ls -l /dev/xvdc,sdd,nulo

brw-rw---- 1 root disk 8, 2 Feb  9 16:05 /dev/xvdc
brw-rw---- 1 root disk 8, 3 Feb  9 16:05 /dev/sdd
crw-rw-rw- 1 root root 1, 5 Feb  9 16:05 /dev/nulo

通常需要将设备直接暴露给容器。这 --device 选项启用该功能。例如,可以将特定的块存储设备或循环设备或音频设备添加到其他 非特权容器(没有 --privileged 标志)并具有 应用程序直接访问它。

默认情况下,容器将能够读取、写入和 mknod 这些 设备。这可以使用第三组 :rwm 选项来覆盖 每个 --device 标志:

如果上述命令不起作用,那么您可能需要在 docker 镜像中添加驱动程序。

sudo apt-get install -y alsa-base alsa-utils

https://packages.debian.org/search?keywords=alsa-base

docker run -it --device /dev/snd:/dev/snd py_sound

【讨论】:

【参考方案2】:

我在 ubuntu 20 中解决了这个问题,使用 python:3 docker 镜像,下一步:

在 Dockerfile 中添加

RUN apt-get install -y pulseaudio

添加 docker run 命令

--device /dev/snd \
-e PULSE_SERVER=unix:$XDG_RUNTIME_DIR/pulse/native \
-v $XDG_RUNTIME_DIR/pulse/native:$XDG_RUNTIME_DIR/pulse/native \
-v ~/.config/pulse/cookie:/root/.config/pulse/cookie \
-v /media/dyan/project/projects/voice/:/data/voice \

命令示例:

 docker run -it --rm \
     --device /dev/snd \
     -e PULSE_SERVER=unix:$XDG_RUNTIME_DIR/pulse/native \
     -v $XDG_RUNTIME_DIR/pulse/native:$XDG_RUNTIME_DIR/pulse/native \
     -v ~/.config/pulse/cookie:/root/.config/pulse/cookie \
     -v /media/dyan/project/projects/voice/:/data/voice \
    --name python-speech-recognition python-speech-recognition-app

【讨论】:

以上是关于无法在 Docker 上托管语音识别和声音播放应用程序的主要内容,如果未能解决你的问题,请参考以下文章

尝试在使用 SpeechKit 进行语音识别之前播放系统声音

在 Ubuntu 18.04 上托管使用 docker 构建的 Web 应用程序

音乐机器人不在 Heroku 上播放音乐,但在本地

使用 Docker 在 Linux 上托管 ASP.NET Core 应用程序

iOS :AudioSession详解 Category选择 听筒扬声器切换

Heroku 上托管的应用程序无法加载网页