Skip to content

Instantly share code, notes, and snippets.

@saeedghsh
Last active March 11, 2019 00:32
Show Gist options
  • Save saeedghsh/2eaaf42709ac0e931df5012dda46edfa to your computer and use it in GitHub Desktop.
Save saeedghsh/2eaaf42709ac0e931df5012dda46edfa to your computer and use it in GitHub Desktop.
install opencv3 on sierra with support for virtual environement

The following is a set of instruction, [half] borrowed from here. But it didn't work for me and I had to modify it. Finally I installed it the linux way, with modified addresses of directories for OSX. Then created a symbolic object and linked to the installed opencv The opencv installation instruction is from here, with following options modified:

PYTHON_LIBRARY=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib \
PYTHON_INCLUDE_DIR=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Headers/ \
PYTHON_EXECUTABLE=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin/python \
PYTHON2_NUMPY_INCLUDE_DIRS=/usr/local/Cellar/numpy/1.13.1/lib/python2.7/site-packages/numpy/core/include/ \

I first followed the instructions from the first link and half-way started improvising. So the python, it's include dir and numpy might seem weird. It could be much cleaner.

One of the problem, which makes it a bit hacky, was that opencv compilation requires numpy. Now if I install numpy inside a virual env and used that address to compile opencv, I don't know what would happen if I remove that particular enviroment. So I installed the dependencies of opencv independent of the virtual env and later manually add a symbolic link for cv2 to the environment.

1_ install xcode

2_ install Homebrew

3_ creat ~/.bash_profile with following lines as content

# Homebrew
export PATH=/usr/local/bin:$PATH

then: source ~/.bash_profile (what does this do exactly?)

4_ install stuff (I don't know if I should have used brew to install python, since it's already installed, but anyway)

brew install python
brew linkapps python
brew install cmake pkg-config
brew install jpeg libpng libtiff openexr
brew install eigen tbb
pip install virtualenv virtualenvwrapper
pip install numpy

Add the following to ~/.bash_profile and source it again

# Virtualenv/VirtualenvWrapper
source /usr/local/bin/virtualenvwrapper.sh

6_ install opencv

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git 

cd opencv
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D PYTHON_LIBRARY=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib \
    -D PYTHON_INCLUDE_DIR=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Headers/ \
    -D PYTHON_EXECUTABLE=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin/python \
    -D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/local/Cellar/numpy/1.13.1/lib/python2.7/site-packages/numpy/core/include/ \
    -D BUILD_opencv_python2=ON \
    -D BUILD_opencv_python3=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=ON ..

make -j4
sudo make install

7_ to give access to the opencv in the virtual environment, find the cv2.so and create symbolic links in the lib/python2.7/site-packages/ of the virtual environment.

ln -s /usr/local/lib/python2.7/site-packages/cv2.so ~/.virtualenvs/phd_dev_env/lib/python2.7/site-packages/cv2.so

The problem is I used the brew address of numpy and I don't the consequences of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment