macOS: Install OpenCV 3 and Python 2.7

sierra_os_hog_example

I’ll admit it: Compiling and installing OpenCV 3 on macOS Sierra was a lot more of a challenge than I thought it would be, even for someone who has a compiled OpenCV on hundreds of machines over his lifetime.

If you’ve tried to use one of my previous tutorials on installing OpenCV on your freshly updated Mac (Sierra or greater) you likely ran into a few errors, specifically with the QTKit.h  header files.

And even if you were able to resolve the QTKit problem, you likely ran into more issues trying to get your CMake command configured just right.

In order to help resolve any issues, problems, or confusion when installing OpenCV with Python bindings on macOS Sierra (or greater) I’ve decided to create two hyper-detailed tutorials:

  1. This first tutorial covers how to install OpenCV 3 with Python 2.7 bindings on macOS.
  2. My second tutorial will come next week where I’ll demonstrate how to install OpenCV 3 with Python 3.5 bindings on macOS.

I decided to break these tutorials into two separate blog posts because they are quite lengthy.

Furthermore, tuning your CMake command to get it exactly right can be a bit of a challenge, especially if you’re new to compiling from OpenCV from source, so I wanted to take the time to devise a foolproof method to help readers get OpenCV installed on macOS.

To learn how to install OpenCV with Python 2.7 bindings on your macOS system, keep reading.

macOS: Install OpenCV 3 and Python 2.7

The first part of this blog post details why I am creating a new tutorial for installing OpenCV 3 with Python bindings on the Mac Operating System. In particular, I explain a common error you may have run across — the QTKit.h  header issue from the now deprecated QTKit library.

From there, I provide super detailed instructions on how to install OpenCV 3 + Python 2.7 your macOS Sierra system or greater.

Avoiding the QTKit/QTKit.h file not found error

In the Mac OSX environment the QTKit (QuickTime Kit) Objective-C framework is used for manipulating, reading, and writing media. In OSX version 10.9 (Mavericks) QTKit was deprecated (source).

However, it wasn’t until the release of macOS Sierra that much of QTKit was removed and instead replaced with AVFoundation, the successor to QTKit. AVFoundation is the new framework for working with audiovisual media in iOS and macOS.

This created a big problem when compiling OpenCV on Mac systems — the QTKit headers were not found on the system and were expected to exist.

Thus, if you tried to compile OpenCV on your Mac using my previous tutorials your compile likely bombed out and you ended up with an error message similar to this:

Even more problematic, both the tagged releases of OpenCV v3.0 and v3.1 do not include fixes to this issue.

That said, the latest commits to the OpenCV GitHub repo do address this issue; however, a new tagged release of v3.2 has yet to be released.

That said, I’m happy to report that by using the latest commit to OpenCV’s GitHub we can install OpenCV on macOS Sierra and greater.

The trick is that we need to use the HEAD  of the repo as opposed to a tagged release.

Once OpenCV 3.2 is released I’m sure the QKit to AVFoundation migration will be included, but until then, if you want to install OpenCV 3 on your macOS system running Sierra or later, you’ll need to avoid using tagged releases and instead compile and install the development version of OpenCV 3.

How do I check my Mac Operating System version?

To check your Mac OS version click the Apple icon at the very top-left corner of your screen in the menu then select “About this Mac”.

A window should then pop up, similar to the one below:

Figure 1: Checking your OS version on Mac. My machine is currently running macOS Sierra (10.12).

Figure 1: Checking your OS version on Mac. My machine is currently running macOS Sierra (10.12).

If you are running macOS Sierra or greater, you can use this tutorial to help you install OpenCV 3 with Python 2.7 bindings.

If you are using an older version of the Mac Operating System (Mavericks, Yosemite, etc.), please refer to my previous tutorials.

Step #1: Install Xcode

Before we can even think about compiling OpenCV, we first need to install Xcode, a full blown set of software development tools for the Mac Operating System.

Register for an Apple Developer account

Before downloading Xcode you’ll want to register with the Apple Developer Program (it’s free). If you have an existing Apple ID (i.e., what you use to sign in to iTunes with) this is even easier. Simply provide some basic information such as name, address, etc. and you’ll be all set.

From there, the easiest way to download Xcode is via the App Store. Search for “Xcode” in the search bar, select it, and then click the “Get” button:

Figure 2: Selecting Xcode from the Apple App Store.

Figure 2: Selecting Xcode from the Apple App Store.

Xcode will then start to download and install. On my machine the download and install process took approximately 30 minutes.

Accept the Apple Developer license

Assuming this is the first time you’ve installed or used Xcode, you’ll need to accept the developer license (otherwise, you can skip this step). I prefer using the terminal whenever possible. You can use the following command to accept the Apple Developer License:

Scroll to the bottom of the license and accept it.

Install Apple Command Line Tools

Finally, we need to install the command line tools. These tools include packages such as make, GCC, clang, etc. This is absolutely a required step, so make sure you install the command line tools:

After you enter the command above a window will pop up confirming that you want to install the command line tools:

Figure 3: Installing the Apple Command Line Tools on macOS.

Figure 3: Installing the Apple Command Line Tools on macOS.

Click “Install” and the Apple Command Line Tools will be downloaded and installed on your system. This should take less than 5 minutes.

Step #2: Install Homebrew

We are now ready to install Homebrew, a package manager for macOS. Think of Homebrew as similar equivalent to apt-get for Ubuntu and Debian-based systems.

Installing Homebrew is simple. Simply copy and paste the command underneath the “Install Homebrew” section of the Homebrew website (make sure you copy and paste the entire command) into your terminal:

Once Homebrew is installed you should update it to ensure the most recent package definitions are downloaded:

The last step is to update our ~/.bash_profile  file. This file may exist on your system already or it may not. In either case, open it with your favorite text editor (I’ll use nano  in this case):

And insert the following lines at the bottom of the file (if ~/.bash_profile  does not exist the file will be empty, so simply insert the following lines):

The above snippet updates your PATH  variable to look for binaries/libraries along the Homebrew path before searching your system path.

After updating the file save and exit the editor. I have included a screenshot of my ~/.bash_profile  below:

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

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

You should then use the source  command to ensure the changes to your ~/.bash_profile  file are manually reloaded:

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

Step #3: Setup Homebrew for Python 2.7 and macOS

In general, you do not want to develop against the system Python as your main interpreter. This is considered bad form. The system version of Python should (in an ideal world) serve only one purpose — support system operations.

Instead, you’ll want to install your own version of Python that is independent of the system one. Installing Python via Homebrew is dead simple:

Note: This tutorial covers how to install OpenCV 3 with Python 2.7 bindings on macOS. Next week I’ll be covering OpenCV 3 with Python 3 bindings — if you want to use Python 3 with OpenCV on macOS, please refer to next week’s blog post.

After the install command finishes we just need to run the following command to complete the Python installation:

To confirm that we are using the Homebrew version of Python rather than the system version of Python you should use the which  command:

Important: Be sure to inspect the output of the which  command! If you see /usr/local/bin/python  then you are correctly using the Hombrew version of Python.

However, if the output is /usr/bin/python  then you are incorrectly using the system version of Python. If this is the case then you should ensure:

  1. Homebrew installed without error.
  2. The brew install python  command completed successfully.
  3. You have properly updated your ~/.bash_profile  file and reloaded the changes using source . This basically boils down to making sure your ~/.bash_profile  looks like mine above in Figure 4.

Step #4: Install virtualenv, virtualenvwrapper, and NumPy

We are now ready to install three Python packages: virtualenv and virtualenvwrapper, along with NumPy, used for numerical processing.

Installing virtualenv and virtualenvwrapper

The virtualenv  and virtualenvwrapper  packages allow us to create separate, independent Python environments for each project we are working on. I’ve mentioned Python virtual environments many times before, 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. There is also an excellent tutorial on the RealPython.com blog that takes a deeper dive into Python virtual environments.

To install virtualenv  and virtualenvwrapper , just use pip :

After these packages have been installed we once again need to update our ~/.bash_profile  file:

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

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

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

Save and exit your text editor, followed by refreshing your environment using the source  command:

Again, this command only needs to be executed once. Whenever you open up a new terminal the contents of your .bash_profile  file will be automatically loaded for you.

Creating your Python virtual environment

Assuming the above commands completed without error, we can now use the mkvirtualenv  command to create our Python virtual environment. We’ll name this Python virtual environent  cv :

This command will create a Python environment that is independent from all other Python environments on the system (meaning this environment has its own separate site-packages  directory, etc.). This is the virtual environment we will be using when compiling and installing OpenCV.

The mkvirtualenv  command only needs to be executed once. If you ever need to access this virtual environment again, just use the workon  command:

To validate that you are in the cv  virtual environment, simply examine your command line — if you see the text (cv)  preceding the prompt, then you are in the cv  virtual environment:

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

Figure 6: 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 7: 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 7: 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.

To access the cv  virtual environment simply use the workon  command mentioned above.

Install NumPy

The last step is to install NumPy, a scientific computing package for Python.

Ensure you are in the cv  virtual environment (otherwise NumPy will be installed into the system version of Python rather than the cv  environment) and then install NumPy using pip :

Step #5: Install OpenCV prerequisites using Homebrew

OpenCV requires a number of prerequisites, all of which can be installed easily using Homebrew.

Some of these packages are related to tools used to actually build and compile OpenCV while others are used for image I/O operations (i.e., loading various image file formats such as JPEG, PNG, TIFF, etc.)

To install the required prerequisites for OpenCV on macOS, just execute these commands:

Step #6: Download the OpenCV 3 source from GitHub

As I mentioned at the top of this tutorial, we need to compile OpenCV from the latest commit, not a tagged release. This requires us to download the OpenCV GitHub repo:

Along with the opencv_contrib repo:

Step #7: Configuring OpenCV 3 and Python 2.7 via CMake on macOS

In this section I detail how to configure your OpenCV 3 + Python 2.7 on macOS Sierra build using CMake.

First, I demonstrate how to setup your build by creating the build  directory.

I then provide a CMake build template that you can use. This template requires you to fill in two values — the path to your libpython2.7.dylib  file and the path to your Python.h  headers.

I will help you find and determine the correct values for these two paths.

Finally, I provide an example of a fully completed CMake command. However, please take note that this command is specific to my machine. Your CMake command may be slightly different due to the paths specified. Please read the rest of this section for more details.

Setting up the build

In order to compile OpenCV 3 with Python 2.7 support for macOS we need to first set up the build. This simply amounts to changing directories into opencv  and creating a build  directory:

OpenCV 3 + Python 2.7 CMake template for macOS

In order to make the compile and install process easier, I have constructed the following template OpenCV 3 + Python 2.7 CMake template:

Looking at this template I want to point out a few things to you:

  1. BUILD_opencv_python2=ON : This indicates that we want to build Python 2.7 bindings for our OpenCV 3 install.
  2. BUILD_opencv_python3=OFF : Since we are compiling Python 2.7 bindings we need to explicitly state that we do not want Python 3 bindings. Failure to include these two switches can cause problems in the CMake configuration process.
  3. PYTHON2_LIBRARY=YYY : This is the first value you need to fill in yourself. You will need to replace YYY  with the path to your libpython2.7.dylib  file (I will help you find it in the next section).
  4. PYTHON2_INCLUDE_DIR : This is the second value you will need to fill in. You need to replace ZZZ  with the path to your Python.h  headers (again, I’ll help you determine this path).

Determining your Python 2.7 library and include directory

Let’s start by configuring your PYTHON2_LIBRARY  value. This switch should point to our libpython2.7.dylib  file. You can find this file within many nested subdirectories of /usr/local/Cellar/python/ . To find the exact path to the libpython2.7.dylib  file, just use the ls  command along with the wildcard asterisk:

Take note of the output of this command — this is the full path to your libpython2.7.dylib  file and will replace YYY  in the CMake template above.

Next, let’s determine the PYTHON2_INCLUDE_DIR . This path should point to the Python.h  headers used to generate our actual OpenCV + Python 2.7 bindings.

Again, we’ll use the same ls  and wildcard trick here to determine the proper path:

The output of the ls -d  command is our full path to the Python.h  headers. This value will replace ZZZ  in the CMake template.

Filling in the CMake template

Now that you’ve determined the PYTHON2_LIBRARY  and PYTHON2_INCLUDE_DIR  values you need to update the CMake command with these values.

On my particular machine the full CMake command looks like this:

However, please do not copy and paste my exact CMake command — make sure you have used the instructions above to properly determine your PYTHON2_LIBRARY  and PYTHON2_INCLUDE_DIR  values.

Once you’ve filled in these values execute your cmake  command and your OpenCV 3 + Python 2.7 build will be configured.

As an example, take a look at the Python 2  section of the output from my configuration:

Figure 8: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for macOS.

Figure 8: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for macOS.

You’ll want to make sure that:

  1. The Interpreter  points to the Python binary in your cv  virtual environment.
  2. Libraries  points to your libpython2.7.dylib  file.
  3. The numpy  version being utilized is the one you installed in your cv  virtual environment.

Step #8: Compile and install OpenCV on macOS

Assuming you cmake  command exited without error and your Python 2  section is properly configured, you can now compile OpenCV:

The -j  switch controls the number of parallel processes to compile OpenCV. We normally set this to the number of available cores/processors on our machine. Since I’m on a quad-core system, I use -j4 .

OpenCV can take awhile to compile (30-90 minutes) depending on the speed of your machine. A successful compile will end with a 100% completion:

Figure 9: Successfully compiling OpenCV 3 from source with Python 2.7 bindings on macOS.

Figure 9: Successfully compiling OpenCV 3 from source with Python 2.7 bindings on macOS.

Assuming that OpenCV compiled without error, you can now install it on your macOS system:

Step #9: Sym-link your OpenCV 3 + Python 2.7 bindings

After running make install  you should now see a file named cv2.so  in /usr/local/lib/python2.7/site-packages :

The cv2.so  file is your actual set of OpenCV 3 + Python 2.7 bindings.

However, we need to sym-link these bindings into our cv  virtual environment. This can be accomplished using the following commands:

Step #10: Testing your OpenCV install on macOS

To verify that your OpenCV 3 + Python 2.7 installation on macOS is working:

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

Here are the exact steps to test the install process:

Note: Take note of the -dev  in the cv2.__version__ . This indicates that we are using the development version of OpenCV and not a tagged release. Once OpenCV 3.2 is released these instructions can be updated to simply download a .zip of the tagged version rather than having to clone down the entire repositories. 

I’ve also included a screenshot below that utilizes these same steps. As you can see, I can access my OpenCV 3 bindings from Python 2.7 shell on macOS Sierra:

Figure 10: Ensuring that I can successfully import my OpenCV + Python bindings on macOS.

Figure 10: Ensuring that I can successfully import my OpenCV 3 + Python 2.7 bindings on macOS.

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

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 this blog post I demonstrated how to install OpenCV 3 with Python 2.7 bindings on macOS Sierra and above.

Next week I’ll have a second tutorial, this one covering OpenCV 3 with Python 3.5 bindings on macOS.

For more OpenCV install tutorials on other operating systems (such as Ubuntu, Raspbian, etc.), please refer to this page where I provide additional links and resources.

But before you go

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!

, , , ,

57 Responses to macOS: Install OpenCV 3 and Python 2.7

  1. George Profenza November 28, 2016 at 12:03 pm #

    OMG! Sooooo frustratingly long-winded with Sierra. Still sticking to 10.11.5 for now.
    Thank you for sparing us the headaches Adrian

    • Adrian Rosebrock November 28, 2016 at 2:40 pm #

      It’s a pain, but it’s getting easier. It will get much easier once OpenCV 3.2 is released.

  2. Mark November 28, 2016 at 12:12 pm #

    Worked like a charm!

    • Adrian Rosebrock November 28, 2016 at 2:41 pm #

      Glad to hear it Mark, congrats on getting OpenCV installed on Sierra!

  3. A K November 29, 2016 at 1:48 am #

    Thank you for this great tutorial! This is exactly what I was searching on your blog during the Thanksgiving break ….looks like an early Christmas gift. Looking forward to next week’s Python 3.5 tutorial.

    • Adrian Rosebrock November 29, 2016 at 7:56 am #

      Thank you for the kind words, comments like these make it worth writing these blog posts :-)

  4. Claudiu November 29, 2016 at 10:46 pm #

    I’ve had problems with step #7 for some reason it wouldn’t have the correct output like your screenshots. I found a github issue where other people had that and wanted to mention it that I just had to delete the build directory and run cmake again about 3-4 times until it gave me the right results (if anyone else runs into that problem)

    Besides that everything else worked perfectly, thank you again!

    • Monkey December 7, 2016 at 5:33 am #

      Thanks for the suggestion, I came across the same problem and now fixed it

  5. Yoni Fihrer November 29, 2016 at 11:10 pm #

    Thank you for this fix!!!
    During step 7, I get the specific path for libpython2.7.dylib and use that for the cmake, but it still come back at YYY. (I did it once without finding the correct filepath)
    Thanks again

    • Adrian Rosebrock December 1, 2016 at 7:42 am #

      It sounds like you might be copying and pasting the template into the command line rather than the updated path. If you’re still getting an issue try deleting the build directory, re-creating, and then re-running the CMake command with the paths updated.

  6. Bill Rodriguez November 30, 2016 at 2:13 pm #

    Adrian, I am pretty sure I followed all the steps properly and in my Cmake list I have python2=ON and python3=OFF as you stated. However when I run the Cmake list the error comes up that I need python3. What silly mistake have I made?

    Thanks,
    Bill

    • Adrian Rosebrock December 1, 2016 at 7:29 am #

      Hey Bill, what do you mean by a CMake error that lists you “need” Python 3? The sections you should be most concerned about are the “Python 2” and “Python 3” output sections of CMake that I have included in Figure 8.

  7. Schi December 4, 2016 at 4:43 am #

    Hey Adrian,

    short question, what are your reasons to use pip & virtuellenv vs anaconda? Friends of mine using anaconda for an virtuell environment and thought I also want to give it a try, but as I’m a noob, I’m afraid I won’t be able to do execute your instructions and using anaconda. So any thoughts about why you don’t use it?

    And also what would be your recommendation to a total beginner?

    Thanks,
    Schi

    • Adrian Rosebrock December 5, 2016 at 1:31 pm #

      Personally, I don’t like the Continuum company. A few years ago I had a pretty big falling out with them over business I would rather not discuss. There is nothing wrong with Anaconda, it’s a good deal — but I won’t recommend using it. I personally think virtualenv is easier to use and gives you more control in the long run.

  8. Tom de Ruiter December 4, 2016 at 7:13 pm #

    Hey! Great work, kudos!

    It seems I get stuck on step #7. Here I get the following response that Python2 has no libraries. It literally has a parameter ‘NO’ where normally the path would have been. Also, python2 is included in the unavailable array in the opencv modules.

    Any idea how to fix?

    • Adrian Rosebrock December 5, 2016 at 1:28 pm #

      Hey Tom — if the parameter says “NO” then you do not have the correct path for the Library. You’ll want to double check your libpython2.7.dylib path. Also make sure you are in the “cv” virtual environment prior to executing CMake.

      • Tom de Ruiter December 5, 2016 at 5:47 pm #

        Aha, I checked it a dozen times but couldn’t figure it out. Turned out that I messed up the virtual environments and used the old cmake parameters which were not initialized good. After reinstalling and deleting all environments I had a successful install! Thanks!

        • Adrian Rosebrock December 7, 2016 at 9:51 am #

          Congrats on resolving the issue Tom, nice job! :-)

        • Pedro Martins December 15, 2016 at 2:50 pm #

          I felt into the same problem on step #7 and couldn’t figure out how to solve it. To reinstall the cv virtual environment was the solution. Thanks Tom!

    • Xiao December 6, 2016 at 3:20 pm #

      I followed the step 7 and I got the same results with the compiling output showing that NO for the libraries… I check my path for the file multiple times and I was in the cv environment to execute the command. Do you happen to know what could be the problem?

      UPDATE:

      I solved my problem by adding sudo … Thank you for making the post. It is really useful.

      • Adrian Rosebrock December 7, 2016 at 9:41 am #

        You needed to add “sudo” to the CMake command? That doesn’t seem right. I would be curious to know more about any other commands you executed or if you are compiling OpenCV on a shared system of some sort.

  9. LibingMao December 6, 2016 at 4:11 am #

    Hey,Adrian.
    Great work.During step 7,i use my own file path replace yours.I have no errer in cmake but when i run make -j4 i still get the QTKit error.I double check the results from the cmake. The python2 is the same as yours.So i am confused now.

    • Adrian Rosebrock December 7, 2016 at 9:45 am #

      You’re still getting the QTKit error? Can you confirm that you are using macOS Sierra? Are you using a fresh install of Sierra? Or an upgraded version?

      • LibingMao December 10, 2016 at 11:07 pm #

        My Sierra version is 10.12.1.

        • Adrian Rosebrock December 12, 2016 at 10:43 am #

          Did you upgrade to Sierra? Or is this a fresh install of Sierra? If you upgraded, I would wipe the drive and start fresh.

  10. Jacob December 7, 2016 at 2:30 am #

    So is the best solution for OSX users to avoid the QTKit problem (I’m on El Capitan) to update to Sierra and follow this tutorial?

    Thanks!

    • Adrian Rosebrock December 7, 2016 at 9:37 am #

      El Capitan is sort of in “no mans land”. You are between Yosemite and Sierra, and unfortunately it’s hard to tell what state your system could be in. I would either downgrade to Yosemite or upgrade to Sierra.

  11. rick December 12, 2016 at 7:07 pm #

    I for the life of me cannot get the cmake template output to match all three requirements for python 2:

    1. The Interpreter points to the Python binary in your cv virtual environment.
    — check
    2. Libraries points to your libpython2.7.dylib file.
    –nope
    3. The numpy version being utilized is the one you installed in your cv virtual environment.
    –nope

    nor is it even listing my packages path.

    I have tried starting with a fresh and following EXACTLY every step of this guide to no avail.

    Can I just continue on without worrying about it?

    • Adrian Rosebrock December 14, 2016 at 8:40 am #

      The “Libraries” is the most important part. You’ll want to make sure that you have that properly configured.

  12. Emanuele Palma December 16, 2016 at 4:49 am #

    Hey,Adrian.
    Great work. I follow all the procedures correctly but when I launch make -j4, it gets to 100% and gets stuck to this line here:

    [100%] Built target example_facial_features

    entering a loop of Id mex sh clang. It doesn’t end the make step and so I cannot go on.
    What do you think the problem may be?

    • Adrian Rosebrock December 18, 2016 at 8:44 am #

      This sounds like a race condition of some sort by using 4 threads/cores for compiling. I would do a make clean followed by just make to compile with a single core — that should resolve the issue.

  13. Michael December 17, 2016 at 5:44 pm #

    All steps were ok, until

    Step #9

    In:
    cd /usr/local/lib/python2.7/site-packages/

    there is no cv2.so

    • Adrian Rosebrock December 18, 2016 at 8:38 am #

      Did you forget to run make install? This would account for the lack of cv2.so file. You should also check your CMake output and make sure your Python 2.7 section matches mine, otherwise your OpenCV bindings may have not been built.

  14. abhijith e m December 18, 2016 at 6:57 am #

    in step 8
    $ make -j4
    make: *** No targets specified and no makefile found. Stop.
    this message occurs why?

    • Adrian Rosebrock December 18, 2016 at 8:33 am #

      This is likely because CMake exited with an error. Go back to the CMake step and check to see if there were any errors.

  15. abhijith e m December 19, 2016 at 11:34 am #

    great finally it worked…
    i tried several times using the sourse code provided in opencv.org but it was failure. but yours was perfect thankyouuu….

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

      Nice job getting OpenCV installed! :-)

  16. Swaroop December 26, 2016 at 12:04 pm #

    I’m getting this error when I’m trying to install virtualenvwrapper, it says the following
    -bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
    I followed the same procedure, can you please help me?

    • Adrian Rosebrock December 31, 2016 at 1:41 pm #

      It seems likely that you either (1) did not update your .bash_profile file correctly and update it or (2) virtualenv/virtualenvwrapper did not install correctly. I would start by double-checking that these packages are installed via pip freeze.

  17. Héctor January 2, 2017 at 6:59 am #

    One question, in the part “Filling in the CMake template” how can we edit text inside the build directory?

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

      Hey Hector — can you clarify what you mean by “edit text inside the build directory”? I’m not sure what you mean.

    • David44 January 7, 2017 at 2:04 am #

      I think you can use any text editor, such as TextEdit on Mac, to first copy the template, then found the right values for your computer, then use the editor to make the changes. In the end you have a modified Cmake command to be executed from your Mac terminal.

      This has nothing to do with the build directory, it’s just to modify the command so that it has correct folder information so Cmake can be executed correctly.

  18. Jake January 4, 2017 at 8:28 pm #

    Hey man I messed up step 7 and did not fill in the blanks for the template. How do I revert and try again? Run the whole thing from step 1?

    Thanks again, great tutorial!

  19. Sylvain January 5, 2017 at 7:31 am #

    Hey Adrian, thank you very much for this tutorial, it helped me a lot. At step 7: when I copied past your template with my path parameters, I had to add the missing “\” otherwise it didn’t work properly (using python 2.7.13 on Sierra)

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

      Hey Sylvain — where is the missing “\” in the tutorial? I’d be happy to update it.

  20. smather January 7, 2017 at 10:35 am #

    Instead of being loaded from /usr/local/bin/python, which python returns /Library/Frameworks/Python.framework/Versions/2.7/bin/python
    I have the correct config line in the .bash_profile file.
    Any ideas about how to fix this?

    • Adrian Rosebrock January 9, 2017 at 9:19 am #

      Have you ran source ~/.bash_profile after updating the file or opening a new terminal? There may also be a typo in your .bash_profile file.

  21. Lindsay Rawitscher January 11, 2017 at 2:33 pm #

    I am getting stuck on step 7. I believe I have done everything correctly regarding the path names and can’t seem to figure out where I am going wrong. Any help would be appreciated! I have pasted my own pathnames into the template, not copied it directly.

    • Adrian Rosebrock January 12, 2017 at 7:58 am #

      Hey Lindsay — what is the error you are getting? Without knowing your error message myself and other PyImageSearch readers cannot help out.

  22. Brian January 15, 2017 at 3:55 am #

    I am getting stuck on step number 7 as well. Once I execute the cmake command with the proper directories pasted into the template, the Python 2 section in the output only includes information for the interpreter. It says nothing about libraries or numpy. I think I have the same problem as rick did above.

    I tried to proceed anyways, and unsurprisingly it did not work. There is no file named cv2.so in:

    /usr/local/lib/python2.7/site-packages

    I am sure I have followed this tutorial exactly, and this is a completely fresh install of Sierra. Any ideas about what to do?

    • Brian January 15, 2017 at 6:58 pm #

      I solved this by wiping my drive and reinstalling sierra, then trying again from scratch. I still don’t know what went wrong, I did everything the same the second time. At least it works now!

      • Adrian Rosebrock January 16, 2017 at 8:10 am #

        Congrats on resolving the error, Brian! When it comes to installing OpenCV I normally recommend a fresh install of macOS.

  23. Conno January 17, 2017 at 5:02 am #

    Hi Adrian, thank you very much for helping us installing OpenCV.
    I perform exactly as you explain steps #1- #3 but then after updating ~/.bash_profile, sourcing it, it still get an output of usr/bin/python for which python. I have tried so many times and I am stuck. What could I try besides doing the steps over and over? Is there a way to delete everything and start over again?

    • Adrian Rosebrock January 17, 2017 at 8:42 am #

      When it comes to installing OpenCV on macOS I recommend a clean, fresh install of the operating system. This ensures there are no legacy paths (perhaps from other libraries you previously installed) that might be causing problems. As far as debugging, I would ensure that the Homebrew Python was actually installed by manually changing directory to /usr/local/bin and ensuring you find the “python” binary there.

  24. Sidharth Sadani January 17, 2017 at 11:04 pm #

    Hi Adrian,
    Thank you so much for this. Worked in one go perfectly.

    Just one pointer, I got really confused whether I should stay in the cv environment while beginning the OpenCV installation or should I go back to the main env. Before reading your post till the end, and I know I might sound like a novice, which I am at OpenCV, I did not know that there are bindings established between OpenCV and python during installation. So you might just want to reiterate to stay in the ‘cv’ environment at the beginning of the OpenCV installation.

    But otherwise, perfect. I loved it. I’m new to working on a Mac, and I’m sure I would’ve stumbled around a lot to set this up, if not for your post. So thanks for taking the effort.

    • Adrian Rosebrock January 18, 2017 at 7:10 am #

      Thank you for the feedback Sidharth, I appreciate it. I’ll certainly keep this in mind for future OpenCV install tutorials.

Trackbacks/Pingbacks

  1. macOS: Install OpenCV 3 and Python 3.5 - PyImageSearch - December 5, 2016

    […] week I covered how to install OpenCV 3 with Python 2.7 bindings on macOS Sierra and […]

Leave a Reply