opencv

查看opencv配置信息

1
python -c "import cv2; print(cv2.getBuildInformation())"

安装opencv

opencv安装过程需要选择支持ffmpeg,有显卡还需要支持cuda。一般是采用编译安装,安装好后不需要利用pip install方式安装opencv-python,这点非常重要!不然会覆盖先前的opencv安装。

下面提供一个具有python3.5+cuda+ffmpeg+opencv环境的dockerfile前半部分。该dockerfile学自valian/docker-python-opencv-ffmpeg,但做了一些修改,已确保能够跑通。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
FROM tensorflow/tensorflow:1.2.1-devel-gpu-py3

ENV PYTHON_VERSION 3.5

# Install all dependencies for OpenCV 3.2
RUN apt-get -y update && apt-get -y install python$PYTHON_VERSION-dev wget unzip \
build-essential cmake git pkg-config libatlas-base-dev gfortran \
libjasper-dev libgtk2.0-dev libavcodec-dev libavformat-dev \
libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libv4l-dev \
&& wget https://bootstrap.pypa.io/get-pip.py && python3 get-pip.py && pip3 install numpy && rm get-pip.py \
&& wget https://github.com/Itseez/opencv/archive/3.2.0.zip -O /opencv3.zip \
&& unzip -q /opencv3.zip -d / && mv /opencv-3.2.0 /opencv && rm /opencv3.zip \
&& wget https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip -O opencv_contrib3.zip \
&& unzip -q opencv_contrib3.zip -d / && mv /opencv_contrib-3.2.0 /opencv_contrib && rm opencv_contrib3.zip \

# prepare build
&& mkdir /opencv/build && cd /opencv/build \
&& cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_PYTHON_SUPPORT=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/opencv_contrib/modules \
-D BUILD_EXAMPLES=OFF \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D WITH_IPP=OFF \
-D WITH_FFMPEG=ON \
-D WITH_V4L=ON \
-D WITH_CUDA=ON .. \

# install
&& cd /opencv/build && make -j$(nproc) && make install && ldconfig \

#clean
&& apt-get -y remove build-essential cmake git pkg-config libatlas-base-dev gfortran \
libjasper-dev libgtk2.0-dev libavcodec-dev libavformat-dev \
libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libv4l-dev \
&& apt-get clean \
&& rm -rf /opencv /opencv_contrib /var/lib/apt/lists/*

# Define other commands.
...

references

valian/docker-python-opencv-ffmpeg