Wednesday, April 19, 2017

Installing OpenCV on MacOS Sierra 10.12.4

Had some issues today getting OpenCV library rolling on my MacOS (Using Python 3). I was getting an error after the initial install: 

pip install python-opencv

This code:

import cv2

img = cv2.imread('test.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


failed with this error:

The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or
Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and
pkg-config, then re-run cmake or configure script.


I was fortunate enough to find the following article: https://solarianprogrammer.com/2016/11/29/install-opencv-3-with-python-3-on-macos/

I use Anaconda to manage my Python environments. Here is what I did to resolve the issue. NOTE: I could not get it to work on Python version newer than 3.5.2. I also use PyCharm as my interpreter, I had to refresh my interpreter for the project for the changes to take affect. 

Switch to your desired Python Environment. 



[Wed Apr 19 15:29:14] ~
 wb@Westons-MBP > source activate py3

(py3)
[Wed Apr 19 15:29:59] ~
 wb@Westons-MBP > conda install --channel https://conda.anaconda.org/menpo opencv3
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment /Users/wb/anaconda/envs/py3:

The following NEW packages will be INSTALLED:

  hdf5: 1.8.17-1
  mkl: 2017.0.1-0
  numpy: 1.12.1-py35_0
  opencv3: 3.1.0-py35_0 menpo
  tbb: 4.3_20141023-0 menpo

Proceed ([y]/n)? y

hdf5-1.8.17-1. 100% |#####################################| Time: 0:00:00 5.67 MB/s
numpy-1.12.1-p 100% |#####################################| Time: 0:00:00 13.01 MB/s
opencv3-3.1.0- 100% |#####################################| Time: 0:00:03 11.73 MB/s

(py3)
[Wed Apr 19 15:30:35] ~
 wb@Westons-MBP > python --version
Python 3.5.2 :: Continuum Analytics, Inc.