Installing opencv in docker container

asked 2017-07-03 14:28:06 -0600

beta gravatar image

Hi,

I'm trying to install opencv for this project. In the original dockerfile, they didn't give instructions for installing opencv. The dockerfile portion is follows:

FROM python:3.4-slim

#RUN apt-get -y update
RUN apt-get update && apt-get install -y \ 
    build-essential \
    cmake \
    gfortran \
    git \
    libatlas-base-dev \
    libav-tools  \
    libgtk2.0-dev \
    libjasper-dev \
    libjpeg-dev \
    libopencv-dev \
    libpng-dev \
    libtiff-dev \
    libvtk6-dev \
    pkg-config \
    python-dev \
    python-numpy \
    python-opencv \
    python-pycurl \
    qt5-default \
    unzip \
    webp \
    wget \
    zlib1g-dev 
    #&& apt-get clean && rm -rf /tmp/* /var/tmp/*

RUN mkdir -p ~/opencv cd ~/opencv && \
    wget https://github.com/Itseez/opencv/archive/3.2.0.zip && \
    unzip 3.2.0.zip && \
    rm 3.2.0.zip && \
    cd opencv-3.2.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 BUILD_EXAMPLES=ON .. && \
    make -j4 && \
    make install && \ 
    ldconfig

RUN ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so /usr/local/lib/python3.4/site-packages/cv2.so

RUN apt-get -y update
RUN apt-get install -y --fix-missing \
    build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-dev \
    libavcodec-dev \
    libavformat-dev \
    libboost-all-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    liblapack-dev \
    libswscale-dev \
    pkg-config \
    python3-dev \
    python3-numpy \
    software-properties-common \
    zip \
    && apt-get clean && rm -rf /tmp/* /var/tmp/*

RUN cd ~ && \
    mkdir -p dlib && \
    git clone -b 'v19.4' --single-branch https://github.com/davisking/dlib.git dlib/ && \
    cd  dlib/ && \
    python3 setup.py install --yes USE_AVX_INSTRUCTIONS

COPY . /root/face_recognition
RUN cd /root/face_recognition && \
pip install -r requirements.txt && \
python setup.py install

CMD cd /root/face_recognition/examples && \
python recognize_faces_in_pictures.py

But when I'm doing import cv2, it's giving me the error:

Traceback (most recent call last): File "", line 1, in ImportError: No module named 'cv2'

``When I'm doingfind / -name "cv.py"`, I'm getting

/usr/lib/python2.7/dist-packages/cv.py

And for thisfind / -name "cv2.so", the result is

/usr/local/lib/python3.4/site-packages/cv2.so /usr/local/lib/python2.7/dist-packages/cv2.so /opencv-3.2.0/build/lib/cv2.so

I don't know where I'm making the mistake. Can someone please guide me?

Thank you!

edit retag flag offensive close merge delete

Comments

1

ahh, finally you made it here ;)

berak gravatar imageberak ( 2017-07-03 14:36:26 -0600 )edit

Yeah :)

Thank you!

beta gravatar imagebeta ( 2017-07-03 23:29:28 -0600 )edit

i'm neither using docker, nor the project you're trying, yet, some remarks:

  • this face_recognition project is actually using dlib for each and everything. none of the examples show any opencv usage (or i did not find it), so no wonder, their script does not include opencv (why and where do you need opencv, exactly ?)
  • you're trying to build opencv's python bindings from scratch, yet you install (outdated?) binary versions with python-opencv and libopencv-dev. those WILL get in your way later.
  • have a look here for the dependancies, those have to go before you build cv2 (e.g. libgtk2, python3-dev), no use to install that later
berak gravatar imageberak ( 2017-07-03 23:45:59 -0600 )edit

Thanks Berak for the comment! Actually opencv is used for reading video file. Actually we are trying objects in video frames. Detection is done by Dlib, but reading the video is done by opencv. I'll try the installation as you suggested. Thanks a lot!

beta gravatar imagebeta ( 2017-07-04 00:02:22 -0600 )edit
1

"opencv is used for reading video file" -- ah, makes sense. missed it.

ffmpeg (for video files) or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev (for webcam)

the cmake output will contain a lot of hints already, if this is going to work, that'll be most useful to lookat in case of trouble

berak gravatar imageberak ( 2017-07-04 00:05:14 -0600 )edit

Thanks Berak! I'll consider them.

beta gravatar imagebeta ( 2017-07-04 00:44:21 -0600 )edit