Installing OpenCV 3.0.0 with python 2.7.9 into a virtualenv on Mac OSX without homebrew
Its pretty easy to install openCV to your machine or vagrant box but its not as easy to install onto your virtualenv without brew. I enjoy brew, it has helped me tremendously and I’ve nothing against it. But I also like to have control over what I compile and where I place it.
Tested on :
- El Capitan
Install pip, virtualenv and activate the current environment as that new env;
$ sudo easy_install pip $ pip install virtualenv $ virtualenv env $ source env/bin/activate
throughout this post dont deactivate your virtual env
Install numpy dependency
(env)$ pip install numpy
Download opencv here and unzip it. For this tutorial this is v3.0.0.
(env)$ cd /path/to/your/opencv-3.0.0 (env)$ mkdir release (env)$ cd release (env)$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$VIRTUAL_ENV -D PYTHON2_PACKAGES_PATH=$VIRTUAL_ENV/lib/python2.7/site-packages -D PYTHON2_LIBRARY=$VIRTUAL_ENV/bin -D PYTHON2_INCLUDE_DIR=$VIRTUAL_ENV/include/python2.7 -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=$VIRTUAL_ENV/opencv_contrib/modules .. (env)$ make -j4
Now go make yourself some good tea and wait because this is going to take a while.
Dont link the file to your env(
ln -s cv.py). This causes
Segmentation Fault 11errors which will drive you insane. Searching through
Segmentation Fault 11errors might drive you down into breaking more stuff.
(env)$ make install
Test your install
(env)$ python Python 2.7.9 (default, Feb 10 2015, 03:28:08) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.0.0'
(SITUATIONAL) Previous versions of Opencv #
For OpenCV V2.4.5 #
Some time ago my opencv libraries got messed up (probably because of my tinkering), I constantly got errors like
[ 66%] Building CXX object modules/ocl/CMakeFiles/opencv_test_ocl.dir/test/utility.cpp.o Linking CXX executable ../../bin/opencv_perf_ocl [ 66%] Built target opencv_perf_ocl Linking CXX executable ../../bin/opencv_test_ocl [ 66%] Built target opencv_test_ocl make: *** [all] Error 2
To bypass this I’ve set another flag on cmake
BUILD_opencv_legacy=OFF which turns off legacy support. This fixed the issue for me. #
dont forget to add your pythonpath to your ~/.profile
$ export PYTHONPATH=~/.local/lib/python2.7/site-packages:$PYTHONPATH
Hope this helps.