14

I'm trying to install OpenCV within a virtualenv on my Ubuntu Server 12.04. I found a thread discussing this but managed to extract no information from it.

I tried using pip install pyopencv but it failed.

...
package/extras/core/ndarray.cpp:598:1:   instantiated from here

package/extras/core/ndarray.cpp:546:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘Py_intptr_t {aka long int}’ [-Wformat]

package/extras/core/ndarray.cpp: In function ‘boost::python::api::object sdcpp::from_ndarray_impl(const sdcpp::ndarray&) [with T = cv::Scalar_<double>]’:

package/extras/core/ndarray.cpp:601:1:   instantiated from here

package/extras/core/ndarray.cpp:546:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘Py_intptr_t {aka long int}’ [-Wformat]

package/extras/core/ndarray.cpp: In function ‘boost::python::api::object sdcpp::from_ndarray_impl(const sdcpp::ndarray&) [with T = cv::Range]’:

package/extras/core/ndarray.cpp:604:1:   instantiated from here

package/extras/core/ndarray.cpp:546:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘Py_intptr_t {aka long int}’ [-Wformat]

error: command 'gcc' failed with exit status 1

This error only occurs the second time I run pip install. If I delete the remainging build/ folder I get this error.

-- Configuring incomplete, errors occurred!

Configuring PyOpenCV via CMake...

Error: error occurred while running CMake to configure PyOpenCV.

You may want to manually configure PyOpenCV by running cmake's tools:

    mkdir build

    cd build

    cmake-gui ..    OR    cmake ..

    cd ..

----------------------------------------
Command python setup.py egg_info failed with error code 255

I have at least the following apt packages installed.

build-essential
uuid-dev
python-dev
python-pip
libpq-dev
cmake
libboost-dev
libcv-dev
libcvaux-dev
libboost-python-dev
libboost1.48-dev

How can I install OpenCV within my virtualenv?

Community
  • 1
  • 1
runfalk
  • 1,996
  • 1
  • 17
  • 20
  • I'm experiencing this same issue. I noticed, though, that 'pyopencv' corresponds to this project http://code.google.com/p/pyopencv/ and not the python bindings that come with opencv 2.X – Nate Parsons Nov 09 '12 at 18:11

3 Answers3

13

Fired up a virtualenv and followed this guide: http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/ , up until manipulating and copying the cv shared objects. Instead, I copied cv.so (from my OpenCV-2.2.0/lib directory) to my virtualenv site-packages (eg. env/lib/python2.7/site-packages/). Once cv.so was in my environment, I was able to import cv within python.

Brian Cajes
  • 3,274
  • 3
  • 21
  • 22
  • 1
    Thanks, copying the shared objects was what I was missing! Only difference was I copied cv.py[c] and cv2.so from /usr/lib/pymodules, analagous to https://github.com/ingenuitas/SimpleCV#installation – Nate Parsons Nov 09 '12 at 20:46
9

Here is the cleanest way, using pyenv and the virtualenv plug-in.

Install Python with shared library support (so we get a libpython2.7.dylib on Mac OS X or libpython2.7.so on Linux).

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install -v 2.7.6

Create the virtualenv, based on the version of python we just installed.

pyenv virtualenv 2.7.6 myvirtualenv

Activate the virtualenv.

pyenv shell myvirtualenv
pyenv rehash

Install numpy. Otherwise opencv will fail to link itself to Python correctly.

pip install numpy

Set the prefix of the python install.

PREFIX_MAIN=`pyenv virtualenv-prefix`

Set the prefix of the environment. (sic! The name of these pyenv commands are a bit deceptive!)

PREFIX=`pyenv prefix`

Now configure and install opencv. Notice that the opencv binaries and packages will be installed in our virtualenv while the dynamic library and includes of the Python install is used.

cd openCV2.4
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX="$PREFIX" -DPYTHON_EXECUTABLE="$PREFIX"/bin/python2.7 -DPYTHON_LIBRARY="$PREFIX_MAIN"/lib/libpython2.7.so -DPYTHON_INCLUDE_DIR="$PREFIX_MAIN"/include/python2.7 -DPYTHON_PACKAGES_PATH="$PREFIX"/lib/python2.7/site-packages/ ..
make install

(On OSX, replace libpython2.7.so with libpython2.7.dylib.)

Dominique PERETTI
  • 1,063
  • 12
  • 12
0

Did you already apt-get build-dep python-opencv? This will install all the required dependencies to build it from source; which you need if you are trying to install it in a virtual environment.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I have all this opencv.i386 : Collection of algorithms for computer vision opencv-devel.i386 : Development files for using the OpenCV library opencv-python.i386 : Python bindings for apps which use OpenCV I use Fedora , the error is:"/include/c++/4.3.2/bits/stl_move.h:87: error: within this context" I using virtualenv.py . – Cătălin George Feștilă Jan 27 '13 at 09:34
  • @CatalinFestila you should open another discussion as your question is different than what this is talking about. – Burhan Khalid Jan 27 '13 at 09:48