Running OpenCV from a Python virtualenv

前端 未结 3 776
你的背包
你的背包 2020-12-02 19:40

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

相关标签:
3条回答
  • 2020-12-02 19:48

    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.

    0 讨论(0)
  • 2020-12-02 19:59

    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.

    0 讨论(0)
  • 2020-12-02 20:09

    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.)

    0 讨论(0)
提交回复
热议问题