python - 使用 Docker 在 jupyter 中运行 OpenCv

标签 python opencv docker jupyter

我想制作一个易于运行的 jupyter支持 OpenCV 并作为 Docker 镜像交付的笔记本。

概念是让一个运行jupyter内核的docker容器,通过宿主机中的浏览器访问notebook。类似于 this 的东西.

但是,问题在于 OpenCV 似乎依赖于正在运行的 Gtk 环境。因此尝试运行以下代码:

import numpy as np
import cv2
img = cv2.imread('pendulum.png',0)
cv2.imshow('image',img) 

导致 jupyter 内核崩溃:

(image:603): Gtk-WARNING **: cannot open display: 
[I 15:23:49.808 NotebookApp] KernelRestarter: restarting kernel (1/5)

有没有办法绕过这种依赖关系,让运行在 docker 容器中的 OpenCV 在主机系统的浏览器中显示图像?


重现问题的步骤:

docker 文件:

FROM ubuntu:16.04

RUN apt-get update

RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
    libglib2.0-0 libxext6 libsm6 libxrender1 \
    git mercurial subversion

RUN echo 'export PATH=/opt/conda/bin:$PATH' > /etc/profile.d/conda.sh && \
    wget --quiet https://repo.continuum.io/archive/Anaconda3-4.0.0-Linux-x86_64.sh && \
    /bin/bash /Anaconda3-4.0.0-Linux-x86_64.sh -b -p /opt/conda && \
    rm /Anaconda3-4.0.0-Linux-x86_64.sh

ENV PATH /opt/conda/bin:$PATH

RUN conda install -y -c https://conda.binstar.org/menpo opencv3

RUN apt-get install -y libgomp1
RUN apt-get install -y libgtk2.0-0 x11-xserver-utils libcanberra-gtk3-module
RUN mkdir /home/user
RUN groupadd -r user -g 777 && \
useradd -u 431 -r -g user -d /home/user -s /sbin/nologin -c "Docker image user" user

RUN apt-get install -y libcanberra-gtk*

RUN chown -R user:user /home/user

USER user
WORKDIR /home/user

要执行的命令:

docker build -t opencv-play .
docker run -v /home/user/.Xauthority:/home/user/.Xauthority -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY -u user -v `pwd`:/home/user -p 8008:8008 -t -i opencv-play
jupyter notebook --ip='*' --no-browser --port=8008   #Inside the container
#Open the browser, URL-> http://localhost:8008
#Run the above code in jupyter

最佳答案

有两种可能的解决方案:

  1. 允许您的容器访问您的本地 XServer。这将显示所有图形输出,就像您直接在主机上而不是在 Docker 容器中运行软件一样。为此,您需要设置 DISPLAY 环境变量,并且需要将 X11 套接字传递给容器。在您的示例中,您将:

    $ docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -v `pwd`:/home/workspace -p 8008:8008 -t -i opencv-play
    
    • -v/tmp/.X11-unix:/tmp/.X11-unix 将X11套接字传递给容器
    • -e DISPLAY=$DISPLAY 将显示环境变量设置为主机系统上的变量
  2. 如果您不需要图形输出,您可以运行一个不显示任何输出的“假”X 服务器。 Xvfb 就是这样一个显示服务器,它不需要访问监视器。为了使用它,您需要在您的图像中安装 Xvfb,即将 apt-get install xvfb 添加到您的 Dockerfile。然后,当你运行容器时,你首先需要启动 xvfb 并相应地设置 DISPLAY。我通常使用一个小脚本来执行此操作,然后启动您的命令,例如

    #!/bin/bash
    export DISPLAY=0:0
    Xvfb $DISPLAY &
    jupyter notebook --ip='*' --no-browser --port=8008
    

    这会在后台启动 Xvfb,然后启动 jupyter,它将所有图形输出传递到 0:0 上的 Xvfb 显示服务器。将此脚本添加到图像中,然后按以下方式运行:

    docker run -v `pwd`:/home/workspace -p 8008:8008 -t -i opencv-play /path/to/the/script
    

    请注意,您不需要DISPLAY 传递给容器。

关于python - 使用 Docker 在 jupyter 中运行 OpenCv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36798277/

相关文章:

docker - Kubernetes - 无法在 Azure 文件共享上安装 Windows 路径(Linux 安装正常)

qt - Qt Creator未检测到opencv

c++ - 使用鼠标手动提取对象

指向 OpenCV GpuMat 的 C++ 指针

docker - 如何使用 rabbitmq docker compose yml 文件构建 docker 镜像?

Docker API 推送到私有(private)注册表错误

Python XML 解析无法找到 child 的 child

python - 当我使用 QtGui.QIntValidator() 时,它将文本的最大长度设置为 10 个整数?

python - 强制 .ix 返回 pandas 中的 DataFrame

python - 圆环图 python