Skip to content

Instantly share code, notes, and snippets.

@yuchen-xue
Created February 1, 2024 12:52
Show Gist options
  • Save yuchen-xue/8b97e648535072ffb7482e716ed385bf to your computer and use it in GitHub Desktop.
Save yuchen-xue/8b97e648535072ffb7482e716ed385bf to your computer and use it in GitHub Desktop.
Build opencv inside a docker image
# ref:
# https://github.com/Valian/docker-python-opencv-ffmpeg/blob/master/Dockerfile-py3-cuda
ARG CUDA_VERSION="10.0"
ARG FLAVOR="runtime"
FROM nvidia/cuda:${CUDA_VERSION}-cudnn7-${FLAVOR}-ubuntu16.04
ARG OPENCV_VERSION="4.1.0"
# Install all dependencies for OpenCV
RUN apt-get -y update && \
apt-get -y install \
python3 \
python3-dev \
git \
wget \
unzip \
cmake \
build-essential \
pkg-config \
libatlas-base-dev \
gfortran \
libjasper-dev \
libgtk2.0-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libv4l-dev \
&& \
# install python dependencies
wget https://bootstrap.pypa.io/get-pip.py && \
python3 get-pip.py && \
rm get-pip.py && \
pip3 install numpy
# Download OpenCV
RUN wget https://github.com/opencv/opencv/archive/$OPENCV_VERSION.zip -O opencv3.zip && \
unzip -q opencv3.zip && \
mv /opencv-$OPENCV_VERSION /opencv && \
rm opencv3.zip && \
wget https://github.com/opencv/opencv_contrib/archive/$OPENCV_VERSION.zip -O opencv_contrib3.zip && \
unzip -q opencv_contrib3.zip && \
mv /opencv_contrib-$OPENCV_VERSION /opencv_contrib && \
rm opencv_contrib3.zip
# Prepare build
RUN 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_CUDA=ON \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D WITH_CUBLAS=ON \
-D WITH_V4L=ON .. \
&& \
# Install
cd /opencv/build && \
make -j$(nproc) && \
make install && \
ldconfig \
&& \
# Clean
apt-get -y remove \
python3-dev \
libatlas-base-dev \
gfortran \
libjasper-dev \
libgtk2.0-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libv4l-dev \
&& \
apt-get clean && \
rm -rf /opencv /opencv_contrib /var/lib/apt/lists/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment