OpenCV imshow 在 Docker 容器中不起作用
我试图从 docker 容器运行 cv2.imshow(...)
以显示主机上的窗口,但不断收到以下错误:
[ERROR:[email protected]] global /opt/opencv-4.5.5/modules/videoio/src/cap.cpp (597) open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.5.5) /opt/opencv-4.5.5/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): /src/test/output/test_april_output.avi in function 'icvExtractPattern'
The program 'python' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAccess (attempt to access private resource denied)'.
(Details: serial 201 error_code 10 request_code 130 minor_code 1)
(Note to programmers: normally, X errors are reported asynchronously;
that is, you will receive the error a while after causing it.
To debug your program, run it with the --sync command line
option to change this behavior. You can then get a meaningful
backtrace from your debugger if you break on the gdk_x_error() function.)
ERROR: 1
non-network local connections being removed from access control list
我通过 run 运行容器。 sh
,当您使用 OpenCV python pip 包时,此设置有效。我将 Dockerfile 切换为从源代码库进行编译,现在出现 GDK 错误。
run.sh
#!/bin/bash
export XSOCK=/tmp/.X11-unix
export XAUTH=/tmp/.docker.xauth
xhost +local:docker
touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
docker-compose build aerial_viewer
docker-compose run aerial_viewer
xhost -local:docker
Dockerfile
FROM python:3.8.8-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
wget \
g++ \
bzip2 \
build-essential \
cmake \
git \
graphviz \
unzip \
pkg-config \
protobuf-compiler \
python-dev \
python-opencv \
libopencv-dev \
libjpeg-dev \
libpng-dev \
libhdf5-dev \
libtiff-dev \
libgl1-mesa-glx \
libgtk2.0-dev \
python-pycurl \
libatlas-base-dev \
gfortran \
webp \
libcanberra-gtk-module \
libcanberra-gtk3-module \
openmpi-bin \
qt5-default \
libvtk6-dev \
zlib1g-dev \
libprotobuf-dev && \
rm -rf /var/lib/apt/lists/*
RUN python3 -m pip install --upgrade pip setuptools wheel
RUN python3 -m pip install numpy==1.20.1
WORKDIR $HOME/src/
ENV LD_LIBRARY_PATH=/usr/local/lib
ENV OPENCV_VERSION="4.5.5"
RUN wget -q https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip -O opencv.zip \
&& wget -q --no-check-certificate https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip -O opencv_contrib.zip \
&& unzip -qq opencv.zip -d /opt && rm -rf opencv.zip \
&& unzip -qq opencv_contrib.zip -d /opt && rm -rf opencv_contrib.zip \
&& cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib-${OPENCV_VERSION}/modules \
-D WITH_OPENMP=ON \
-D WITH_FFMPEG=ON \
-D WITH_OPENCL=OFF \
-D BUILD_ZLIB=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_GTK=ON \
-D WITH_QT=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.8/site-packages/ \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF \
/opt/opencv-${OPENCV_VERSION} \
&& make -j$(nproc) \
&& make install \
&& rm -rf /opt/build/* \
&& rm -rf /opt/opencv-${OPENCV_VERSION}
RUN git clone https://github.com/AprilRobotics/apriltag.git --branch v3.1.7 && \
cd apriltag && cmake . && make install && cd ../ && rm -rf apriltag
RUN (apt-get autoremove -y; \
apt-get autoclean -y)
COPY requirements.txt $HOME/src/
RUN python3 -m pip install -r requirements.txt
COPY . $HOME/src/
ENTRYPOINT [ "python", "aerial_viewer.py"]
docker-compose.yml
aerial_viewer:
image: star_aerial_viewer:latest
build:
context: aerial_viewer
dockerfile: Dockerfile
environment:
- DISPLAY=$DISPLAY
- XAUTHORITY=$XAUTH
- XSOCK=$XSOCK
restart: unless-stopped
volumes:
- $XSOCK:$XSOCK
- $XAUTH:$XAUTH
I am trying to run cv2.imshow(...)
from a docker container to show the window on the host machine, and keep getting the following error:
[ERROR:[email protected]] global /opt/opencv-4.5.5/modules/videoio/src/cap.cpp (597) open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.5.5) /opt/opencv-4.5.5/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): /src/test/output/test_april_output.avi in function 'icvExtractPattern'
The program 'python' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAccess (attempt to access private resource denied)'.
(Details: serial 201 error_code 10 request_code 130 minor_code 1)
(Note to programmers: normally, X errors are reported asynchronously;
that is, you will receive the error a while after causing it.
To debug your program, run it with the --sync command line
option to change this behavior. You can then get a meaningful
backtrace from your debugger if you break on the gdk_x_error() function.)
ERROR: 1
non-network local connections being removed from access control list
I run the container through run.sh
, and this setup works when you use OpenCV python pip package. I switched the Dockerfile to compiling from the source repo, and now I am getting GDK errors.
run.sh
#!/bin/bash
export XSOCK=/tmp/.X11-unix
export XAUTH=/tmp/.docker.xauth
xhost +local:docker
touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
docker-compose build aerial_viewer
docker-compose run aerial_viewer
xhost -local:docker
Dockerfile
FROM python:3.8.8-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
wget \
g++ \
bzip2 \
build-essential \
cmake \
git \
graphviz \
unzip \
pkg-config \
protobuf-compiler \
python-dev \
python-opencv \
libopencv-dev \
libjpeg-dev \
libpng-dev \
libhdf5-dev \
libtiff-dev \
libgl1-mesa-glx \
libgtk2.0-dev \
python-pycurl \
libatlas-base-dev \
gfortran \
webp \
libcanberra-gtk-module \
libcanberra-gtk3-module \
openmpi-bin \
qt5-default \
libvtk6-dev \
zlib1g-dev \
libprotobuf-dev && \
rm -rf /var/lib/apt/lists/*
RUN python3 -m pip install --upgrade pip setuptools wheel
RUN python3 -m pip install numpy==1.20.1
WORKDIR $HOME/src/
ENV LD_LIBRARY_PATH=/usr/local/lib
ENV OPENCV_VERSION="4.5.5"
RUN wget -q https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip -O opencv.zip \
&& wget -q --no-check-certificate https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip -O opencv_contrib.zip \
&& unzip -qq opencv.zip -d /opt && rm -rf opencv.zip \
&& unzip -qq opencv_contrib.zip -d /opt && rm -rf opencv_contrib.zip \
&& cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib-${OPENCV_VERSION}/modules \
-D WITH_OPENMP=ON \
-D WITH_FFMPEG=ON \
-D WITH_OPENCL=OFF \
-D BUILD_ZLIB=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_GTK=ON \
-D WITH_QT=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.8/site-packages/ \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF \
/opt/opencv-${OPENCV_VERSION} \
&& make -j$(nproc) \
&& make install \
&& rm -rf /opt/build/* \
&& rm -rf /opt/opencv-${OPENCV_VERSION}
RUN git clone https://github.com/AprilRobotics/apriltag.git --branch v3.1.7 && \
cd apriltag && cmake . && make install && cd ../ && rm -rf apriltag
RUN (apt-get autoremove -y; \
apt-get autoclean -y)
COPY requirements.txt $HOME/src/
RUN python3 -m pip install -r requirements.txt
COPY . $HOME/src/
ENTRYPOINT [ "python", "aerial_viewer.py"]
docker-compose.yml
aerial_viewer:
image: star_aerial_viewer:latest
build:
context: aerial_viewer
dockerfile: Dockerfile
environment:
- DISPLAY=$DISPLAY
- XAUTHORITY=$XAUTH
- XSOCK=$XSOCK
restart: unless-stopped
volumes:
- $XSOCK:$XSOCK
- $XAUTH:$XAUTH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论