从Python virtualenv运行OpenCV

run*_*alk 14 python opencv virtualenv

我正在尝试在我的Ubuntu Server 12.04上的virtualenv中安装OpenCV.我找到了一个讨论这个问题的线程,但设法从中提取没有信息.

我尝试使用pip install pyopencv但失败了.

...
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
Run Code Online (Sandbox Code Playgroud)

此错误仅在我第二次运行时发生pip install.如果我删除了正在删除的build/文件夹,我会收到此错误.

-- 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
Run Code Online (Sandbox Code Playgroud)

我至少安装了以下apt包.

build-essential
uuid-dev
python-dev
python-pip
libpq-dev
cmake
libboost-dev
libcv-dev
libcvaux-dev
libboost-python-dev
libboost1.48-dev
Run Code Online (Sandbox Code Playgroud)

如何在virtualenv中安装OpenCV?

Bri*_*jes 13

解雇了virtualenv并遵循本指南:http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/,直到操作和复制cv共享对象.相反,我将cv.so(从我的OpenCV-2.2.0/lib目录)复制到我的virtualenv site-packages(例如env/lib/python2.7/site-packages /).一旦cv.so在我的环境中,我就能在python中导入cv.


Dom*_*TTI 9

这是最干净的方法,使用pyenv和virtualenv插件.

使用共享库支持安装Python(因此我们在Mac OS X上获得libpython2.7.dylib或在Linux上获得libpython2.7.so).

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install -v 2.7.6
Run Code Online (Sandbox Code Playgroud)

根据我们刚刚安装的python版本创建virtualenv.

pyenv virtualenv 2.7.6 myvirtualenv
Run Code Online (Sandbox Code Playgroud)

激活virtualenv.

pyenv shell myvirtualenv
pyenv rehash
Run Code Online (Sandbox Code Playgroud)

安装numpy.否则opencv将无法正确链接到Python.

pip install numpy
Run Code Online (Sandbox Code Playgroud)

设置python安装的前缀.

PREFIX_MAIN=`pyenv virtualenv-prefix`
Run Code Online (Sandbox Code Playgroud)

设置环境的前缀.(原文如此!这些pyenv命令的名称有点欺骗!)

PREFIX=`pyenv prefix`
Run Code Online (Sandbox Code Playgroud)

现在配置并安装opencv.请注意,opencv二进制文件和包将安装在virtualenv中,同时使用动态库和Python安装包含.

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
Run Code Online (Sandbox Code Playgroud)

(在OSX上,将libpython2.7.so替换为libpython2.7.dylib.)