beginh
beginh

Reputation: 1153

Two versions of OpenCV in virtualenv, python

To my best I did not find a step by step tutorial on how to achieve what I want.

I would like to have two configurations for my projects: python2 with opencv2.4 and python3 with opencv3.1. For that I clone opencv and create a virtualenv following the tutorial from here (just not raspberry PI and with CUDA, but it's just one more flag during configuration).

Q1: How should I efficiently manage the installation of opencv when I create another virtualenv, for python3? Could I somehow link to various build folders?

Removing the same build folder, checking out an opencv branch with the respective version and building it every time from scratch when I switch the virtualenv seems to be suboptimal solution.

(suppose, opencv is cloned into /home/libraries/opencv/)

Q2: The tutorial describes in section 1 installation of various dependencies for opencv and python. It is before creating virtualenv. Can I still install for example python-dev and python-dev3 simultaneously or shall I overwrite things within virtualenv?


Q3: I managed to install and symlink properly both opencv versions with respective python versions within each of the virtualenv. However I don't fully understand cmake output:

Upon executing cmake (in virtualenv that uses python2.7 by default; opencv git checkout 2.4.13 and opencv_contrib in master since there is none for opencv2.4), it takes python2.7 as interpreter. However packages path seems to not use virtualenv, why is that and why it still works?:

--   Python:
--     Interpreter:                 /home/josh/.virtualenvs/tfpy2/bin/python2 (ver 2.7.6)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
--     numpy:                       /home/josh/.virtualenvs/tfpy2/local/lib/python2.7/site-packages/numpy/core/include (ver 1.12.0)
--     packages path:               lib/python2.7/site-packages

While trying to use virtualenv with python3.4 (git checkout 3.1.0), cmake now seems to recognize two python versions, however seems to choose the wrong one 2.7 (but i somehow built successfully with the desired 3.4). Why does it find python3.4 within virtualenv and python2.7 not, plus why chooses the wrong python for build?:

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.6)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
--     numpy:                       /usr/local/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.3)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Python 3:
--     Interpreter:                 /home/josh/.virtualenvs/tfpy3/bin/python3.4 (ver 3.4.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
--     numpy:                       /home/josh/.virtualenvs/tfpy3/lib/python3.4/site-packages/numpy/core/include (ver 1.12.0)
--     packages path:               lib/python3.4/site-packages
-- 
--   Python (for build):            /usr/bin/python2.7

(I am aware that here is some solution, but isn't the main idea of virtualenv to avoid such problems automatically? Also, it seems to be similar problem as here, but by me if the wrong version of python was recognized/used in compilation, so cannot simply symlink the built library.)

Upvotes: 1

Views: 2414

Answers (2)

thewaywewere
thewaywewere

Reputation: 8636

You may consider to use Anaconda for Python development. Its Miniconda is for IoT devices like Raspberry Pi.

Anaconda does support virtual environment through its conda-env package and config. With conda-env, you can run multiple copies and versions of Python. Install OpenCV through conda install which will download the Python package from its Anaconda cloud package repository once you registered. That would reduce a lot of hassles on downwload, package dependency and installaiton. For package not available in Anaconda Cloud, any pip install supported python packages can be installed to Anaconda. There is a learning curve but not too difficult to pick up.

Hope this help.

Disclaimer: I'm not with Anaconda, am just an user of it.

Upvotes: 0

ivan_onys
ivan_onys

Reputation: 2392

A1: You may reuse OpenCV sources as follows. First, you build OpenCV 2.4, is a way similar to that you referenced, but you do git checkout 2.4.13 instead of 3.1.0 for both opencv and opencv_contrib.

Once you built and installed (with make install) version 2.4, you then delete build folder, do git checkout 3.1.0, configure build, so it uses Python 3. Build and install again. At this point you should have two OpenCV versions built and installed on your computer.

A2: virtualenv is all about python modules or python bindings for libraries. Note, how tutorial uses virtualenv: you install numpy in a virtualenv and you create a link to cv2.so built to support python version for this environment.

Upvotes: 1

Related Questions