paint-brush
Installing OpenCV 3.1 for Python on MacOSX Sierraby@grvashu

Installing OpenCV 3.1 for Python on MacOSX Sierra

by Gaurav SinghAugust 26th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Originally developed by Intel, OpenCV is a real-time computer vision programming library available for cross-platform usage. The library is cross-platform and free for use under the open-source BSD license.
featured image - Installing OpenCV 3.1 for Python on MacOSX Sierra
Gaurav Singh HackerNoon profile picture

Originally developed by Intel, OpenCV is a real-time computer vision programming library available for cross-platform usage. The library is cross-platform and free for use under the open-source BSD license.


“It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing. Enabled with OpenCL, it can take advantage of the hardware acceleration of the underlying heterogeneous compute platform. Adopted all around the world, OpenCV has more than 47 thousand people of user community and estimated number of downloads exceeding 14 million. Usage ranges from interactive art, to mines inspection, stitching maps on the web or through advanced robotics.” — OpenCV Org.

OpenCV, despite being powerful is slightly intimidating for beginners due to its slightly long installation procedure. I’m writing this simple guide to get started with OpenCV 3.1 for Python on MacOSX Sierra to simplify the setup process. Feel free to leave suggestions in the comments.

For this installation, I will be using Anaconda (Python Distribution and Package Manager) which is simple, to begin with. I’m using Anaconda 4.4.0 for MacOSX with Python 3.6 on my computer.

While Anaconda comes with major scientific Python modules, it doesn’t come with the OpenCV module. But OpenCV module (unofficial) is available on few Anaconda package channels. We’re here going to use Menpo/OpenCV 3.1 to install OpenCV.

Note. While the latest stable release of OpenCV is 3.2 (as I’m writing this) but OpenCV 3.2 is not available for x64 channels on Anaconda. Hence, we will be using 3.1 here.

Step 1. Open Terminal and create a new Python environment.

conda create -yn opencvtest python=3.5

Make sure that you use Python 3.5 with OpenCV 3.1, Python 3.6 produces some incompatibilities. The ‘opencvtest’ is the name of the new environment which can be anything else also.

Step 2. Switch environment

source activate opencvtest

Get into the new environment to install OpenCV and use it.

Step 3. Install OpenCV module

conda search -c menpo --spec 'opencv=3*'

You may search for available builds for the OpenCV module and install the desired one. For this guide, as I said earlier I will be going to use OpenCV 3.1.

conda install -y -c menpo opencv=3.1

This will install the OpenCV 3.1 and its dependencies and you’re now ready to use the OpenCV.

You may now test a simple OpenCV program. The below program uses Haar Cascade classifiers to detect any human faces and eyes and draws a rectangle on the input image.


import numpy as npimport cv2


face_cascade = cv2.CascadeClassifier('path/to/haarcascade_frontalface_default.xml')eye_cascade = cv2.CascadeClassifier('path/to/haarcascade_eye.xml')


img = cv2.imread('image.jpg')gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)








faces = face_cascade.detectMultiScale(gray, 1.3, 5)for (x,y,w,h) in faces:cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)roi_gray = gray[y:y+h, x:x+w]roi_color = img[y:y+h, x:x+w]eyes = eye_cascade.detectMultiScale(roi_gray)for (ex,ey,ew,eh) in eyes:cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)



cv2.imshow('img',img)cv2.waitKey(0)cv2.destroyAllWindows()

opencvtest.png

Harr-Cascade — Face Detection Example | Photo by Mike Wilson on Unsplash