Create an OpenCV3 + python3 environment on OSX

environment

osx El Capitan

What to install

flow

  1. Install python3.5.1 with pyenv
  2. Install OpenCV3
  3. Make OpenCV3 work in the environment of 2.

1. Install python3.5.1 with pyenv

I want to run OpenCV3 series and I want to use a new python. However, there are many things written for the OpenCV2 series, so I want to be able to run the OpenCV2 series as well. OpenCV2 series seems to work only with python2 series, so I want to make it possible to run multiple environments. So I decided to install pyenv and change the environment from there.

$ brew install pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(pyenv init -)"' >> ~/.zshrc

# python3.5.Install 1
$ pyenv install 3.5.1

2. Install OpenCV3

Introduced from brew.

# brew install opencv3 --with-I tried hitting python3, but opencv3 is homebrew/science/I'm told to change to opencv3
$ brew install homebrew/science/opencv3 --with-python3

I put the above command from brew, but it doesn't work with the following error.

/tmp/opencv3-20161123-64293-17bmzya/opencv-3.1.0/modules/videoio/src/cap_qtkit.mm:46:9: fatal error: 'QTKit/QTKit.h' file not found
#import <QTKit/QTKit.h>

http://qiita.com/masaori/items/0c78fcd58a6c6bf4f655 This link says it happened on OS X Sierra, but it also happened on El Capitan. The solution is the same for now, get a new one by specifying HEAD in the brew option. Getting the HEAD may have mixed some other commits, but at this point there is no particular inconvenience.

$ brew install homebrew/science/opencv3 --with-python3 --HEAD

3. Make OpenCV3 work in the environment of 2.

Make a symbolic link so that you can read the OpenCV so file via pyenv

$ ln -s /usr/local/Cellar/opencv3/HEAD-b8c875d_4/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so ~/.pyenv/versions/3.5.1/lib/python3.5/site-packages

Check if it can be read properly from REPL below

$ python

Python 3.5.1 (default, Nov  7 2016, 22:30:16)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

Being addicted

At some point, the python2.7 module was loaded. Therefore, when you build OpenCV with brew, the so file is placed in / usr / local / opt / opencv3 / lib / python2.7 / site-packages /, and even in the file, it is built on the premise of python2.7. Because of this, even if I put up a symbolic link, I got the following error and couldn't read it with python.

$ python

Python 3.5.1 (default, Nov  7 2016, 22:30:16)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/opt/opencv3/lib/python2.7/site-packages/cv2.so, 2): Symbol not found: _PyCObject_Type
  Referenced from: /usr/local/opt/opencv3/lib/python2.7/site-packages/cv2.so
  Expected in: flat namespace
 in /usr/local/opt/opencv3/lib/python2.7/site-packages/cv2.so
>>>

Solve the module loading problem from here http://qiita.com/Asakage/items/690ce9048e708de41166

If you comment out the line in ~ / .local / lib / python3.5 / site-packages / homebrew.pth, python3.5 will be loaded properly. It is unknown when this path was read. I set up pyvenv and did some things that weren't written here, so it may have happened at that time.

It takes a long time to build and various problems have occurred, so it may be the fastest to install OpenCV using conda.

Recommended Posts

Create an OpenCV3 + python3 environment on OSX
Kivy + Python3 on OSX environment maintenance
Create a Python environment on Mac (2017/4)
OpenCV3 & Python3 environment construction on Ubuntu
Build Python3 and OpenCV environment on Ubuntu 18.04
Create a python environment on your Mac
Build a Python + OpenCV environment on Cloud9
Create a Python environment
Create an environment of 64bit Windows + python 2.7 + MeCab 0.996
[Python] Create an asynchronous task execution environment + monitoring environment
[Venv] Create a python virtual environment on Ubuntu
Create a Python execution environment on IBM i
Create a Python virtual development environment on Windows
Build an OpenCV4 environment on Raspberry Pi using Poetry
Create an environment with virtualenv
Create a comfortable Python 3 (Anaconda) development environment on windows
Install OpenCV on Ubuntu + python
Build python environment on windows
Create an OpenAI Gym environment with bash on Windows 10
Create a decent shell and python environment on Windows
Environment construction of python2 & 3 (OSX)
Create a Python development environment on OS X Lion
Build an environment for machine learning using Python on MacOSX
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Let's get started with Python ~ Building an environment on Windows 10 ~
Build an Ubuntu python development environment on Google Cloud Platform
Building an environment to execute python programs on AWS EC2
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]
Create a virtual environment for python on mac [Very easy]
Building a Python environment on Mac
Environment construction of python and opencv
Python environment construction memo on Windows 10
Prepare Python development environment on Ubuntu
Anaconda python environment construction on Windows 10
Building a Python environment on Ubuntu
Install python2.7 on windows 32bit environment
Studying Python Part.1 Creating an environment
Create a virtual environment with Python!
Python environment construction memo on Mac
Create a Linux environment on Windows 10
Python development environment construction on macOS
Environment construction of python3.8 on mac
Install Python development environment on Windows 10
Draw an illustration with Python + OpenCV
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Build a python3 environment on CentOS7
Introduce Python 3.5.2 environment on Amazon Linux
I set the python operating environment on OSX Yosemite (pyenv + pip)
Create a Python environment for professionals in VS Code on Windows
I tried to create an environment of MkDocs on Amazon Linux
Create an AWS Cloud9 development environment on your Amazon EC2 instance
Build and try an OpenCV & Python environment in minutes using Docker
When creating an environment that uses python django on Ubuntu 12.04 LTS
Notes for using OpenCV on Windows10 Python 3.8.3.
[Python] Building an environment with Anaconda [Mac]
Build an environment for Blender built-in Python
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Build a python environment on MacOS (Catallina)
Python virtual environment and packages on Ubuntu
How to create an NVIDIA Docker environment
Note when creating an environment with python