Python 在 Docker 容器中安装 OpenCV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36862589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Install OpenCV in a Docker container
提问by Ewan Valentine
I'm attempting to Dockerise a Python application, which depends on OpenCV. I've tried several different ways, but I keep getting... ImportError: No module named cv2
when I attempt to run the application.
我正在尝试 Dockerise 一个 Python 应用程序,它依赖于 OpenCV。我尝试了几种不同的方法,但是ImportError: No module named cv2
当我尝试运行该应用程序时,我不断遇到...。
Here's my current Dockerfile.
这是我当前的 Dockerfile。
FROM python:2.7
MAINTAINER Ewan Valentine <[email protected]>
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
#?Various Python and C/build deps
RUN apt-get update && apt-get install -y \
wget \
build-essential \
cmake \
git \
pkg-config \
python-dev \
python-opencv \
libopencv-dev \
libav-tools \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libjasper-dev \
libgtk2.0-dev \
python-numpy \
python-pycurl \
libatlas-base-dev \
gfortran \
webp \
python-opencv
#?Install Open CV - Warning, this takes absolutely forever
RUN cd ~ && git clone https://github.com/Itseez/opencv.git && \
cd opencv && \
git checkout 3.0.0 && \
cd ~ && git clone https://github.com/Itseez/opencv_contrib.git && \
cd opencv_contrib && \
git checkout 3.0.0 && \
cd ~/opencv && mkdir -p build && cd build && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=OFF .. && \
make -j4 && \
make install && \
ldconfig
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /usr/src/app
And my requirements.txt file
还有我的requirements.txt文件
Flask==0.8
gunicorn==0.14.2
requests==0.11.1
bs4==0.0.1
nltk==3.2.1
pymysql==0.7.2
xlsxwriter==0.8.5
numpy==1.11
Pillow==3.2.0
cv2==1.0
pytesseract==0.1
回答by Ewan Valentine
Fixed with a slightly different set-up
修复了稍微不同的设置
FROM python:2.7
MAINTAINER Ewan Valentine <[email protected]>
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
#?Various Python and C/build deps
RUN apt-get update && apt-get install -y \
wget \
build-essential \
cmake \
git \
unzip \
pkg-config \
python-dev \
python-opencv \
libopencv-dev \
libav-tools \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libjasper-dev \
libgtk2.0-dev \
python-numpy \
python-pycurl \
libatlas-base-dev \
gfortran \
webp \
python-opencv \
qt5-default \
libvtk6-dev \
zlib1g-dev
#?Install Open CV - Warning, this takes absolutely forever
RUN mkdir -p ~/opencv cd ~/opencv && \
wget https://github.com/opencv/opencv/archive/3.0.0.zip && \
unzip 3.0.0.zip && \
rm 3.0.0.zip && \
mv opencv-3.0.0 OpenCV && \
cd OpenCV && \
mkdir build && \
cd build && \
cmake \
-DWITH_QT=ON \
-DWITH_OPENGL=ON \
-DFORCE_VTK=ON \
-DWITH_TBB=ON \
-DWITH_GDAL=ON \
-DWITH_XINE=ON \
-DBUILD_EXAMPLES=ON .. && \
make -j4 && \
make install && \
ldconfig
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /usr/src/app
回答by bradden_gross
Thanks for posting this. I ran into the same issue and tried your solution and although it seemed to install OpenCV it left me with an issue of conflicting versions of the Python six library so I took a different route. I think a simpler way to do this is to install Anaconda in your container and then add OpenCV. I'm using Python 2 so my entire Dockerfile to get OpenCvv installed is just:
感谢您发布此信息。我遇到了同样的问题并尝试了你的解决方案,虽然它似乎安装了 OpenCV,但它给我留下了 Python 6 库版本冲突的问题,所以我采取了不同的路线。我认为一个更简单的方法是在你的容器中安装 Anaconda,然后添加 OpenCV。我正在使用 Python 2,所以我安装 OpenCvv 的整个 Dockerfile 只是:
FROM continuumio/anaconda
EXPOSE 5000
ADD . /code-directory
WORKDIR code-directory
RUN conda install opencv
CMD ["python", "run-code.py"]
This will install Anaconda from the continuumio/anaconda Dockerfile and then it will use Anaconda to install opencv. There is a seperate continuumio Dockerfile for Python 3 if you need that as well.
这将从 continuumio/anaconda Dockerfile 安装 Anaconda,然后它将使用 Anaconda 安装 opencv。如果您需要,还有一个单独的用于 Python 3 的 continuumio Dockerfile。
回答by blueskin
Here's an imagethat is built on Ubuntu 16.04 with Python2 + Python3 + OpenCV. You can pull it using
docker pull chennavarri/ubuntu_opencv_python
这是一个使用 Python2 + Python3 + OpenCV 在 Ubuntu 16.04 上构建的图像。你可以拉它使用
docker pull chennavarri/ubuntu_opencv_python
Here's the Dockerfile (provided in the same dockerhub repo mentioned above) that will install opencv for both python2 and python3 on Ubuntu 16.04 and also sets the appropriate raw1394 link. Copied from https://github.com/chennavarri/docker-ubuntu-python-opencv
这是 Dockerfile(在上面提到的同一个 dockerhub 存储库中提供),它将在 Ubuntu 16.04 上为 python2 和 python3 安装 opencv,并设置适当的 raw1394 链接。复制自https://github.com/chennavarri/docker-ubuntu-python-opencv
FROM ubuntu:16.04
MAINTAINER Chenna Varri
RUN apt-get update
RUN apt-get install -y build-essential apt-utils
RUN apt-get install -y cmake git libgtk2.0-dev pkg-config libavcodec-dev \
libavformat-dev libswscale-dev
RUN apt-get update && apt-get install -y python-dev python-numpy \
python3 python3-pip python3-dev libtbb2 libtbb-dev \
libjpeg-dev libjasper-dev libdc1394-22-dev \
python-opencv libopencv-dev libav-tools python-pycurl \
libatlas-base-dev gfortran webp qt5-default libvtk6-dev zlib1g-dev
RUN pip3 install numpy
RUN apt-get install -y python-pip
RUN pip install --upgrade pip
RUN cd ~/ &&\
git clone https://github.com/Itseez/opencv.git &&\
git clone https://github.com/Itseez/opencv_contrib.git &&\
cd opencv && mkdir build && cd build && cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON .. && \
make -j4 && make install && ldconfig
# Set the appropriate link
RUN ln /dev/null /dev/raw1394
RUN cd ~/opencv
Some additional instructions for people newly starting with Docker:
给刚开始使用 Docker 的人的一些额外说明:
In the directory where you put this Dockerfile, build the docker image as
docker build -t ubuntu_cv .
Once the image is built, you can check by doing
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu_cv latest 6210ddd6346b 24 minutes ago 2.192 GB
You can start a docker container as
docker run -t -i ubuntu_cv:latest
在您放置此 Dockerfile 的目录中,将 docker 映像构建为
docker build -t ubuntu_cv .
构建图像后,您可以通过执行检查
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu_cv latest 6210ddd6346b 24 minutes ago 2.192 GB
您可以启动 docker 容器作为
docker run -t -i ubuntu_cv:latest
回答by MingCHEN
from ubuntu:12.10
# Ubuntu sides with libav, I side with ffmpeg.
run echo "deb http://ppa.launchpad.net/jon-severinsson/ffmpeg/ubuntu quantal main" >> /etc/apt/sources.list
run apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1DB8ADC1CFCA9579
run apt-get update
run apt-get install -y -q wget curl
run apt-get install -y -q build-essential
run apt-get install -y -q cmake
run apt-get install -y -q python2.7 python2.7-dev
run wget 'https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg' && /bin/sh setuptools-0.6c11-py2.7.egg && rm -f setuptools-0.6c11-py2.7.egg
run curl 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py' | python2.7
run pip install numpy
run apt-get install -y -q libavformat-dev libavcodec-dev libavfilter-dev libswscale-dev
run apt-get install -y -q libjpeg-dev libpng-dev libtiff-dev libjasper-dev zlib1g-dev libopenexr-dev libxine-dev libeigen3-dev libtbb-dev
add build_opencv.sh /build_opencv.sh
run /bin/sh /build_opencv.sh
run rm -rf /build_opencv.sh
回答by Eyshika
To install Opencv (latest) in docker ... the steps are similar to Linux version just the symlink path is different:
在 docker 中安装 Opencv(最新)......步骤与 Linux 版本类似,只是符号链接路径不同:
apt install -y libtiff5-dev libjpeg8-dev libpng-dev
apt install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
apt install -y libxine2-dev libv4l-dev
apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
apt install -y qt5-default
apt install -y libatlas-base-dev
apt install -y libfaac-dev libmp3lame-dev libtheora-dev
apt install -y libvorbis-dev libxvidcore-dev
apt install -y libopencore-amrnb-dev libopencore-amrwb-dev
apt install -y x264 x265 v4l-utils
apt install -y libprotobuf-dev protobuf-compiler
apt install -y libeigen3-dev
wget --output-document cv.zip https://github.com/opencv/opencv/archive/4.0.1.zip
wget --output-document contrib.zip
https://github.com/opencv/opencv_contrib/archive/4.0.1.zip
unzip cv.zip
unzip contrib.zip
cd opencv-4.0.1
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D WITH_CUDA=ON \
-D WITH_NVCUVID=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.1/modules \
-D OPENCV_ENABLE_NONFREE=ON \
..
make -j 'number of gpu'
make install
ldconfig
ln -s /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.so
This works for me!!
这对我有用!!