domingo, 3 de novembro de 2019

Building Python OpenCV Contrib with Conda on Linux/Ubuntu

Building OpenCV form the source code can be a pain in the ass when working with Conda environment. I haven't found a working conda package, and the few tutorials i found didn't workout of the box, most likely because of different versions of anaconda, ubuntu and environment.

This post is based on these tutorials,which have enough information to get the build working:

https://alexsm.com/building-opencv-with-conda/ https://docs.opencv.org/master/d2/de6/tutorial_py_setup_in_ubuntu.html


Dependencies

First, install the dependencies need for opencv (more information on the opencv tutotrial above):

>sudo apt-get install cmake
>sudo apt-get install gcc g++
>sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
>sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
>sudo apt-get install libgtk2.0-dev
>sudo apt-get install libgtk-3-dev


Creating conda environment


Create and enable a new conda environment with the wanted python version and numpy:

>conda create -n opencv_build python=3.6 numpy
>conda activate opencv_build


Get source code

Make a folder and clone the opencv and contrib git:

>mkdir opencv_install
>cd opencv_install
>cd opencv_install
>git clone https://github.com/opencv/opencv.git
>git clone https://github.com/opencv/opencv_contrib.git

If you want some specific version do the checkout, otherwise skip the commands bellow:

>cd opencv; git checkout 3.4.1 >cd ../opencv_contrib; git checkout 3.4.1
>cd .. # in opencv_install


Check paths inside conda


It is good to check the correct environment path before building. With the environment activated:

>conda info

You should see something like this:

     active environment : opencv_build
    active env location : /home/jairo/anaconda3/envs/opencv_build
            shell level : 1
       user config file : /home/jairo/.condarc
 populated config files : /home/jairo/.condarc
          conda version : 4.6.11
    conda-build version : 3.17.8
         python version : 3.7.3.final.0
       base environment : /home/jairo/anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/linux-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/jairo/anaconda3/pkgs
                          /home/jairo/.conda/pkgs
       envs directories : /home/jairo/anaconda3/envs
                          /home/jairo/.conda/envs
               platform : linux-64
             user-agent : conda/4.6.11 requests/2.21.0 CPython/3.7.3 Linux/4.15.0-66-generic ubuntu/16.04.6 glibc/2.23
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False

You need to take note of the active env location. Is this case is /home/jairo/anaconda3/envs/opencv_build.

Check the path for python executable in the active enviroment::

>which python

/home/jairo/anaconda3/envs/opencv_build/bin/python

Take note of these 2 paths.


Create make file

Create the xcmake.sh file in the opencv_install folder. Remember to update the CONDA_ENV_PATH and CONDA_PYTHON_PATH with the information above:

CONDA_ENV_PATH=/home/jairo/anaconda3/envs/opencv_build
CONDA_PYTHON_PATH=/home/jairo/anaconda3/envs/opencv_build/bin/python
WHERE_OPENCV=../opencv
WHERE_OPENCV_CONTRIB=../opencv_contrib
cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D PYTHON3_EXECUTABLE=$CONDA_PYTHON_PATH \
        -D INSTALL_C_EXAMPLES=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D OPENCV_EXTRA_MODULES_PATH=$WHERE_OPENCV_CONTRIB/modules \
        -D BUILD_EXAMPLES=ON $WHERE_OPENCV


Prepare for building


Create the folder build inside the opencv_install:

>mkdir build
>cd build
>../xcmake.sh

You should get something like this if everything is correct:

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
--     numpy:                       /home/jairo/.local/lib/python2.7/site-packages/numpy/core/include (ver 1.16.5)
--     install path:                lib/python2.7/dist-packages/cv2/python-2.7
--
--   Python 3:
--     Interpreter:                 /home/jairo/anaconda3/envs/opencv_build/bin/python (ver 3.6.9)
--     Libraries:                   /home/jairo/anaconda3/envs/opencv_build/lib/libpython3.6m.so (ver 3.6.9)
--     numpy:                       /home/jairo/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.17.3)
--     install path:                lib/python3.6/site-packages/cv2/python-3.6
--
--   Python (for build):            /home/jairo/anaconda3/envs/opencv_build/bin/python
--
--   Java:                         
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Install to:                    /home/jairo/anaconda3/envs/opencv_build
-- -----------------------------------------------------------------

You want to make sure the Python(for build) and Install to folders are correct and inside your conda environment.


Build it

If everything is fine, make it. In the build folder:

>make -j4

-j4 is a flag for paralel processing. You can remove it if you see any problem.


Install it



>sudo make install
>sudo ldconfig


Test it


Run python and type:


>python
>import cv2
>cv2.__version__

You should see the opencv version installed.
In case of problem you need to check the paths, usually the problem is wrong paths. Check the paths where python is looking for modules (always with the environment activated):

Run python and type:

>python
>import sys
>print ('\n'.join(sys.path))

/home/jairo/anaconda3/envs/opencv_build/lib/python36.zip
/home/jairo/anaconda3/envs/opencv_build/lib/python3.6
/home/jairo/anaconda3/envs/opencv_build/lib/python3.6/lib-dynload
/home/jairo/.local/lib/python3.6/site-packages
/home/jairo/anaconda3/envs/opencv_build/lib/python3.6/site-packages

This will show the folders python is looking for the modules. If they are different from the specified on the build process the installation will fail.


Using the binary in another environment


To use this binary in other environment you should copy the cv2 folder to the respective folder. In this case the folder is located at /home/jairo/anaconda3/envs/opencv_build/lib/python3.6/sites-packages/cv2, and you should copy it to the new environment (change the opencv_build to the name of the new environment already create).

Nenhum comentário:

Postar um comentário