Wrong python packages path for opencv cmake instal

2019-04-08 21:16发布

问题:

I've been trying to follow the opencv installation steps from pyimagesearch.com with virtualenv. Everything works fine except for the packages path: it should be /Users/JLee/Envs/cv/lib/python2.7/site-packages but it's configured as lib/python2.7/site-packages

In Python, import cv2 works well in the global setting but doesn't work in the 'cv' virtual environment.

While following the steps from the site, I first proceeded without installing virtualenv, then realized I haven't installed it so I installed it later and followed the steps again. Could this be a problem?

 Python 2:
--     Interpreter:                 /Users/JLee/Envs/cv/bin/python2.7 (ver 2.7.10)
--     Libraries:                   /usr/lib/libpython2.7.dylib (ver 2.7.10)
--     numpy:                       /Users/JLee/Envs/cv/lib/python2.7/site-packages/numpy/core/include (ver 1.11.1)
--     packages path:               lib/python2.7/site-packages

This is the code for cmake to configure the build:

cmake -D CMAKE_BUILD_TYPE=RELEASE 
-D CMAKE_INSTALL_PREFIX=/usr/local \ 
-D PYTHON2_PACKAGES_PATH=/Users/JLee/Envs/cv/lib/python2.7/site-packages \ 
-D PYTHON2_LIBRARY=/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/bin \ 
-D PYTHON2_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers \ 
-D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=ON \ -D BUILD_EXAMPLES=ON \ 
-D OPENCV_EXTRA_MODULES_PATH=/Users/JLee/Developer/opencv_project/opencv_contrib/modules ..

Thanks for the help in advance!

回答1:

For some reason it looks like CMake didn't automatically determine your site-packages directory for your virtual environment. That's not an issue though, because all you need to do is sym-link in the cv2.so file.

Find your cv2.so file on disk (based on your output, it seems to be in lib/python2.7/site-packages) and then sym-link into your Python virtual environment site-packages directory. From there, everything will work as expected.