patrick
patrick

Reputation: 6840

How to install OpenCV for python

HI! I'm trying to install opencv and use it with python, but when I compile it I get no errors but I can't import cv module from python:

patrick:release patrick$ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv

The code I used to compile it is this:

cd opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON
make
sudo make install

how can I get it working with python?

Upvotes: 13

Views: 41443

Answers (8)

HimanshuGahlot
HimanshuGahlot

Reputation: 571

When using Virtual Environment

Thanks to @user495470. Follow these steps

brew update
brew install -v cmake 
brew install opencv`

If Part 1 didn't work kindly follow Part 2

Part I
Next step might work sometime, although it didn't work for me
export PYTHONPATH="/VENV_PATH/python2.7/site-packages:$PYTHONPATH"
Then check in python IDE check with import cv or import cv2

Part 2
Go to this path /usr/local/Cellar/opencv/3.4.3/lib/python2.7/site-packages/ or /usr/local/lib/python2.7/site-packages
Copy cv2.so file
Paste it /VENV_PATH/lib/python2.7/site-packages here
Then check in python IDE check with import cv or import cv2

Kindly let me know if this thing works.

Upvotes: 0

JobJob
JobJob

Reputation: 4127

This worked for me (change python36 to whatever version you want)

sudo port install opencv +avx2 +python36 +qt5 +contrib +eigen

I got this error and had to apply the patch there (download link)

Apply the patch with:

sudo patch /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-3.2.0/modules/highgui/src/window_QT.cpp ~/Downloads/patch-cpp11-narrowing-error.diff

Then run sudo port install -N opencv +avx2 +python36 +qt5 +contrib +eigen again

Upvotes: 1

Ive Ji
Ive Ji

Reputation: 21

easy_install pip
pip install opencv-python --user
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

Maybe you will use sudo,

and the path of installed may not be like mentioned.

Upvotes: 2

nodejh
nodejh

Reputation: 8008

We can install opencv for Python for Mac OS X with home-brew.

First, install home-brew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

You can see the details for how to install homebrew. http://brew.sh

If you don't install Python, install python(brew will install python2.7):

brew install python

Maybe you want install Python3: brew install python3

Then install opencv3 for Python3:

brew install opencv3 --with-python3

If you want install opencv3 for Python(Python2.7): brew install opencv3 --with-python

OR install opencv2 for Python3: brew install opencv --with-python3

OR if you want install opencv2 for Python3: brew install opencv --with-python3

Finally, maybe you will link site-packages of opencv to the site-packages of Python.

Notes: In the follow command, /usr/local/opt/opencv3/lib/python3.5/site-packages is the directory of opencv3's site-packages, /usr/local/lib/python3.5/site-packages/ is the directory of Python3.5's site-packages.

Maybe you should change the two to your own OPENCV AND PYTHON site-packages directories.

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

Upvotes: 2

Jaime Ivan Cervantes
Jaime Ivan Cervantes

Reputation: 3697

If you have you want a simple and quick install in Windows, you can download Python(x,y). This distribution includes OpenCv. Be sure to specify that you want to install OpenCV in the installation setup, because it is not installed by default.

Upvotes: 0

Lri
Lri

Reputation: 27593

brew tap homebrew/science
brew install opencv
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

Upvotes: 18

patrick
patrick

Reputation: 6840

I found here a way to install opencv for python: http://recursive-design.com/blog/2010/12/14/face-detection-with-osx-and-python/ :)

Upvotes: 4

Alex Martelli
Alex Martelli

Reputation: 881457

You could try ctypes-opencv -- not sure why building and installing with -D BUILD_PYTHON_SUPPORT=ON didn't work for you (maybe it doesn't know where to install the Python wrappers in osx...?), but the ctypes wrappers should, in theory, work anyway.

Upvotes: 0

Related Questions