13

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?

patrick
  • 6,533
  • 7
  • 45
  • 66

8 Answers8

18
brew tap homebrew/science
brew install opencv
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
Lri
  • 26,768
  • 8
  • 84
  • 82
  • 9
    I think it would be more helpful for the OP and further visitos when you add some explaination to your intension. – Reporter Aug 08 '14 at 10:44
4

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

patrick
  • 6,533
  • 7
  • 45
  • 66
  • It works the same also for Linux, not only OSX. The important step is to define [PYTHONPATH](http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH) environment variable according to where `cv.so` is installed. – sastanin Feb 05 '11 at 18:40
2

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

nodejh
  • 7,928
  • 1
  • 20
  • 24
2
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.

Ive Ji
  • 21
  • 2
1

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

JobJob
  • 3,822
  • 3
  • 33
  • 34
0

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.

Jaime Ivan Cervantes
  • 3,579
  • 1
  • 40
  • 38
0

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.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • the build_python instruction seems to be not working because it doesn't create the cv.pyd (it creates only cv.so). Anyway I just tried ctypes-opencv and I get this error: http://dpaste.com/221536/ – patrick Jul 24 '10 at 18:38
0

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.

HimanshuGahlot
  • 561
  • 4
  • 11