简体   繁体   English

cv2.VideoCapture 在 mac 主机上的 docker 容器中不起作用

[英]cv2.VideoCapture not working in docker container on a mac host

I am unable to open the web cam or capture a video in an Ubuntu 16.04 docker container on a Mac OS Sierra 10.12.6 Host.我无法在 Mac OS Sierra 10.12.6 主机上的 Ubuntu 16.04 docker 容器中打开网络摄像头或捕获视频。

vid = cv2.VideoCapture(0)

where vid.isOpened() returns a False and ret, img = vid.read() returns False, None其中vid.isOpened()返回Falseret, img = vid.read()返回False, None

I didnt just use opencv from pip.我不只是使用 pip 中的 opencv。 I compiled cv2 from the source.我从源代码编译了 cv2。 ffmpeg should be installed.应安装 ffmpeg。 Is the container not able to connect to the webcam device?容器是否无法连接到网络摄像头设备?

I tried running the docker with:我尝试使用以下命令运行 docker:

docker run -it --privileged --device=/dev/video0:/dev/video0 --entrypoint /bin/bash <ImageID>

My dockerfile is below:我的 dockerfile 如下:

#
# Ubuntu Dockerfile
#
# https://github.com/dockerfile/ubuntu
#

# Pull base image.
FROM ubuntu:16.04

# Install.
RUN \
  sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \
  apt-get update && \
  apt-get -y upgrade && \
  apt-get install -y build-essential && \
  apt-get install -y software-properties-common && \
  apt-get install -y byobu curl git htop man unzip vim wget && \
  apt-get install -y cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev && \
  apt-get install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg8-dev libpng12-dev libtiff5-dev libjasper-dev libdc1394-22-dev && \
  apt-get install -y libxvidcore-dev libx264-dev && \
  apt-get install -y libgtk-3-dev && \
  apt-get install -y libatlas-base-dev gfortran && \
  apt-get install -y libopencv-dev && \
  rm -rf /var/lib/apt/lists/*


RUN apt-get update && apt-get install python2.7-dev python3.5-dev

# Set environment variables.
ENV HOME /root

# Define working directory.
WORKDIR /root

# Get OpenCV
RUN wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
RUN unzip opencv.zip
RUN wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
RUN unzip opencv_contrib.zip

# GET PIP
RUN apt-get update && apt-get install -y python3-pip
RUN pip3 install numpy

# Build OpenCV
## NOTE: cd wont work by itself, need to be with the actual command to be performed
WORKDIR /root/opencv-3.1.0
RUN mkdir build
WORKDIR /root/opencv-3.1.0/build
RUN cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_PYTHON_EXAMPLES=ON \
  -D INSTALL_C_EXAMPLES=OFF \
  -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
  -D BUILD_EXAMPLES=ON \
  -D PYTHON_EXECUTABLE=/usr/bin/python3.5 \
  -D PYTHON_PACKAGES_PATH=/usr/local/lib/python3.5/site-packages \
  -D PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.5/dist-packages/numpy/core/include ..

RUN make -j4
RUN make install
RUN ldconfig

## RENAME BUILT cv2.so
WORKDIR /usr/local/lib/python3.5/dist-packages
RUN mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
WORKDIR /usr/local/lib/python3.5/site-packages
RUN ln -s /usr/local/lib/python3.5/dist-packages/cv2.so cv2.so

## DELETE DIRECTORIES
# WORKDIR /root
# RUN rm -rf opencv-3.1.0 opencv_contrib-3.1.0 opencv.zip opencv_contrib.zip

# Define default command.
CMD ["bash"]

You cannot do this using Docker for Mac.您不能使用 Docker for Mac 执行此操作。 The problem is that Docker runs Hyperkit which in turn is based on Xhyve问题是 Docker 运行Hyperkit ,而Hyperkit又基于Xhyve

If you read the Readme of xhyve如果你阅读了xhyve的自述文件

Notably absent are sound, USB, HID and any kind of graphics support.值得注意的是没有声音、USB、HID 和任何类型的图形支持。 With a focus on server virtualization this is not strictly a requirement.由于专注于服务器虚拟化,这并不是严格的要求。 bhyve may gain desktop virtualization capabilities in the future but this doesn't seem to be a priority. bhyve 将来可能会获得桌面虚拟化功能,但这似乎不是优先事项。

So your docker container which is running inside the Hyperkit VM will never be able to access the device.因此,在 Hyperkit VM 内运行的 docker 容器将永远无法访问该设备。

Your --device=/dev/video0:/dev/video0 is just mapping a device from inside the container and it may not be there in the VM at all.您的--device=/dev/video0:/dev/video0只是从容器内部映射设备,它可能根本不在 VM 中。

So what are you alternatives?那么你有什么替代品? Instead of using Docker for Mac, use VirtualBox or VMWare fusion.不要使用 Docker for Mac,而是使用 VirtualBox 或 VMWare fusion。 Create a Ubuntu 16.04 or any other supported OS VM inside it.在其中创建 Ubuntu 16.04 或任何其他受支持的 OS VM。 Shared the webcam device with the VM using settings for that VM.使用该 VM 的设置与该 VM 共享网络摄像头设备。 Now your VM OS will have the device.现在您的 VM OS 将拥有该设备。

Inside your VM install docker and access the device.在您的 VM 中安装 docker 并访问设备。

I noticed the problem that Docker running on macOS can't use the camera.我注意到在 macOS 上运行的 Docker 无法使用相机的问题。 I do not feel like installing VM on my mac, but I want to use the convenience of Docker, so I designed the technical plan, which and read images from mac itself, and use docker container as a server.我不想在我的mac上安装VM,但我想使用Docker的方便,所以我设计了技术方案,从mac本身读取图像,并使用docker容器作为服务器。

The general:一般:

  • Use docker as a service, listen to a port, receive and process images使用 docker as a service,监听端口,接收和处理镜像
  • Use macOS to read images, and give image analysis tasks to Docker container使用 macOS 读取图像,并将图像分析任务交给 Docker 容器

The specific:具体:

  • Read images from macOS, which's not difficult从 macOS 读取图像,并不难
  • Send images to Docker container via inter-process communication(IPC) like socket (I don't quite understand this so much now, I will research a little about it)通过像socket这样的进程间通信(IPC)将图像发送到Docker容器(我现在不太了解这个,我会研究一下)
  • Process images use some specific algorithms处理图像使用一些特定的算法
  • Return result to macOS host via IPC通过 IPC 将结果返回给 macOS 主机
  • Other easy jobs on mac mac 上的其他简单工作

The method could be concluded as this image该方法可以总结为这张图片我们用来连接mac和Docker的方法

I haven't implemented it, but I searched there are some materials that may help, I list below:我还没有实现它,但我搜索了一些可能有帮助的材料,我在下面列出:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM