从Python virtualen运行OpenCV

2024-05-17 06:33:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的Ubuntu服务器12.04上的virtualenv中安装OpenCV。我找到了a thread discussing this,但没有从中提取任何信息。

我试过使用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

此错误仅在我第二次运行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

我至少安装了以下apt包。

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

如何在我的virtualenv中安装OpenCV?


Tags: pipfromcoredevbuildcmakeextraspackage
3条回答

启动一个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站点包(例如env/lib/python2.7/sitepackages/)。一旦cv.so进入我的环境,我就可以在python中导入cv。

你已经apt-get build-dep python-opencv了吗?这将安装从源代码构建它所需的所有依赖项;如果要在虚拟环境中安装它,则需要安装这些依赖项。

下面是最干净的方法,使用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

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

pyenv virtualenv 2.7.6 myvirtualenv

激活virtualenv。

pyenv shell myvirtualenv
pyenv rehash

安装numpy。否则opencv将无法将自身正确链接到Python。

pip install numpy

设置python安装的前缀。

PREFIX_MAIN=`pyenv virtualenv-prefix`

设置环境的前缀。(原文如此!这些pyenv命令的名字有点骗人!)

PREFIX=`pyenv prefix`

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

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

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

相关问题 更多 >