opencv python osx

回眸只為那壹抹淺笑 提交于 2019-11-27 19:29:20
Ferguzz

I struggled for a while with Python on Mac. Here is how I finally (and easily!) installed it. Remove all the things Python you have on there already. They will probably be located at /Library/Frameworks/Python.Framework and /opt/local/var/macports/software/py26*

First download Python with Macports.

sudo port install python27

Then make sure your system is using this version with:

sudo port select --set python python27

Next install numpy with:

sudo port install py27-numpy

Now install opencv:

sudo port install opencv +python27

Now edit your ~/.bash_profile with:

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit ~/.bash_profile

or

open -t ~/.bash_profile

and add the line:

export PYTHONPATH=/opt/local/var/macports/software/opencv/2.2.0_0+python27/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH

or where ever your version of the cv.so file is hidden....

Now restart terminal and try:

%python
>>>import cv

I'm using Netbeans for opencv and python and it works really nice. Good luck.

$ brew search opencv
        homebrew/science/opencv
$ brew install homebrew/science/opencv

after installed, there is warning:

==> Caveats If you need Python to find the installed site-packages:

mkdir -p ~/Library/Python/2.7/lib/python/site-packages

echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth

so, just do

  mkdir -p ~/Library/Python/2.7/lib/python/site-packages
  echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth

If you notice the first line output when running python, you'll see that you're still using the Apple-supplied Python interpreter. Try installing and using the python-select package in MacPorts and then try the instructions again starting from step 2.

Also make sure you followed all of the steps when installing MacPorts so that /usr/local/bin is on $PATH.

Another "hack" I found during my struggles using CMake (but maybe the problem is the same with ports) : it appears that the python modules location has been duplicated on my Mac OS Lion, for a reason I can't explain.

CMake wants to put the "cv" module here :

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

Whereas my default Python interpreter is looking here (thanks PyCharm for telling me) :

/Library/Python/2.7/site-packages

Moving both cv2.so and cv.py files to the second location, did the trick for me. I don't know if this is the cleanest way.

Hope it can help some googlers !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!