Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install openCV for python3.5 on docker (debian:jessie)

I've been struggling for a few days with this, couldn't find the proper way to install OpenCV + python3.5 on debian:jessie docker (I can't use any over base image beside debian:jessie).

I currently trying the tangled monstrosity as you can find deep bellow.

I've based it mostly on many error handling on the way and: https://www.pyimagesearch.com/2017/09/25/configuring-ubuntu-for-deep-learning-with-python/ https://github.com/janza/docker-python3-opencv/blob/master/Dockerfile

I think my issue is the installation of python3.5 and pointing the pip3 for it to be the default, then installing the numpy through it and then the rest. But at this point I'm not sure at all, nor am I sure on how to solve it.

Could really use any help and pointers solving this mess. Thanks.

FROM debian:jessie
RUN mkdir -p /home/deployer
RUN apt-get update && apt-get install -yq libgconf-2-4 && apt-get install vim -y
RUN apt-get update && apt-get install -y wget --no-install-recommends \
    && apt-get install -y curl \
    && apt-get install -y libssl-dev openssl \
    && apt-get install -y software-properties-common \
    && apt-get install -y python-dev build-essential \
    && apt-get install -y python-setuptools \
    && easy_install pip \
    && apt-get install -y python3-pip \
    && apt-get install -y python \
    && wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz \
    && tar xzvf Python-3.5.0.tgz \
    && cd Python-3.5.0 \
    && ./configure \
    && make \
    && make install \
    && cd /home/deployer/ \
    && apt-get update \
    && apt-get upgrade -y \
    && apt-get install -y p7zip \
    && apt-get install -y p7zip-full \
    && apt-get install -y unace \
    && apt-get install -y zip \
    && apt-get install -y unzip \
    && apt-get install -y xz-utils \
    && apt-get install -y sharutils \
    && apt-get install -y build-essential cmake pkg-config \
    && apt-get install -y libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev \
    && apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
    && apt-get install -y libxvidcore-dev libx264-dev \
    && apt-get install -y libgtk2.0-dev libgtk-3-dev \
    && apt-get install -y libatlas-base-dev gfortran \
    && apt-get install -y python2.7-dev python3-dev \
    && wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip \
    && wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip \
    && wget https://bootstrap.pypa.io/get-pip.py \
    && python get-pip.py \
    && python3 get-pip.py \
    && pip install numpy \
    && pip3 install numpy \
RUN cd /home/deployer/ \
    && unzip opencv.zip \
    && unzip opencv_contrib.zip \
    && cd /home/deployer/opencv-3.3.0/ \
    && mkdir 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=/home/deployer/opencv_contrib-3.3.0/modules \
     -D BUILD_EXAMPLES=ON \
     -D WITH_TBB=ON \
     -D WITH_V4L=ON \
     -D INSTALL_C_EXAMPLES=ON \
     -D PYTHON_EXECUTABLE=/usr/local/bin/python3.5 \
     -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \
     -D PYTHON_INCLUDE_DIR=/usr/include/python3.5m/ \
     -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m/ \
     -D PYTHON_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ \
     -D BUILD_opencv_java=OFF .. \
    && make -j4 \
    && make install \
    && ldconfig
RUN pip3 install Pillow
like image 568
Captain_Meow_Meow Avatar asked Sep 23 '18 06:09

Captain_Meow_Meow


People also ask

Can I use OpenCV with python 3?

The nice thing with OpenCV is that it comes with a complete Python 3 library. The latest GeeXlab 0.29. 17.0 for Windows 64-bit comes with Python 3.8. 2 and OpenCV 4.2.

Can you pip install OpenCV?

OpenCV can be installed using pip.

What version of python works with cv2?

Currently, only the Python 2 version of the cv2 module is built and included in the latest Windows release. Support for Python 3 (as well as adding other non-standard features/modules), requires compiling from source - see the official OpenCV documentation for details.


1 Answers

Given below the Dockerfile to install python3.5 as default with pip and install openCV 3.4.2 in debian:jessie base image. This will resolve your issue.

FROM debian:jessie
ENV PATH /usr/local/bin:$PATH
ENV LANG C.UTF-8
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        netbase \
    && rm -rf /var/lib/apt/lists/*

ENV GPG_KEY 97FC712E4C024BBEA48A61ED3A5CA953F73C700D
ENV PYTHON_VERSION 3.5.6
RUN set -ex \
    \
    && savedAptMark="$(apt-mark showmanual)" \
    && apt-get update && apt-get install -y --no-install-recommends \
        dpkg-dev \
        gcc \
        libbz2-dev \
        libc6-dev \
        libexpat1-dev \
        libffi-dev \
        libgdbm-dev \
        liblzma-dev \
        libncursesw5-dev \
        libreadline-dev \
        libsqlite3-dev \
        libssl-dev \
        make \
        tk-dev \
        wget \
        xz-utils \
        zlib1g-dev \
        $(command -v gpg > /dev/null || echo 'gnupg dirmngr') \
    \
    && wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
    && wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
    && export GNUPGHOME="$(mktemp -d)" \
    && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$GPG_KEY" \
    && gpg --batch --verify python.tar.xz.asc python.tar.xz \
    && { command -v gpgconf > /dev/null && gpgconf --kill all || :; } \
    && rm -rf "$GNUPGHOME" python.tar.xz.asc \
    && mkdir -p /usr/src/python \
    && tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
    && rm python.tar.xz \
    \
    && cd /usr/src/python \
    && gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
    && ./configure \
        --build="$gnuArch" \
        --enable-loadable-sqlite-extensions \
        --enable-shared \
        --with-system-expat \
        --with-system-ffi \
        --without-ensurepip \
    && make -j "$(nproc)" \
    && make install \
    && ldconfig \
    \
    && apt-mark auto '.*' > /dev/null \
    && apt-mark manual $savedAptMark \
    && find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec ldd '{}' ';' \
        | awk '/=>/ { print $(NF-1) }' \
        | sort -u \
        | xargs -r dpkg-query --search \
        | cut -d: -f1 \
        | sort -u \
        | xargs -r apt-mark manual \
    && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
    && rm -rf /var/lib/apt/lists/* \
    \
    && find /usr/local -depth \
        \( \
            \( -type d -a \( -name test -o -name tests \) \) \
            -o \
            \( -type f -a \( -name '*.pyc' -o -name '*.pyo' \) \) \
        \) -exec rm -rf '{}' + \
    && rm -rf /usr/src/python \
    \
    && python3 --version
RUN cd /usr/local/bin \
    && ln -s idle3 idle \
    && ln -s pydoc3 pydoc \
    && ln -s python3 python \
    && ln -s python3-config python-config
ENV PYTHON_PIP_VERSION 18.0
RUN set -ex; \
    \
    savedAptMark="$(apt-mark showmanual)"; \
    apt-get update; \
    apt-get install -y --no-install-recommends wget; \
    \
    wget -O get-pip.py 'https://bootstrap.pypa.io/get-pip.py'; \
    \
    apt-mark auto '.*' > /dev/null; \
    [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; \
    apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
    rm -rf /var/lib/apt/lists/*; \
    \
    python get-pip.py \
        --disable-pip-version-check \
        --no-cache-dir \
        "pip==$PYTHON_PIP_VERSION" \
    ; \
    pip --version; \
    \
    find /usr/local -depth \
        \( \
            \( -type d -a \( -name test -o -name tests \) \) \
            -o \
            \( -type f -a \( -name '*.pyc' -o -name '*.pyo' \) \) \
        \) -exec rm -rf '{}' +; \
    rm -f get-pip.py    
RUN apt-get update && \
        apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        git \
        wget \
        unzip \
        yasm \
        pkg-config \
        libswscale-dev \
        libtbb2 \
        libtbb-dev \
        libjpeg-dev \
        libpng-dev \
        libtiff-dev \
        libavformat-dev \
        libpq-dev \
    && rm -rf /var/lib/apt/lists/*
RUN pip install numpy
WORKDIR /
ENV OPENCV_VERSION="3.4.2"
RUN wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip \
&& unzip ${OPENCV_VERSION}.zip \
&& mkdir /opencv-${OPENCV_VERSION}/cmake_binary \
&& cd /opencv-${OPENCV_VERSION}/cmake_binary \
&& cmake -DBUILD_TIFF=ON \
  -DBUILD_opencv_java=OFF \
  -DWITH_CUDA=OFF \
  -DWITH_OPENGL=ON \
  -DWITH_OPENCL=ON \
  -DWITH_IPP=ON \
  -DWITH_TBB=ON \
  -DWITH_EIGEN=ON \
  -DWITH_V4L=ON \
  -DBUILD_TESTS=OFF \
  -DBUILD_PERF_TESTS=OFF \
  -DCMAKE_BUILD_TYPE=RELEASE \
  -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") \
  -DPYTHON_EXECUTABLE=$(which python) \
  -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
  -DPYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
  .. \
&& make install \
&& rm /${OPENCV_VERSION}.zip \
&& rm -r /opencv-${OPENCV_VERSION}  
CMD ["python"]

python version :

 root@68e4f88ab646:/# python -V
 Python 3.5.6

pip version :

 root@68e4f88ab646:/# pip -V
 pip 18.0 from /usr/local/lib/python3.5/site-packages/pip (python 3.5)

opencv version :

 root@68e4f88ab646:/# pkg-config --modversion opencv
 3.4.2

Tested and Works !!!

like image 103
Prem Avatar answered Sep 30 '22 12:09

Prem