Opencv python install mac os

Installing OpenCV in MacOS High Sierra for python 3

Dec 5, 2017 · 3 min read

When trying to install OpenCV in Mac OS High Sierra by following tutorials on the internet, most probably you’ll end up having errors related to permission denied while installation of virtualenv and virtualenvwrapper. For example, the following is the most common one that most people encounter.

Most probably installing any python library using pip m ight give similar kind of errors. First, let’s try to understand why this kind of errors occurs in Mac OS but not on Linux. In Mac OS, there is a new security layer called System Integrity Protection . This will protect changing system files even from the root user. Therefore for the permission relates issues, though you use sudo with pip it might not work. The other thing is python is already installed in Mac OS and above error shows the system installed python location. Most of the time people try to install Open CV and python using brew and though they set path to use python installed by brew, the above error can occur. This might occur due to previously installed packages with system python installation and brew try to attach to those.

Following steps will help to resolve those issues.

  1. First of all install Xcode. This is to install C compiler to mac and easiest way to install Xcode is via the app store and you’ll able to find an ample amount of tutorials on how to do this on the internet. After that install Xcode command-line tools using the following command.

2. Install homebrew using any tutorial and add the following line to at the end of your

/.bashrc file. I prefer .bashrc since I use a custom terminal and bash_profile won’t always run.

And then use the source command to load the changes in the corresponding file.

3. Then install python using brew using the following command and link them.

Note that we have installed both python2 and python3 as well in here it is better since using system python can cause problems in future.

4. Next, let’s install pip3 for python3. Unlike with normal brew python 2.7 installation, it won’t automatically install with python3. You have to run the following command to do that.

5. Then we have to install virtualenv and virtualenvwrapper . This is to create a virtual environment for each project therefor dependencies of multiple projects do not collide with each other. This comes handy when two projects need the same python package but with two different versions.

After that add following lines to the

6. Install OpenCV using following command. This will install OpenCV globally and later when we create virtual environments and we’ll refer this within our virtual environments since we do not need to install it in every environment we create.

7. Add OpenCV site path to global site path. Please note that python version number can different in your installation and change accordingly if required.

Читайте также:  Error 0x800f081f windows server 2012

8. Now let’s create a virtual environment for the OpenCV project and point our previous OpenCV installation so that it can reuse it. To create a virtual environment with the name of cv-py3 run first command. You can give any name you prefer. The second command will switch to that env and the third one will exit from env.

After you run the second command it will switch to that environment and whatever python packages you install will be within the environment. This virtual environment is located in

/.virtualenvs. You’ll see a separate folder for each environment. Now you can install any python library within this environment see the following example.

Then lets link previously installed OpenCV library to this virtual environment so we can use it without reinstalling.

9. Now let’s try running OpenCV

The first command will switch to previously created env second will run the python3 and the third one will import open cv. If the third one runs without any errors, installation is a success. And you can print the version of it using the fourth command.

You can exit from env using deactivate command.

Источник

PERPETUAL ENIGMA

PERENNIAL FASCINATION WITH ALL THINGS TECH

Installing OpenCV 3 With Python On Mac OS X

OpenCV is the world’s most popular computer vision library and it’s used extensively by researchers and developers around the world. OpenCV has been around for a while now and they add something new and interesting with every new release. One of the main additions of OpenCV 3 is “opencv_contrib” which contains a lot of cutting edge algorithms for feature descriptors, text detection, object tracking, shape matching, and so on. They have greatly improved Python support in this release as well. Since OpenCV is available on almost all the popular platforms, this version looks very promising. Let’s see how to install OpenCV 3 with Python support on Mac OS X.

Prerequisites

CMake: Make sure you have cmake. If you don’t, you can download it from here. It’s a dmg file, so you can just download it and run the installer.

Install Python using Homebrew: This is an important step! Homebrew is a package manager for OS X that makes our lives easier in many different ways. Instead of using system Python, we need to use brewed Python (this is basically Python installed using Homebrew). If you don’t have Homebrew, you can install it using the following command:

Now that Homebrew is installed, let’s update it and install Python:

/.profile file and add the following line:

We need to reload the file to update the environment variables. Run the following command to do it:

Let’s confirm that you are using brewed Python. Run the following command from your terminal:

If you see “/usr/local/bin/python” printed on your terminal, you can proceed.

Download OpenCV 3.0.0: You can download it from here.

Download “opencv_contrib”: As discussed earlier, we can use the latest computer vision algorithms from “opencv_contrib”. It is basically a repository that contains state of the art algorithms. Bear in mind that some of them are not free for commercial use, but it is great tool to learn new algorithms. Download opencv_contrib from here.

Installation

We are now ready to build. Run the following commands from you terminal:

Читайте также:  Драйвера via hd audio для windows 10 x64

Let’s take a moment to understand what these flags mean exactly:

  • CMAKE_BUILD_TYPE=RELEASE : We are telling cmake that we are building a “release” version of OpenCV.
  • CMAKE_INSTALL_PREFIX : This is the directory where OpenCV 3.0.0 will be installed
  • PYTHON2_LIBRARY : This is the path to your brewed Python (Hombrew installation of Python)
  • PYTHON2_INCLUDE_DIR : This is the path to Python header files for compilation.
  • INSTALL_C_EXAMPLES : This flag indicates that the C/C++ examples need to be installed after compilation.
  • INSTALL_PYTHON_EXAMPLES : This flag indicates that the Python examples need to be installed after compilation.
  • BUILD_EXAMPLES=ON : This flag indicates that we want to compile the included OpenCV examples.
  • OPENCV_EXTRA_MODULES_PATH : This flag indicates that OpenCV should compile the extra modules (opencv_contrib) that we downloaded earlier.

Let’s go ahead and install OpenCV 3.0.0. Make sure you are inside the directory “/path/to/opencv-3.0.0/build” and run the following commands:

The “-j4” flag indicates that it should use 4 cores. We are not done yet! Let’s set the library path:

If you want to make it permanent, just add the following line in your “

We need to copy the pkg-config file “opencv.pc” to “/usr/local/lib/pkgconfig” and name it “opencv3.pc” so that it doesn’t conflict with our existing OpenCV 2.4.x config file:

We also need to update our PKG_CONFIG_PATH environment variable to make sure it knows where opencv3.pc is located. Open up your “

/.profile” file and add the following line:

Let’s see if OpenCV with C++ is working:

If you see “Welcome to OpenCV 3.0.0” printed on the terminal, you are good! Let’s check the OpenCV-Python version:

You should see “3.0.0” printed on the terminal. If you see that, then you are done! You have successfully installed OpenCV 3 with Python support on Mac OS X. Let’s check if it’s working by using something that exists in OpenCV 3.0.0 but not in OpenCV 2.4.9. Go into Python shell by typing “python” in your terminal and run the following commands:

If the above line doesn’t throw an error, then you are all set! You have now successfully verified your OpenCV 3 installation with Python support.

Источник

Building and Installing OpenCV on M1 Macbook

Prabhat Kumar Sahu

May 27 · 5 min read

In this blog post, we are going to build and install OpenCV on anM1 Mac.

Last year in November 2020 apple releases their first ARM64-based M1 chip. It got a lot of attention from everyone.

Being a tech enthusiast and a programmer, I was amazed to see the performance of the new apple M1 chip. The benchmarks were really good.

Recently only some months back, I bought myself an M1 Macbook Pro with 8Gigs of RAM and 512GB of SSD.

Let’s get started.

Here are the things that we are going to do.

  • Step 1: Homebrew and Xcode Command Line Tools Installation
  • Step2: Install Miniforge
  • Step3: Create a virtual environment with python3.8
  • Step4: Install necessary dependencies
  • Step5: Compile and Build OpenCV
  • Step6: Test OpenCV

Step1: Homebrew and XCode CLI Installation

If Home b rew is not already installed in your system you can install it by running the following in your terminal.

After it gets installed, install wget and CMake.

I have already installed Xcode Command Line Tools on my mac. If it’s not already installed in your system, you can install it by running the following command below in your terminal.

Читайте также:  Драйвер canon laser shot lbp 1120 для linux

Step2: Install Miniforge

Install miniforge for arm64 (Apple Silicon) from miniforge GitHub.

Miniforge enables installing python packages natively compiled for Apple Silicon.

After the installation of miniforge, by default, it gives us one base environment. You can turn off the default base env by running

Step3: Create a virtual environment

Let’s create a virtual environment named cv2 with python3.8 installed.

And activate it by running

Step4: Installing Some Dependencies

As we are going to install OpenCV natively we have to install the compatible version of Numpy which Apple provides for M1.

Run the following commands below on a terminal to install Numpy and some other dependencies.

Step5: Compile and Build OpenCV

We have to download OpenCV and OpenCV Contrib first.

I have created one folder named test_opencv where I will be downloading files and build there.

Let’s Set up the OpenCV build with CMake:

Make sure to put your own PATH on OPENCV_EXTRA_MODULES

Also, make sure to activate your conda environment before running make.

You have to put your Python3 executable path also. You can get it by running

For eg: My path is

You have to change and put your own path there.

Now you can run

You will see the screen below after Cmake. The compilation doesn’t take that much time.

Now we have the run the make command.

Here j8 stands for 8 cores. You can do either make or you can adjust the j option . So let’s run

You will get an output like the below. It took around 10 minutes for me.

Then the last step is to run

You will get the above after sudo make install.

We need to first locate the .so file, which has been generated during the compilation of OpenCV.

Now, we need to execute the following to sym-link one of the .so files in our current Python virtual environment.

You can see this from the screenshot. There’s one so file that has been generated inside the build folder. The path to the .so file in my system is

Also, Please double-check the paths before executing the commands.

Open up another terminal and go to your python environment site-packages directory. In my case it’s

And execute the sym-link

This I ran on my second terminal.

And that’s it. Now we are good to go.

Open a separate terminal if you want and activate your virtual environment

And just run the below

Step6: Test OpenCV by running a program

Create a new file named main.py and paste the code below. Put a dummy photo for test purposes in the third line. The code just converts the image to a black-white image.

And run it python3 main.py. Here’s the output

Additional Step

If you want to uninstall OpenCV, you can go inside the build folder and run

It will remove OpenCV 4.5.0 from your system. Also, you can delete your conda environment. First, deactivate if it’s activated

It will remove completely.

We were able to build and install OpenCV 4.5.0 successfully in our M1 Mac and we are able to run some code also.

If you have any questions, recommendations, or critiques, I can be reached via Twitter or via my mail. Feel free to reach out to me.

Источник

Оцените статью