Installing OpenCV 3 on macOS with Homebrew

I am following this pyimagesearch tutorial to help me install OpenCV 3 on macOS Sierra 10.12.6 with Homebrew. I am using Python 2.7.15 & 3.6.4 installed with conda, instead of Python installed with Homebrew (like in the tutorial).

 Contents

   1. Major blog updates
   2. Installation steps: Homebrew install command
   3. cv2 import error
   4. Skipped brew tap
   5. --HEAD flag error
   6. Flags ignored: --with-contrib and --with-python3
   7. OpenCV installation test code
   8. SIFT and SURF test code

Major blog updates

  • May 29, 2018: added webcam test code + references, explained more the "sym-link" part, and added update of ~/.bash_profile

Installation steps: Homebrew install command

These were the prerequisites I had on my computer before installing OpenCV 3 via Homebrew:
  • Operating system: macOS Sierra 10.12.6
  • Homebrew
  • Two conda environments
          First virtual environment: Python 2.7.15 + numpy 1.14.3
          Second virtual environment: Python 3.6.4 + numpy 1.15.0

These are the 4 steps I followed to install and test OpenCV 3 on macOS Sierra 10.12.6 with Homebrew (not all of the steps from the tutorial were followed):

1. Before running the brew install command, I updated the ~/.bash_profile with
export PATH=/user/local/bin:$PATH

2. This is the command I used to install OpenCV 3 with both Python 2.7 and Python 3 bindings via Homebrew:
$ brew install opencv3 --with-contrib --with-python3

3. As explained in the pyimagesearch tutorial, you have to "sym-link" your Python 2.7 + OpenCV 3 bindings so your conda's Python 2.7 installation will look for additional packages in Homebrew OpenCV compiled's site-packages:
$ echo /usr/local/Cellar/opencv/3.4.1_5/lib/python2.7/site-packages >> ~/miniconda3/envs/cv3_py2/lib/python2.7/site-packages/opencv3.pth
Do the same for your Python 3.6 + OpenCV 3 bindings:
$ echo /usr/local/Cellar/opencv/3.4.1_5/lib/python3.6/site-packages/ >> ~/miniconda3/envs/cv3_py3/lib/python3.6/site-packages/opencv3.pth

NOTE 1: /usr/local/Cellar/opencv/3.4.1_5/lib/python{2.7,3.6}/site-packages/ is the Homebrew path to the OpenCV compile

NOTE 2: ~/miniconda3/envs/cv3_py3/lib/python{2.7,3.6}/site-packages/ is the path to the site-packages directory for conda's Python 2.7 & 3.6


cv2 import error

When you activate your conda environment and try to import cv2, if you get the error: "ImportError: numpy.core.multiarray failed to import", either your numpy is too old (I tried with numpy 1.13.3 and I got this error) or your numpy is not found in your environment. Thus, either install numpy via:
$ conda update numpy
or install your numpy with:
$ conda install numpy

Skipped brew tap

When tapping the “homebrew/science” repo like suggested in the pyimagesearch tutorial, I got an error
$ brew tap homebrew/science
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask). No changes to formulae.

Error: homebrew/science was deprecated. This tap is now empty as all its formulae were migrated.
Thus I just skipped the brew tap command which was recommended in the pyimagesearch's tutorial.

--HEAD flag error

When using the pyimagesearch tutorial's brew install command, I got an error with the --HEAD flag
$ brew install opencv3 --with-contrib --with-python3 --HEAD
[...]
Error: No head is defined for opencv
Thus I dropped the --HEAD flag from the brew install command.

Flags ignored: --with-contrib and --with-python3

Homebrew seems to not recognize --with-contrib and --with-python3 flags
Warning: opencv: this formula has no --with-contrib option so it will be ignored!
Warning: opencv: this formula has no --with-python3 option so it will be ignored!
However, python 3 + OpenCV 3 bindings are installed on the system which I confirmed. Also, the OpenCV's extra modules (features such as SIFT, SURF) are also installed which I tested.

OpenCV installation test code

We will test our OpenCV 3 installation by loading an image or streaming from a webcam, and displaying the image or webcam feed:
# Code for showing webcam feed is from Adrian Rosebrock's tutorial @
# https://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/
import cv2

print('OpenCV version: {}'.format(cv2.__version__))
# Read test image and show it
img = cv2.imread('test.png')
cv2.imshow('Window', img)
cv2.waitKey(0)

# Read from the video webcam
camera = cv2.VideoCapture(0)
# Loop over the frames of the video
while True:
    # Grab the current frame
    _, frame = camera.read()

    # Show the frame and record if the user presses a key
    cv2.imshow("Webcam Feed", frame)
    key = cv2.waitKey(1) & 0xFF
    # If the `q` key is pressed, break from the loop
    if key == ord("q"):
        break

# Cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
Test code @ gist
Reference: test code for webcam is from Adrian Rosebrock's tutorial

SIFT and SURF test code

We will test that we can access the SIFT, SURF, and other keypoint detectors and local invariant descriptors:
# Test code is from Adrian Rosebrock's blog post @ https://www.pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/
import cv2

image = cv2.imread("test.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# SIFT features
sift = cv2.xfeatures2d.SIFT_create()
(kps, descs) = sift.detectAndCompute(gray, None)
print("# kps: {}, descriptors: {}".format(len(kps), descs.shape))

# SURF features
surf = cv2.xfeatures2d.SURF_create()
(kps, descs) = surf.detectAndCompute(gray, None)
print("# kps: {}, descriptors: {}".format(len(kps), descs.shape))

# Other keypoint detectors and local invariant descriptors
# KAZE
kaze = cv2.KAZE_create()
(kps, descs) = kaze.detectAndCompute(gray, None)
print("# kps: {}, descriptors: {}".format(len(kps), descs.shape))
# AKAZE
akaze = cv2.AKAZE_create()
(kps, descs) = akaze.detectAndCompute(gray, None)
print("# kps: {}, descriptors: {}".format(len(kps), descs.shape))
# BRISK
brisk = cv2.BRISK_create()
(kps, descs) = brisk.detectAndCompute(gray, None)
print("# kps: {}, descriptors: {}".format(len(kps), descs.shape))
Test code @ gist
Reference: test code is from Adrian Rosebrock's blog post (Where did SIFT and SURF go in OpenCV 3?).

Comments

Popular posts from this blog

Install the MAMP (Mac, Apache, MySQL, PHP) stack

Deactivate conda's base environment on startup

Product review: SMONET wireless security camera system