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

时间:2019-07-18 21:36:53

标签: python macos docker alsa pyaudio

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

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

print

尝试构建它时,我得到一个成功的构建响应,如下所示:

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" ]

但是当我尝试破坏他的映像时,我遇到了很多错误,我的泊坞映像无法找到指定的驱动程序。虽然可以解决虚拟机中的类似问题,但我不确定在Docker容器中是否有解决该问题的简单方法。

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

2 个答案:

答案 0 :(得分:0)

我确信在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

答案 1 :(得分:0)

我在ubuntu 20中解决了这个问题,使用python:3 docker image,接下来做:

在 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