简体   繁体   English

在 Docker 中使用 OpenCV (Python) 访问网络摄像头?

[英]Access webcam using OpenCV (Python) in Docker?

I'm trying to use Docker for one of our projects which uses OpenCV to process webcam feed (Python).我正在尝试将 Docker 用于我们的一个项目,该项目使用 OpenCV 来处理网络摄像头提要(Python)。 But I can't seem to get access to the webcam within docker, here's the code which I use to test webcam access:但我似乎无法访问 docker 内的网络摄像头,这是我用来测试网络摄像头访问的代码:

python -c "import cv2;print(cv2.VideoCapture(0).isOpened())"

And here's what I tried so far,到目前为止,这是我尝试过的,

 docker run --device=/dev/video0 -it rec bash

 docker run --privileged --device=/dev/video0 -it rec bash

 sudo docker run --privileged --device=/dev/video0:/dev/video0 -it rec bash

All of these return False , what am I doing wrong?所有这些都返回False ,我做错了什么?

The Dockerfile in the link you provided doesn't specify how opencv was installed, can you provide the Dockerfile you used?您提供的链接中的 Dockerfile 没有指定 opencv 是如何安装的,您能提供您使用的 Dockerfile 吗? Or how you installed opencv?或者你是怎么安装opencv的?

VideoCapture(0) won't work if you install opencv via pip.如果您通过 pip 安装 opencv,则 VideoCapture(0) 将无法工作。

You're using --device=/dev/video0:/dev/video0 correctly.您正在正确使用--device=/dev/video0:/dev/video0

Try to use this:尝试使用这个:

-v /dev/video0:/dev/video0

in place of代替

--device=/dev/video0 

and execute:并执行:

$ xhost + 

before docker rundocker 运行之前

The easisest way I found to have a dockerized OpenCV able to connect to your webcam is by using the following Dockerfile :我发现让 dockerized OpenCV 能够连接到您的网络摄像头的最简单方法是使用以下 Dockerfile :

FROM ubuntu:20.04

ENV TERM=xterm
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
    libopencv-dev \
    python3-opencv \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /app
ADD . /app

ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app

and then build it and run in this way :然后构建它并以这种方式运行:

docker build -t opencv-webcam .
docker run -it -v $PWD:/app/ --device=/dev/video0:/dev/video0 -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY opencv-webcam bash

and run the following script inside the container with python3 script.py :并使用python3 script.py在容器内运行以下脚本:

import cv2


WINDOW_NAME = "Opencv Webcam"

def run():

    cv2.namedWindow(WINDOW_NAME)

    video_capture = cv2.VideoCapture(0)

    while True:
        ret, frame = video_capture.read()
        print(ret, frame.shape)

        cv2.imshow(WINDOW_NAME, frame)
        cv2.waitKey(1)

if __name__ == "__main__":
    run()

I ran into issues when using a non-privileged Dockerfile (ie with a USER line):我在使用非特权 Dockerfile(即使用 USER 行)时遇到问题:

The container must be started with --user {UID} ;容器必须以--user {UID}开头; NOT --user {UID}:{GID} (same for user: line in docker-compose.yml, If the gid is set. weirdly this will not inherit the video group permission needed for access to the webcam files (/dev/video0 etc.), Also; the internal Dockerfile user must be added to the video group: add this after the USER line (and after installing sudo and adding the user to sudoers): NOT --user {UID}:{GID} (与user: docker-compose.yml 中的行,如果设置了 gid。奇怪的是,这不会继承访问网络摄像头文件(/dev/video0 等)所需的视频组权限.), 另外; 必须将内部 Dockerfile 用户添加到视频组中:在 USER 行之后添加它(并且在安装 sudo 并将用户添加到 sudoers 之后):

RUN sudo usermod -a -G video <InternalUsername>

Before you try theses steps: run everything in privileged mode to test if your problem lies really here.在你尝试这些步骤之前:在特权模式下运行所有的东西来测试你的问题是否真的出在这里。

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

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