Install OpenCV 3 on macOS with Homebrew (the easy way)

homebrew_opencv3_header

Over the past few weeks I have demonstrated how to compile OpenCV 3 on macOS with Python (2.7, 3.5) bindings from source.

Compiling OpenCV via source gives you complete and total control over which modules you want to build, how they are built, and where they are installed.

All this control can come at a price though.

The downside is that determining the correct CMake paths to your Python interpreter, libraries, and include directories can be non-trivial, especially for users who are new to OpenCV/Unix systems.

That begs the question…

“Is there an easier way to install OpenCV on macOS? A way that avoids the complicated CMake configuration?”

It turns out, there is — just use Homebrew, what many consider to be “the missing package manager for Mac”.

So, is it really that easy? Just can a few simple keystrokes and commands can be used to avoid the hassle and install OpenCV 3 without the headaches?

Well, there’s a little more to it than that…but the process is greatly simplified. You lose a bit of control (as compared to compiling from source), but what you gain is an easier to follow path to installing OpenCV on your Mac system.

To discover the easy way to install OpenCV 3 on macOS via Homebrew, just keep reading.

Install OpenCV 3 on macOS with Homebrew (the easy way)

The remainder of this blog post demonstrates how to install OpenCV 3 with both Python 2.7 and Python 3 bindings on macOS via Homebrew. The benefit of using Homebrew is that it greatly simplifies the install process (although it can pose problems of its own if you aren’t careful) to only a few set of commands that need to be run.

If you prefer to compile OpenCV from source with Python bindings on macOS, please refer to these tutorials:

Step #1: Install XCode

Before we can install OpenCV 3 on macOS via Homebrew, we first need to install Xcode, a set of software development tools for the Mac Operating System.

Download Xcode

The easiest method to download and install Xcode is to use the included App Store application on your macOS system. Simply open up App Store, search for “Xcode” in the search bar, and then click the “Get” button:

Figure 1: Downloading and installing Xcode on macOS.

Figure 1: Downloading and installing Xcode on macOS.

Depending on your internet connection and system speed, the download and install process can take anywhere from 30 to 60 minutes. I would suggest installing Xocde in the background while you are getting some other work done or going for a nice long walk.

Accept the Apple developer license

I’m assuming that you’re working with a fresh install of macOS and Xcode. If so, you’ll need to accept the developer license before continuing. Personally, I think this is easier to do via the terminal. Just open up a terminal and execute the following command:

Scroll to the bottom of the license and accept it.

If you have already installed Xcode and previously accepted the Apple developer license, you can skip this step.

Install the Apple Command Line Tools

Now that Xcode is installed and we have accepted the Apple developer license, we can install the Apple Command Line Tools. These tools include packages such as make, GCC, clang, etc. This is a required step, so make you install the Apple Command line tools via:

When executing the above command you’ll see a confirmation window pop up asking you to approve the install:

Figure 2: Installing Apple Command Line Tools on macOS.

Figure 2: Installing Apple Command Line Tools on macOS.

Simply click the “Install” button to continue. The actual install process of Apple Command Line Tools should take less than a 5 minutes.

Step #2: Install Homebrew

We are now ready to install Homebrew, a package manager for macOS. You can think of Homebrew as the macOS equivalent of the Ubuntu/Debian-based apt-get.

Installing Homebrew is dead simple — simply copy and paste the command below the “Install Homebrew” section of the Homebrew website (make sure you copy and paste the entire command into your terminal). I have included the command below as reference:

After Homebrew installs you should make sure the package definitions (i.e., the instructions used to install a given library/package) are up to date by executing the following command:

Now that Homebrew is successfully installed and updated, we need to update our ~/.bash_profile  file so that it searches the Homebrew path for packages/libraries before it searches the system path. Failure to complete this step can lead to confusing errors, import problems, and segfaults when trying to utilize Python and OpenCV, so make sure you update your ~/.bash_profile  file correctly!

The ~/.bash_profile  file may or may not already exist on your system. In either case, open it with your favorite text editor (I’ll be using nano  in this example):

And then insert the following lines at the bottom of the file (if ~/.bash_profile  does not exist the file will be empty — this is okay, just add the following lines to the file):

All this snippet is doing is updating your PATH  variable to look for libraries/binaries along the Homebrew path before it searches the system path.

After updating the ~/.bash_profile  file, save and exit your text editor.

To make sure you are on the right path, I have included a screenshot of my ~/.bash_profile  below so you can compare it to yours:

Figure 3: Updating my .bash_profile file to include Homebrew.

Figure 3: Updating my .bash_profile file to include Homebrew.

Remember, your ~/.bash_profile  may look very different than mine — that’s okay! Just make sure you have included the above Homebrew snippet in your file, followed by successfully saving and editing the editor.

Finally, we need to manually source  the ~/.bash_profile  file to ensure the changes have been reloaded:

The above command only needs to be executed once. Whenever you open up a new terminal, login, etc., your .bash_profile  file will be automatically loaded and sourced for you.

Step #3: Install Python 2.7 and Python 3 using Homebrew

The next step is to install the Homebrew versions of Python 2.7 and Python 3. It is considered bad form to develop against the system Python as your main interpreter. The system version of Python should serve exactly that — system routines.

Instead, you should install your own version of Python that is independent from the system install. Using Homebrew, we can install both Python 2.7 and Python 3 using the following command:

At the time of this writing the current Python versions installed by Homebrew are Python 2.7.12 and Python 3.5.2.

After the Python 2.7 and Python 3 install completes, we need to create some symbolic links:

As a sanity check, let’s confirm that you are using the Homebrew version of Python rather than the system version of Python. You can accomplish this via the which  command:

Inspect the output of which  closely. If you are see /usr/local/bin/python  and /usr/local/bin/python3  for each of the paths then you are correctly using the Homebrew versions of Python. However, if the output is instead /usr/bin/python  and /usr/bin/python3  then you are incorrectly using the system version of Python.

If you find yourself in this situation you should:

  1. Go back to Step #2 and ensure Homebrew installed without error.
  2. Check that brew install python python3  finished successfully.
  3. You have correctly updated your ~/.bash_profile  file and reloaded the changes via source . Your ~/.bash_profile  should look similar to mine in Figure 3 above.

Step #4: Install OpenCV 3 with Python bindings on macOS using Homebrew

Now that we have installed the Homebrew versions of Python 2.7 and Python 3 we are now ready to install OpenCV 3.

Tap the “homebrew/science” repo

The first step is to add the homebrew/science  repository to the set of packages we are tracking. This allows us to access the formulae to install OpenCV. To accomplish this, just use the following command:

Understanding the “brew install” command

To install OpenCV on our macOS system via Homebrew we are going to use the brew install  command. This command accepts the name of a package to install (like Debian/Ubuntu’s apt-get), followed by set of optional arguments.

The base of our command is: brew install opencv3 ; however, we need to add some additional parameters.

The most important set of parameters are listed below:

  • --with-contrib : This ensures that the opencv_contrib repository is installed, giving us access to additional, critical OpenCV features such as SIFT, SURF, etc.
  • --with-python3 : OpenCV 3 + Python 2.7 bindings will be automatically compiled; however, to compile OpenCV 3 + Python 3 bindings we need to explicitly supply the --with-python3  switch.
  • --HEAD : Rather than compiling a tagged OpenCV release (i.e., v3.0, v3.1, etc.) the --HEAD  switch instead clones down the bleeding-edge version of OpenCV from GitHub. Why would we bother doing this? Simple. We need to avoid the QTKit error that plagues macOS Sierra systems with the current tagged OpenCV 3 releases (please see the “Avoiding the QTKit/QTKit.h file not found error” section of this blog post for more information)

You can see the full listing of options/switches by running brew info opencv3 , the output of which I’ve included below:

For those who are curious, the Homebrew formulae (i.e., the actual commands used to install OpenCV 3) can be found here. Use the parameters above and the install script as a reference if you want to add any additional OpenCV 3 features.

We are now ready to install OpenCV 3 with Python bindings on your macOS system via Homebrew. Depending on the dependencies you do or do not already have installed, along with the speed of your system, this compilation could easily take a couple of hours, so you might want to go for a walk once you kick-off the install process.

Installing OpenCV 3 with Python 3 bindings via Homebrew

To start the OpenCV 3 install process, just execute the following command:

This command will install OpenCV 3 on your macOS system with both Python 2.7 and Python 3 bindings via Homebew. We’ll also be compiling the latest, bleeding edge version of OpenCV 3 (to avoid any QTKit errors) along with opencv_contrib  support enabled.


Update:

If the above command exits with the error  Error: opencv3: Does not support building both Python 2 and 3 wrappers , then there is likely a misconfiguration in the Homebrew formula (hopefully the Homebrew formula will be updated in the future).

In the meantime, you should be able to get around around the error simply by omitting the --HEAD  option, like this:

More information on the error can be found in this StackOverflow thread.


As I mentioned, this install process can take some time so consider going for a long walk while OpenCV installs. However, make sure your computer doesn’t go to sleep/shut down while you are gone! If it does, the install process will break and you’ll have to restart it.

Assuming OpenCV 3 installed without a problem, your terminal output should look similar to mine below:

Figure 5: Compiling and installing OpenCV 3 with Python bindings on macOS with Homebrew.

Figure 4: Compiling and installing OpenCV 3 with Python bindings on macOS with Homebrew.

However, we’re not quite done yet.

You’ll notice a little note at the bottom of the install output:

This means that our Python 2.7 + OpenCV 3 bindings are now installed in /usr/local/opt/opencv3/lib/python2.7/site-packages , which is Homebrew path to the OpenCV compile. We can verify this via the ls  command:

However, we need to get these bindings into /usr/local/lib/python2.7/site-packages/ , which is the site-packages  directory for Python 2.7. We can do this by executing the following command:

The above command creates a .pth  file which tells Homebrew’s Python 2.7 install to look for additional packages in /usr/local/opt/opencv3/lib/python2.7/site-packages — in essence, the .pth  file can be considered a “glorified sym-link”.

At this point you now have OpenCV 3 + Python 2.7 bindings installed!

However, we’re not quite done yet…there is still a few extra steps we need to take for Python 3.

Handling the Python 3 issue

Remember the --with-python3  option we supplied to brew install opencv3 ?

Well, this option did work (although it might not seem like it) — we do have Python 3 + OpenCV 3 bindings installed on our system.

Note: A big thank you to Brandon Hurr for pointing this out. For a long time I thought the --with-python3  switch simply wasn’t working.

However, there’s a bit of a problem. If you check the contents of /usr/local/opt/opencv3/lib/python3.5/site-packages/  you’ll see that our cv2.so  file has a funny name:

I have no idea why the Python 3 + OpenCV 3 bindings are not named cv2.so  as they should be, but the same is true across operating systems. You’ll see this same issue on macOS, Ubuntu, and Raspbian.

Luckily, the fix is easy — all you need to do is rename cv2.cpython-35m-darwin.so  to cv2.so :

From there, we can create another .pth  file, this time for the Python 3 + OpenCV 3 install:

At this point you now have both Python 2.7 + OpenCV 3 and Python 3 + OpenCV 3 installed on your macOS system via Homebrew.

Verifying that OpenCV 3 has been installed

Here are the commands I use to validate that OpenCV 3 with Python 2.7 bindings are working on my system:

The screenshot below shows how to import the OpenCV 3 bindings into a Python 3 shell as well:

Figure 6: Confirming that OpenCV 3 with Python 3 bindings have been successfully installed on my macOS system via Homebrew.

Figure 5: Confirming that OpenCV 3 with Python 3 bindings have been successfully installed on my macOS system via Homebrew.

Congratulations, you have installed OpenCV 3 with Python bindings on your macOS system via Homebrew!

But if you’re a longtime reader reader of this blog, you know that I use Python virtual environments extensively — and you should too.

Step #5: Setup your Python virtual environment (optional)

You’ll notice that unlike many of my previous OpenCV 3 install tutorials, Homebrew does not make use of Python virtual environments, a best practice when doing Python development.

While Steps #5-#7 are optional, I highly recommend that you do them to ensure your system is configured in the same way as my previous tutorials. You’ll see many tutorials on the PyImageSearch blog leverage Python virtual environments. While they are indeed optional, you’ll find that in the long run they make your life easier.

Installing virtualenv and virtualenvwrapper

The virtualenv and virtualenvwrapper packages allow us to create separate, independent Python virtual environments for each project we are working on. I’ve mentioned Python virtual environments many times before on this blog so I won’t rehash what’s already been said. Instead, if you are unfamiliar with Python virtual environments, how they work, and why we use them, please refer to the first half of this blog post. I also recommend this excellent tutorial on the RealPython.com blog that takes a more in-depth dive into Python virtual environments.

To install both virtualenv  and virtualenvwrapper , just use pip :

After both packages have successfully installed, you’ll need to update your ~/.bash_profile  file again:

Append the following lines to the file:

After updating, your ~/.bash_profile  should look similar to mine below:

Figure 7: Update your .bash_profile file to include virtualenv/virtualenvwrapper.

Figure 6: Update your .bash_profile file to include virtualenv/virtualenvwrapper.

Once you have confirmed that your ~/.bash_profile  has been created, you need to refresh your shell by using the source  command:

This command only needs to be executed once. Assuming that your ~/.bash_profile  has been updated correctly, it will automatically be loaded and source ‘d each time you open a new shell, login, etc.

Create your Python virtual environment

We are now ready to use the mkvirtualenv  command to create a Python virtual environment named cv  (for “computer vision”).

For Python 2.7 use the following command:

For Python 3 use this command:

The -p  switch controls which Python version is used to create your virtual environment. Please note that each virtual environment needs to be uniquely named so if you want to create two separate virtual environments, one for Python 2.7 and another for Python 3, you’ll want to make sure that each environment has a separate name — both cannot be named “cv”.

The mkvirtualenv  command only needs to be executed once. To access the cv  Python virtual environment after you have already created it, just use the workon  command:

To visually validate you are in the cv  virtual environment, just examine your command line. If you see the text (cv)  preceding the prompt, then you are in the cv  virtual environment:

Figure 8: Make sure you see the "(cv)" text on your prompt, indicating that you are in the cv virtual environment.

Figure 7: Make sure you see the “(cv)” text on your prompt, indicating that you are in the cv virtual environment.

Otherwise, if you do not  see the cv  text, then you are not in the cv  virtual environment:

Figure 9: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and you need to run the "workon" command to resolve this issue before continuing.

Figure 8: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and you need to run the “workon” command to resolve this issue before continuing.

Install NumPy

The only Python prerequisite for OpenCV is NumPy, a scientific computing package.

To install NumPy, first make sure you are in the cv  virtual environment and then let pip  handle the actual installation:

Step #6: Sym-link the OpenCV 3 bindings (optional)

We are now ready to sym-link in the cv2.so  bindings into our cv  virtual environment. I have included the commands for both Python 2.7 and Python 3, although the process is very similar.

For Python 2.7

To sym-link the cv2.so  bindings into your Python 2.7 virtual environment named cv , use these commands:

For Python 3:

To sym-link the cv2.so  bindings installed via Homebrew to your Python 3 virtual environment (named cv ), execute these commands:

Repeat as necessary

If you would like to have OpenCV 3 bindings installed for both Python 2.7 and Python 3, then you’ll want to repeat Step #5 and Step #6 for both Python versions. This includes creating a uniquely named Python virtual environment, installing NumPy, and sym-linking in the cv2.so  bindings.

Step #7: Test your OpenCV 3 install (optional)

To verify that your OpenCV 3 + Python + virtual environment install on macOS is working properly, you should:

  1. Open up a new terminal window.
  2. Execute the workon  command to access the cv  Python virtual environment.
  3. Attempt to import your Python + OpenCV 3 bindings on macOS.

Here are the exact commands I used to validate that my Python virtual environment + OpenCV install are working correctly:

Note that the above output demonstrates how to use OpenCV 3 + Python 2.7 with virtual environments.

I also created an OpenCV 3 + Python 3 virtual environment as well (named py3cv3 ), installed NumPy, and sym-linked the OpenCV 3 bindings. The output of me accessing the py3cv3  virtual environment and importing OpenCV can be seen below:

Figure 10: Utilizing virtual environments with Python 3 + OpenCV 3 on macOS.

Figure 10: Utilizing virtual environments with Python 3 + OpenCV 3 on macOS.

So, what’s next?

Congrats! You now have a brand new, fresh install of OpenCV on your macOS system — and I’m sure you’re just itching to leverage your install to build some awesome computer vision apps…

…but I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and probably feeling a bit confused and overwhelmed on exactly where to start.

Personally, I’m a big fan of learning by example, so a good first step would be to have some fun and read this blog post on detecting cats in images/videos. This tutorial is meant to be very hands-on and demonstrate how you can (quickly) build a Python + OpenCV application to detect the presence of cats in images.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real-world computer vision problems including face detection in images and video streamsobject tracking in video, and handwriting recognition.

curious_about_cv

So, let’s put that fresh install of OpenCV 3 on your macOS system to good use — just click here to learn more about the real-world projects you can solve using Practical Python and OpenCV.

Summary

In today’s blog post I demonstrated how to install OpenCV 3 with Python 2.7 and Python 3 bindings on your macOS system via Homebrew.

As you can see, utilizing Homebrew is a great method to avoid the tedious process of manually configuring your CMake command to compile OpenCV via source (my full list of OpenCV install tutorials can be found on this page).

The downside is that you lose much of the control that CMake affords you.

Furthermore, while the Homebrew method certainly requires executing less commands and avoids potentially frustrating configurations, it’s still worth mentioning that you still need to do a bit of work yourself, especially when it comes to the Python 3 bindings.

These steps also compound if you decide to use virtual environments, a best practice when doing Python development.

When it comes to installing OpenCV 3 on your own macOS system I would suggest you:

  1. First try to install OpenCV 3 via source. If you run into considerable trouble and struggle to get OpenCV 3 to compile, use this as an opportunity to teach yourself more about Unix environments. More times than not, OpenCV 3 failing to compile is due to an incorrect CMake parameter that can be correctly determined with a little more knowledge over Unix systems, paths, and libraries.
  2. Use Homebrew as a fallback. I would recommend using the Homebrew method to install OpenCV 3 as your fallback option. You lose a bit of control when installing OpenCV 3 via Homebrew, and worse, if any sym-links break during a major operating system upgrade you’ll struggle to resolve them. Don’t get me wrong: I love Homebrew and think it’s a great tool — but make sure you use it wisely.

Anyway, I hope you enjoyed this blog post! And I hope it helps you get OpenCV 3 installed on their macOS systems.

If you’re interested in learning more about OpenCV, computer vision, and image processing, be sure to enter your email address in the form below to be notified when new blog posts + tutorials are published!

, , , , , ,

106 Responses to Install OpenCV 3 on macOS with Homebrew (the easy way)

  1. Javier de la Rosa December 19, 2016 at 11:25 am #

    Thanks! This is super useful. FWIW, I’ve also been using the opencv-python [1] package. If the architectures, Python versions, and OS versions are supported, it’s a super easy alternative.

    [1] https://pypi.python.org/pypi/opencv-python

    • Adrian Rosebrock December 21, 2016 at 10:34 am #

      Thanks for sharing Javier. I personally have not used the opencv-python package. I’ll look into this more.

      My biggest worry is that the opencv_contrib package isn’t installed by default. I can’t seem to find any mention of this in their GitHub repo?

      • Javier de la Rosa January 2, 2017 at 10:10 pm #

        Yeah, it seems like unfortunately opencv_contrib is not there yet.

        • Adrian Rosebrock January 4, 2017 at 10:49 am #

          That’s good to know — and also sounds like a great pull request for the project 🙂

      • Quang February 15, 2017 at 11:34 pm #

        I tried “opencv-python” before finding out this blog. I still recommend newcomers to install opencv from source, or through homebrew. opencv-python works by providing a precompile cv.so and the Python binding. It is fast and extremely convenient, but at the cost of the limitation of that cv.so. It will not have UI support, since it is not compiled with GTK on *your* computer.

        That may work for some people, when they know exactly what they need, and can work around the limitation. But for newcomers, it is better to grab everything, and save yourself a headache of finding out which is missing.

        • Adrian Rosebrock February 16, 2017 at 9:48 am #

          Thank you for sharing Quang, I didn’t realize that “opencv-python” didn’t have UI support out of the box. I’ll also worth nothing that the “opencv_contrib” module is not included either.

  2. Scott Sideleau December 19, 2016 at 11:33 am #

    I think that it’s easier to install/manage Python3 bindings via ‘pip’, the Python package manager. In this case, the Homebrew dependencies shrink and the additional flags are not required, as specifying the pip3 installer handles the configuration:

    $ brew install python3 opencv3
    $ pip3 install scipy scikit-image matplotlib opencv-python

    This works to get the compare-two-images example from 2014/09/15 running correctly on MacOS 10.11+, for example.

    • Adrian Rosebrock December 21, 2016 at 10:35 am #

      Thanks for sharing Scott. I’m not familiar with the opencv-python package, but I’ll check it out.

      As I mentioned to “Javier” above, I’m concerned with opencv_contrib isn’t installed by default, but I’ll look into this.

  3. Stewart F December 19, 2016 at 1:47 pm #

    n Step 7, I execute python and cv2.__version__ and the version returns 2.4.12. How do I update OpenCV to 3.1.0-dev?

    • Adrian Rosebrock December 21, 2016 at 10:31 am #

      It sounds like you already have OpenCV 2.4.12 installed on your system. How did you previously install OpenCV 2.4? Via Homebrew? Or from source?

      • Visaal A. January 5, 2017 at 6:30 pm #

        I have the same problem, it says the version I installed is 2.4.12. The only thing I did differently was not include the –with-python3 when installing opencv3, but I don’t think that would cause this weird error. I did previously try to install opencv from the source on your other guide but ran into the QTkit error.

        • Adrian Rosebrock January 7, 2017 at 9:34 am #

          Did you have OpenCV installed on your system prior to running the steps in this tutorial?

  4. Steve December 19, 2016 at 8:07 pm #

    Thanks, this was very helpful!

    FWIW: I didn’t have to rename the python 3.5 opencv shared object file in order to successfully import cv2 in python3.5 and I didn’t need to create the .pth file either. Leaving the versioned file name alone the import still succeeded. Unless I need a more involved test?

    • Adrian Rosebrock December 21, 2016 at 10:29 am #

      Thanks for sharing your experience Steve, congrats on getting OpenCV installed!

  5. Guilherme December 23, 2016 at 10:01 am #

    Hi Adrian,

    Have you tried conda? conda is a cross-platform package manager (OSX, Windows, Linux).
    It provides a precompiled opencv package. I never tried this package specifically, but I highly recommend conda when working with compiled libraries.

    After installing conda, it should be just a matter of running:


    conda create -n cvenv opencv
    source activate cvenv

    • Adrian Rosebrock December 23, 2016 at 10:46 am #

      I’ve used conda in the past and found it reasonable. I might still use it today, but the parent company (Continuum) responsible for conda had a very large falling out over a business deal when I was creating the PyImageSearch Gurus course. It’s a great tool, but I’d caution you regarding their OpenCV support — they have removed it in the past and have only recently added it back in over the past couple of years.

      • Guilherme December 26, 2016 at 5:21 pm #

        Ok, I see, but I think it is getting more stable now and they are getting more open. And meanwhile conda-forge was created: https://conda-forge.github.io/
        It is a community-driven initiative to provide conda packages and it has almost all packages provided by Continuum, so it is probably less risky now.

  6. Guilherme December 23, 2016 at 10:09 am #

    Missing reference: http://conda.pydata.org/miniconda.html
    miniconda is a minimal conda installer

  7. JBeale January 1, 2017 at 11:08 am #

    Anyone try to download https://codeload.github.com/opencv/opencv/zip/3.2.0 recently? Just timing out for me (Jan.1 2017) despite status.github.com saying all systems running.

    • Adrian Rosebrock January 4, 2017 at 11:07 am #

      The link seems to be working with me.

  8. Tori January 5, 2017 at 2:27 pm #

    This was a breeze! I’ve spent *days upon days* trying to get OpenCV installed before finding this tutorial. Installing this way took me about an hour. Amazing.

    Note that the latest version of Python is now 3.6 so if you’re copy-pasting Adrian’s code, you might need to replace any instances of 3.5 with 3.6.

    • Adrian Rosebrock January 7, 2017 at 9:36 am #

      Congrats on getting OpenCV installed Tori, I know how much of a struggle it was based on our email chats! I hope you can now continue with your research 🙂

  9. Yasu January 18, 2017 at 9:35 am #

    Thank you so much for this tutorial!! Still don’t get why installing OpenCV is so hard compared to other modules… :p
    Articles like this really helps us noobs actually get into the environment.

    I think there were some updates to the installation of OpenCV, and though it may just be an issue with me, I’ll note the changes I had to make, in case it may help someone…

    As Tori has already said, I had to change all the 3.5 for 3.6 (and for “Handling the Python 3 issue” in step 4, cv2.cpython-35m-darwin.so became cv2.cpython-36m-darwin.so), to accommodate Python 3.6.

    Also, “the little note” that came when I installed OpenCV via Homebrew, told me to run
    $ echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
    $ mkdir -p /Users/yasunori/Library/Python/2.7/lib/python/site-packages
    $ echo ‘import site; site.addsitedir(“/usr/local/lib/python2.7/site-packages”)’ >> /Users/yasunori/Library/Python/2.7/lib/python/site-packages/homebrew.pth

    whereas in this tutorial, only the first line was mentioned.
    This fixes finding the binding for Python 2.7, and for 3.6, I ran the same 3 lines except with 3.6 in the place of 2.7 .

    • Adrian Rosebrock January 18, 2017 at 12:19 pm #

      Thanks for sharing Yasu!

  10. David Skidmore January 21, 2017 at 12:35 pm #

    I could not get the Homebrew version of Python to work. Then I found out about brew doctor.

    • T January 22, 2017 at 10:11 pm #

      Can you explain how you got yours to work? I’m struggling here, installing on a MacBook Pro on OSierra. I keep getting an ImportError. What did you do with brew doctor?

  11. hua January 22, 2017 at 11:00 am #

    Thank you so much for this tutorial also. And a small reminder to the people in China, want to install successfully, need to cross GFW…

  12. Andy February 1, 2017 at 9:03 am #

    Hey Adrian,
    Wonderful installation walkthrough!
    The only problem is that I seem to be getting an error when I’m “solving the python3 issue”
    In my terminal I write: “ls -l /usr/local/opt/opencv3/lib/python3.5/site-packages/” like you said, but my output is saying “no such file or directory”.
    Did I do something wrong? I’m pretty sure that I followed your instructions to the letter and when I did “ls” for python (not python3) it worked…

    Thanks!

    • Adrian Rosebrock February 1, 2017 at 12:45 pm #

      Hey Andy — are you sure you’re using Python 3.5? You might be using Python 3.6. Another suggestion is to check whether the cv2.so file is in the site-packages or the dist-packages.

      • SoPhi February 7, 2017 at 2:09 pm #

        Hey Adrian,
        Thanks for the tutorial! It worked perfectly for Python2.7 and also for Python 3.5.
        But I encountered the same problem as Andy with Python3.6, no *.so file in site-packages directory. It seems coming from 3.6 since with 3.5 it worked. Any idea where it may come from?

        • Adrian Rosebrock February 10, 2017 at 2:16 pm #

          I haven’t tried with Python 3.6, so I’m not sure what the exact problem is. I would check the CMake logs created by Homebrew and then see if the path to the cv2.so file is listed there.

        • Quang February 15, 2017 at 11:47 pm #

          If you coming from 3.5, you may need to reinstall opencv3 with homebrew, to get it work with python 3.6. I don’t have a concrete proof about how opencv3 is brewed, but it seems to me that it looks for the current version of python and creates the “site-package” for that.

          So, when you upgrade your python from 3.5 to 3.6, I don’t think you can expect to have “/usr/local/opt/opencv3/lib/python3.6” created for you. But you can just link the *.so file in “/usr/local/opt/opent3/lib/python3.5/site-package”.

          For what it worth, I can confirm that brew install opencv3 works for my python 3.6.

    • Sundar February 19, 2017 at 2:02 am #

      I had the same issue “No such file or directory” while performing the “Handling the python3 issue” steps. Like Adrian pointed out below, I saw that I was using python 3.6, so I changed all the paths to python3.6 instead of python3.5:
      $ cd /usr/local/opt/opencv3/lib/python3.6/site-packages/
      $ mv cv2.cpython-36m-darwin.so cv2.so
      $ cd ~

      echo /usr/local/opt/opencv3/lib/python3.6/site-packages >> /usr/local/lib/python3.6/site-packages/opencv3.pth

      I was able to complete the install without any errors.

      • Adrian Rosebrock February 20, 2017 at 7:48 am #

        Thanks for sharing Sunday!

    • Justin April 20, 2017 at 12:22 am #

      Problem and solution found here:
      http://stackoverflow.com/questions/43113151/error-in-installing-opencv3-with-homebrew-and-python3

  13. Andres Alberto Ramirez Duque February 23, 2017 at 12:29 pm #

    I have the same issue “No such file or directory” “/usr/local/opt/opencv3/lib/python3.6”, any solution?

    MacOS Sierra Python3.6 Opencv3.2.0

    • Guillermo Duenas Arana February 28, 2017 at 6:42 pm #

      Same here, I installed it –with-python3, but the only directory in “/usr/local/opt/opencv3/lib/” is python2.7.

      • Adrian Rosebrock March 2, 2017 at 6:56 am #

        Just to clarify, did you use Homebrew to install Python 3? And if so, is the python3 command pointing to your Homebrew installation?

        • Claudio Fritsche March 13, 2017 at 6:12 pm #

          I’ve got the same problem, in /usr/local/opt/opencv3/lib there is only python2.7, but no python3.6.
          While following the tutorial I did type “which python3” which gave me “/usr/local/bin/python3” so it seems to point to the Homebrew installation.

          Do you have any idea how to tackle this problem?

          • Zeyu Zhang March 17, 2017 at 12:56 am #

            Same problem happened to me. It bothered my nearly two days, and finally I figured out what is going wrong here, but I cannot solve it.

            Enter following command in your terminal:

            brew gist-logs opencv3

            then you will get I link to github, and find out the whole installation log. In the log, you will find out following lines (may not exactly the same, but similar):

            — Found PythonInterp: /usr/local/bin/python3.4 (found suitable version “3.4.4”, minimum required is “3.4”)
            — Could NOT find PythonLibs: Found unsuitable version “3.6.0”, but required is exact version “3.4.4” (found /usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin/libpython3.6.dylib)

            Here is the problem, since opencv3 cannot find a suitable PythonLibs, therefore it does not install the opencv3 for python3 (e.g. the python3.6 folder in /usr/local/opt/opencv3/lib).

            How to solve this problem? Here is my “solution”,
            remove python3 and start using python2 :(.

  14. Emmanuel Ovalle February 25, 2017 at 3:39 pm #

    Thanks for the tutorial Adrian. !!

    I followed the steps, and i can run opencv in python, but i cannot run an opencv example with c++ and qt-creator. Any solutions ?

    Thanks.

  15. Sam H March 5, 2017 at 6:50 am #

    Without doubt the easiest to follow tutorial i’ve found thus far – thank you (as I mentioned on Twitter)

    However, I still encounter a couple of errors as I go through – including the 3.5/3.6 ones mentioned above.

    So, I went back to the beginning and noticed that $ which python gives me: /usr/bin/python

    But $ which python3 gives me: /usr/local/bin/python3

    I’m fairly sure this is probably the root of the rest of the errors, but I’ve not idea why it would be like this?

    • Adrian Rosebrock March 6, 2017 at 3:44 pm #

      Hey Sam — it’s great to hear that you’re enjoying the tutorial.

      Regarding your error, it sounds like your Python 3 path is updated, but not Python 2.7. Have you updated your .bash_profile file? And have you installed both Python 3 and Python 2.7 via Homebrew?

  16. Claudio Fritsche March 13, 2017 at 6:14 pm #

    I followed the whole tutorial, everything worked out, the only problem I’ve got was the binding to Python 3.6.

    If I open the python shell with IDLE, everything works fine. But if I open Xcode and try to import the cv2 (for c++) it doesn’t work at all, do you have any idea how to make it work there as well?

    Thank you very much for the tutorial! It was very helpful

    • Adrian Rosebrock March 15, 2017 at 9:04 am #

      I’m not sure what you are trying to accomplish here. You’re trying to import Python OpenCV bindings into a C++ program? Or you’re trying to include the OpenCV header files in a C++ program?

    • Zeyu Zhang March 17, 2017 at 12:44 am #

      When you tried to compile your C++ source files, you have to tell the compiler where is the Opencv header file (.h or .hpp), so that the compiler can find the header file.

      You can add follow command when you compile your c++-opencv source file:

      -I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include

      Note: “/usr/local/Cellar/opencv3/3.2.0/include/opencv” and “/usr/local/Cellar/opencv3/3.2.0/include” is the path where my opencv header files stored, you may have different paths.

      Then during, the linking process, you need to tell the compiler where is the .lib file, so that them could be linked to your final executable file.

      Add following command to achieve this goal:

      -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dpm -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core

      Note: “/usr/local/Cellar/opencv3/3.2.0/lib” is the path where my opencv lib files stored, you may have a different path, just change it to yours.

  17. Arthur March 24, 2017 at 6:23 pm #

    Hi Adrian

    this instruction is awsome! I installed opencv3 successfully on my mac sierra system. However now I may want to add ffmpeg surpport to my opencv3.
    I noticed that actually there is a –with–ffmpeg option for brew install. However I followed exactly your commands and did not add this option at that time.
    Now how can I add ffmpeg support without uninstalling and installing opencv3 again ?

    • Adrian Rosebrock March 25, 2017 at 9:17 am #

      If you want to add FFMPEG support you will have to uninstall OpenCV and install it with the FFMPEG flag.

      • Arthur March 28, 2017 at 12:52 am #

        I brew install ffmpeg and I uninstall opencv and re-install it with ffmpeg flag. However today when i was trying to read a mp4 file with videocapture() it always returned False, which made me doubt whether I added ffmpeg successfully.

        If I installed opencv with ffmpeg support correctly, where and which file ( kind like opencv-ffmpeg.h ?) should I see ?

        • Adrian Rosebrock March 28, 2017 at 12:54 pm #

          Instead of trying to enable FFMPEG from Homebrew, try compiling OpenCV from source.

  18. VVZ3n March 26, 2017 at 7:00 pm #

    Thank you so much for writing this tutorial! Awesome informations!

    • Adrian Rosebrock March 28, 2017 at 1:04 pm #

      Thank you!

  19. Mateusz March 27, 2017 at 2:23 pm #

    Hi Adrian,

    I’ve followed your instructions up to the point of actually installing OpenCV3 (brew install…)

    At the end of the process I get the “missing Python.h file” error. Any ideas?

    Thanks!

    • Adrian Rosebrock March 28, 2017 at 12:56 pm #

      Have you installed the Homebrew version of Python? Also, which version of Python are you using?

      • Mateusz March 29, 2017 at 3:17 am #

        Adrian, thanks for the reply. Seems to be solved now, I exported a $CPATH pointing to the missing Headers folder. I think it’s working now, however, which is funny, python imports cv2, while pyhon3 imports cv3. Haven’t played with virtualenvs yet, though.

  20. Neil Slater March 29, 2017 at 5:00 pm #

    Thanks for covering this tricky install so thoroughly. My Python (2&3) and OpenCV installs were damaged – I think by XCode update or other auto upgrade in Mac OS. I had to re-install Python3 from scratch and re-do all the libraries I had from before. Following this tutorial worked for me first time.

    • Adrian Rosebrock March 31, 2017 at 1:57 pm #

      Congrats on getting OpenCV installed, Neil! In general I would recommend turning off the macOS auto upgrade feature. Major OS upgrades almost always break sym-links in your development environment which is likely what happened here.

  21. Williams March 30, 2017 at 3:09 am #

    I got a error in installing opencv3 step:
    ==> Installing opencv3 from homebrew/science
    Error: You must brew link cmake before homebrew/science/opencv3 can be installed
    Could you help with it?

    • Adrian Rosebrock March 31, 2017 at 1:53 pm #

      Do what the message says:

      $ brew link cmake

      And try continuing.

  22. haffj March 30, 2017 at 4:02 pm #

    Hi,

    Thank you for the tutorial!
    I correctly installed opencv3 in this way. However I wish to install the version 3.1.0 of opencv, and by following the tutorial I’m only able to get the latest 3.2 version.

    I have searched a lot for a way to do this, but couldn’t find something.

    Is it possible to choose the version we want to install?

    • Adrian Rosebrock March 31, 2017 at 1:51 pm #

      Using the automated Homebrew method I don’t think you can install specific versions of OpenCV. If you need a specific OpenCV version I would suggest compiling from source.

      • haffj April 3, 2017 at 9:44 am #

        Thank you for the reply i’ll try from source.

        I wish to try another version of opencv because i get trouble with using opencv 3.2.0 on my mac Sierra 10.12.3. The problem is that i want to read a video with python 3.6 and opencv 3.2.0, but it is very slow. I tried to install opencv with many options but still i get the same problem. (i made the install –with-cuda –with-contrib –with-ffmpeg –with-python3 –with-qt5 –with-tbb –without-opencl)

        Moreover i get a normal video speed with exactly the same script on another computer with macOs El Capitan 10.11.6 and with opencv 3.1.0_3 and python 3.5.1. So i think the slow speed may be caused by the version of opencv.

        Did you already have such a problem?

        • Adrian Rosebrock April 3, 2017 at 1:51 pm #

          It’s hard to say without having physical access to your system. When it comes to processing video, I would recommend Ubuntu slightly more as you’ll have better control over the package management. Other than that, I’m not sure what the exact issue would be.

  23. Ameya April 1, 2017 at 3:44 pm #

    Great tutorial!
    However, I have a quick question. When I type which python3, I get the correct directory. With which python, I only get /usr/bin/python.

    I have installed both python 2.7 and 3, and my .bash profile is updated.
    Any ideas as to why this is happening?

    • Adrian Rosebrock April 3, 2017 at 2:06 pm #

      Hmm, that is indeed very strange. Make sure your source your .bash_profile. Try re-installing Python 2.7 via Homebrew and checking for any errors.

      • Louis April 14, 2017 at 6:46 am #

        Hi Adrian, thanks for tutorial. I am having the same issue as Ameya and have uninstalled/reinstalled/updated .bashprofile/sourced it, and repeated the process. Any further suggestions?

        • Adrian Rosebrock April 16, 2017 at 8:57 am #

          Without physical access to your machine, unfortunately I’m not sure what the exact problem is.

  24. Ln Sriram April 6, 2017 at 8:34 am #

    Hello,

    Many thanks for this tutorial. Really helped me. I am new to OpenCV.

    I have the source code to open an image in a window and closing it when any key is pressed.

    I compiled the program like this:
    g++ pkg-config --libs --cflags opencv -o display_image display_image.cpp

    Is there an easier way to compile the code? It is very tedious to have to type this every time.

    Thanks in advance!

    • Adrian Rosebrock April 8, 2017 at 12:57 pm #

      Keep in mind that this is a Python blog, not a C++ blog. In any case, you should look into creating Makefiles.

  25. Peeraya S April 7, 2017 at 8:53 am #

    Dear Adrian

    Thank you very much for the tutorial. I have been trying to search for comment with the same problem but I cannot find so I decided to post here to ask.

    I have previously installed OpenCV 2.4.12 in my OSX with brew and python 2 and 3 several months ago.
    Today I have installed OpenCV 3 following your homebrew tutorial and everything seems fine until I tried

    cv2.__version__
    ‘2.4.12’

    Any ideas?

    • Peeraya S April 7, 2017 at 9:24 am #

      I have tried the following and it worked!

      brew uninstall opencv

      Thank you for your time 🙂

      • Adrian Rosebrock April 8, 2017 at 12:48 pm #

        Glad to hear it Peeraya! 🙂

  26. Nidhi April 11, 2017 at 2:58 am #

    My terminal shows me this error while Installing OpenCV 3 with Python 3 bindings via Homebrew. What do i do?

    ==> Checking out branch master
    Error: No such file or directory – /private/tmp/opencv3-20170411-5809-hafb4d/3rdparty/ippicv/downloader.cmake

    • Adrian Rosebrock April 12, 2017 at 1:10 pm #

      I’ve seen a few readers encounter this issue. Unfortunately, I’m not sure what the exact problem is as it seems to be related to Homebrew. I would suggest trying to install OpenCV from source.

    • Creekee April 16, 2017 at 7:31 pm #

      I got the same problem when installing opencv3. Then I solve the problem like this: go to the official website and download the zip code source, unzip it and you can get downloader.cmake file in 3rdparty, then you put it into the same file in opencv–git. After that try again and it will work.

  27. Creekee April 16, 2017 at 8:00 am #

    Thanks very much for your very helpful tutorial. But I still get some problem.
    First, when I follow your instruction at step4, after cloning the file from github, I got an error with a prompt: the ‘downloader.cmake’ file in 3rdparty is missing. I checked it out and found a problem. The code post on Github does not include ‘downloader.cmake’ while when I download the zip code, I found it was there. So I add that file manually and succeeded finally.
    Second, after I completing the installation of opencv, when I call the formula cv2.VideoCapture(path), it failed to open the video file. Now I don’t how to solve the problem out. Additionally, the video file I was trying to open is .mkv file. Can you give me some advice?
    Thank you for your reply.

    • Adrian Rosebrock April 16, 2017 at 8:48 am #

      This seems to be an issue with the latest version of Homebrew definition files that many readers are encountering. Try running brew update to refresh your definitions. Otherwise, you might need to install OpenCV via source.

      As for opening a .mkv file, it sounds like you may have installed OpenCV without MKV codec support.

      • Creekee April 16, 2017 at 7:32 pm #

        So how could I install it with MKV codec support?

        • Adrian Rosebrock April 19, 2017 at 1:03 pm #

          You would need to install the MKV drivers then re-install OpenCV. I haven’t tried to compile OpenCV support with MKV support before, so I unfortunately cannot provide any more guidance than this.

  28. sumi April 20, 2017 at 12:43 pm #

    Thank you for sharing the Tutorial. But when I use command ” brew install opencv3 –with-contrib –with-python3 –HEAD” to install Opencv3, there is a error

    Error ” No such file or directory – /private/tmp/opencv3-20170421-20765-4mxj69/3rdparty/ippicv/downloader.cmake ”

    What can I do to deal with it.

    • Adrian Rosebrock April 21, 2017 at 10:54 am #

      Please read the comments before posting. “Nidhi” mentioned this error above and “Creekee” provided the solution.

  29. Siddharth Krishan April 20, 2017 at 11:29 pm #

    Hi Adrian,
    in the phase when I use a
    $ ls -l /usr/local/opt/opencv3/lib/python3.5/site-packages/

    I dont see any packages for python3.6. How can I include it???

  30. Siddharth Krishan April 21, 2017 at 4:22 am #

    Hi I have python 3.6 installed without homebrew, but I followed rest of the case. But i do not have a python 3.x folder as mentioned in the Handling the python3 issue. I have tried mutliple things but they are not working.

    • Adrian Rosebrock April 21, 2017 at 10:43 am #

      This blog post assumes you are using Homebrew and the Homebrew paths to Python 3.6. I would still suggest that you install Python via Homebrew to ensure it’s separate from your system install.

      • Siddharth Krishan April 21, 2017 at 1:15 pm #

        When I do so, it throws me this warning
        “python3-3.6.1 already installed, it’s just not linked.” What does this mean??

        • Siddharth Krishan April 21, 2017 at 1:25 pm #

          and when using ‘which python3’ it shows /usr/local/bin/python3.

        • Adrian Rosebrock April 24, 2017 at 9:56 am #

          Try running:

          $ brew linkapps python3

          And see if that resolves the issue.

  31. Gilles M. April 21, 2017 at 7:14 pm #

    Hello Adrian,

    Thanks for your post ! Only with your tutorial did I find a way to make it work.

    However, it only does when I am in the /usr/local/opt/opencv3/lib/python2.7 folder and launch python from there. Then it works and I can use cv2 package (check import and version ok).

    However, when I try to launch python from my root folder (or any others), then it doesn’t work. I can launch python but I get the typical answer ImportError: No module name cv2

    That is an issue as it prevents from using my favorite editors (Rodeo or Pycharm) as they also return the same error message.

    Would you have some advice on how it could make it work from any folder and my editors?

    Many thanks,
    -Gilles

    • Adrian Rosebrock April 24, 2017 at 9:51 am #

      Hey Gilles — are you using Python from a virtual environment like I recommend in this tutorial? Or did you skip the virtual environment step?

  32. Laura April 21, 2017 at 7:55 pm #

    hey, I need help

    I did everything as the tuturial says, got the same messages on the terminal after installing opencv, renamed the file to cv2.so and everything, but opencv only works under python and not under python3

    Thanks

    • Laura April 21, 2017 at 8:16 pm #

      I found out what it was. ‘brew doctor’ showed python3 was unlinked, I just followed brew doctor instructions and it worked fine.

      Thanks for this tutorial 🙂

      • Adrian Rosebrock April 24, 2017 at 9:50 am #

        Congrats on resolving the error Laura! And thank you for sharing the solution as well.

  33. Cory April 22, 2017 at 2:20 pm #

    Three things:

    1. This was super helpful, thank you so much!
    2.Your recommendation to compile from source, and use Homebrew as a fallback- that’s at the end! If I had read that first, I would have went with your recommendation, but in the beginning it sounded like this was the easier route.
    3. Everything “worked” except for the very last part: testing the opencv install in the virtual environment. When I type “import cv2” I get the error message: “ModuleNotFoundError: No module named ‘cv2′”, which is odd. It works outside the virtual environment, and when I redo the step to sym-link, I get feedback: “ln: cv2.so: File exists” . I looked around stackoverflow / github, but did not see a proper solution.

    Thanks again for everything!

    • Adrian Rosebrock April 24, 2017 at 9:38 am #

      Hi Cory, thanks for the feedback. You’re right, I probably should have recommended compiling from source at the top of the post.

      As far as the sym-link existing, you can delete it using rm just like any other file. Double-check your path to the cv2.so file before you create the sym-link — that is likely where the error is.

  34. Andreas April 23, 2017 at 2:16 pm #

    Hi!

    When trying to install using the syntax

    brew install opencv3 –with-contrib –with-python3 –HEAD

    I get the following error:

    “Error: opencv3: Does not support building both Python 2 and 3 wrappers”

    Does it mean that OpenCV doesn’t support wrappers for both Python verions?

    Do I have to run it with the extra syntax –without-python to make it work and thereby sacrifice the Python 2 support?

    I’m quite a newbie within this field so any suggestions would be appreciated!

    Thanks!

    • Adrian Rosebrock April 24, 2017 at 9:35 am #

      Hi Andreas — I would make sure you run brew update to make sure the latest install definitions are pulled down. Homebrew should not have an issue compiling and installing OpenCV with Python bindings.

      • Leon April 25, 2017 at 8:36 am #

        Hi Adrian, I’m facing the same problem as Andreas. I followed your advice by running brew update and I got “Already up-to-date.”

        I then tried to run brew install opencv3 –with-contrib –with-python3 –HEAD and I got the error again, really not sure what I can do to resolve the problem.

        Any advice or help will be greatly appreciated!

        • Adrian Rosebrock April 25, 2017 at 12:00 pm #

          I’m curious if there is an improper sym-link somewhere. Can you remove the previous Python sym-link and try again?

          I would also suggest reading the comment thread started by “Nidhi” above as it also addresses the same issue.

          Based on the two threads, it seems that there was an update to the actual Homebrew formula that’s causing the error.

          • Adrian Rosebrock April 25, 2017 at 12:06 pm #

            UPDATE:

            The solution to the problem can be found here. If you want to install with --HEAD, then you’ll need to update the Homebrew formula.

            Otherwise, just omit --HEAD

            $ brew install opencv3 --with-contrib --with-python3

      • Sasha Naidoo April 26, 2017 at 5:23 am #

        Hi Adrian,

        I have the same issue as Andreas and my homebrew is up-to-date. Any other suggestions?

        • Sasha Naidoo April 26, 2017 at 5:37 am #

          Hi Adrian,

          I tried the solutions mentioned above for installing with –HEAD and also tried without, I still get the same error?

          • Adrian Rosebrock April 26, 2017 at 6:48 am #

            It sounds like the issue is with the formula itself then. Please see the StackOverflow thread I linked to.

            Basically, you’ll want to edit the formula:

            $ brew edit opencv3

            And then comment out the lines:

            From there you should be able to compile with --HEAD

            Please give that a try and let us know if it works.

          • Patrick April 28, 2017 at 3:25 am #

            Replying to Adrian, I tried commenting out those lines as well as with or without the –HEAD option. Looking more at the opencv3.rb file, there’s a pretty plain and simple IF/ELSE block to me:

            if build.with?(“python3”) && build.with?(“python”)
            # Opencv3 Does not support building both Python 2 and 3 versions
            odie “opencv3: Does not support building both Python 2 and 3 wrappers”
            end

            I even did try compiling having this built-in block commented out and it “worked” as in no error, but only built the library path for the 2.7 python and not python3 (under the opencv lib path it makes the python2.7 folder, not the python3.x

            So my thought, is there a way to not simultaneously install opencv for both python 2x and 3x at same time, and force it to only do 3x (–without-python seems to skip python all together)? Because it almost seems as if it’s now built to only handle one at a time.

          • Adrian Rosebrock April 28, 2017 at 9:23 am #

            Hey Patrick — thanks for the comment. Homebrew would need to do two separate compiles of OpenCV for Python 2.7 and Python 3 as they are two entirely different Python versions and will thus bind with OpenCV differently. As the current Homebrew formula stands, it looks like you can compile Python 2.7 bindings, but there is a bug with the Python 3 bindings. As I mentioned to “Daniel”, I’ll have to look into this further as the Homebrew formula has clearly changed.

  35. Daniel S April 28, 2017 at 4:07 am #

    Unfortunately, I’m having the same issue.

    Error: opencv3: Does not support building both Python 2 and 3 wrappers

    I tried editing the formula as suggested. I’ve also tried without the contrib and head params but the failure persists on macOS Sierra.

    Any other suggestions?

    • Adrian Rosebrock April 28, 2017 at 9:17 am #

      Unfortunately, I don’t have any other suggestions at this time. I will try to play around with installing OpenCV via Homebrew on a local machine, but it unfortunately seems it an issue with the actual OpenCV + Homebrew formula. Until they update the formula, the problem will persist.

Leave a Reply