Installing OpenCV3 and Python3 in virtualenv on MacOS Sierra

I spent almost 3 hours today and finally got CV3 installed. I found this detailed tutorial by Andrian Rosebrock early on:

http://www.pyimagesearch.com/2016/12/05/macos-install-opencv-3-and-python-3-5/

My Hackintosh desktop has not been upgraded for 3 years after it was built in 2013 (running Mountain Lion), and there was no way to get homebrew properly installed due to “Command Line Tool version too old” error. So I decided to upgrade my MacBook Air to the latest MacOS Sierra and install OpenCV on it, also to get a more (physically) portable development environment.

One thing I had installed on my MBA is Anaconda. This, as it proved later on, made my installation a bit more complicated and I had to make adjustment to the tutorial to make OpenCV work.

$ python --version
Python 2.7.10 :: Anaconda 2.3.0 (x86_64)
$ conda --version
conda 3.14.1

Here is a list of adjustments I had to make:

  • virtualenvwrapper.sh is not in /usr/local/bin/, but rather in

    /anaconda/bin/. Specify this path instead in ~/.bash_profile. 

  • After installing virtualenv, when creating virtualenv named cv and later calling workon cv, I got the following error message:
    Usage: source deactivate
    
    removes the 'bin' directory of the environment activated with 'source
    activate' from PATH. 

The error message is a bit cryptic, but it turns out that anaconda has a command called deactivate in Anaconda’s bin. So I had to rename the activate and deactivate commands.

$ cd /anaconda/bin/
$ mv activate activate_ana 
$ mv deadectivate deactivate_ana

http://stackoverflow.com/questions/19869059/why-i-am-suddenly-seeing-usage-source-deactivate-whenever-i-run-virtualenvwra

  • OpenCV compiled correctly, but when I tried to import it, it throw an error message “libhdf5.10.dylib not loaded opencv”. The culprit is also the previously installed anaconda.
$ sudo find / -name libhdf5.10.dylib
/anaconda/lib/libhdf5.10.dylib
/anaconda/pkgs/hdf5-1.8.15.1-1/lib/libhdf5.10.dylib

Luckily I found a blog post in Japanese (日本語ができてよかった!) and the blogger had almost exactly the same problem as this. The problem is solved by

$ brew tap homebrew/science
$ brew install hdf5
$ brew search

http://www.ketsuago.com/entry/2016/07/03/010606

(cv) [ llgc @ megatron ~ ]$ python
Python 3.6.0 (default, Dec 24 2016, 08:01:42) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0-dev'

Now I got Python 3.6 + OpenCV 3.2 successfully installed and imported. I will update this post if there are more hiccups caused by Anaconda down the road.

Edit: 01/11/2017

An alternative way to load and display image to cv2 is using matplotlib module. However, the latest version (matplotlib-1.5.3) does not work with virtual environment (e.g., non-system version of Python), and throws a “Framework” error The fix is to install an older version that is compatible with virtual environment (such as 1.4.3).

$ pip uninstall matplotlib
$ pip install matplotlib==1.4.3

 

Advertisements
Installing OpenCV3 and Python3 in virtualenv on MacOS Sierra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s