macos opencv python osx

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5846745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:01:15  来源:igfitidea点击:

opencv python osx

pythonmacosopencvmacports

提问by dsg

(Step 1)

(步骤1)

I'm trying to get openCV to run from python on my mac using the MacPorts install http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port, and also trying to follow The Petite Geek's guide:

我正在尝试使用 MacPorts 安装http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port来让 openCV 在我的 Mac 上从 python 运行,并且还尝试遵循The Petite Geek's guide

sudo port -v install opencv +python26

It runs for about 10 minutes without errors.

它运行了大约 10 分钟没有错误。

(Step 2)

(第2步)

I download ctypes-opencv source and demo files. I navigate to the srcdirectory and run:

我下载了ctypes-opencv 源代码和演示文件。我导航到src目录并运行:

sudo python setup.py install

I see like 50 lines almost all of the form: copying ... -> ..., which looks good to me. No errors here.

我看到几乎所有的形式都有 50 行: copying ... -> ...,这对我来说看起来不错。这里没有错误。

(Step 3)

(第 3 步)

I add export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/libto the end of my ~/.profile.

我添加export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib到我的~/.profile.

(Step 4)

(步骤4)

I open a new terminal to test my install. From my home folder:

我打开一个新终端来测试我的安装。从我的主文件夹:

$ python

Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] 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
>>> 

Does not work.

不起作用。



I read somewherethat opencv installs python bindings with the default version of python for OSX, and I am probably running a non-default version, but this is not actionable information to me.

我在某处读到opencv 使用 OSX 默认版本的 python 安装 python 绑定,我可能运行的是非默认版本,但这对我来说不是可操作的信息。

回答by 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*

我在 Mac 上使用 Python 挣扎了一段时间。这是我最终(而且很容易!)安装它的方法。删除你已经在那里的所有 Python 东西。它们可能位于 /Library/Frameworks/Python.Framework 和 /opt/local/var/macports/software/py26*

First download Python with Macports.

首先下载带有 Macports 的 Python。

sudo port install python27

Then make sure your system is using this version with:

然后确保您的系统正在使用此版本:

sudo port select --set python python27

Next install numpy with:

接下来安装numpy:

sudo port install py27-numpy

Now install opencv:

现在安装opencv:

sudo port install opencv +python27

Now edit your ~/.bash_profile with:

现在编辑你的 ~/.bash_profile :

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....

或者你的 cv.so 文件版本在哪里隐藏....

Now restart terminal and try:

现在重新启动终端并尝试:

%python
>>>import cv

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

我正在将 Netbeans 用于 opencv 和 python,它的效果非常好。祝你好运。

回答by lbsweek

$ 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:

==> 警告 如果您需要 Python 来查找已安装的站点包:

mkdir -p ~/Library/Python/2.7/lib/python/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

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

回答by rspeed

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.

如果您在运行 python 时注意到第一行输出,您将看到您仍在使用 Apple 提供的 Python 解释器。尝试在 MacPorts 中安装和使用 python-select 包,然后从步骤 2 开始再次尝试说明。

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

还要确保在安装 MacPorts 时遵循所有步骤,以便 /usr/local/bin 位于 $PATH 上。

回答by Arnaud P

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 的过程中发现的另一个“黑客”(但也许问题与端口相同):似乎 python 模块位置已在我的 Mac OS Lion 上重复,原因我无法解释。

CMake wants to put the "cv" module here :

CMake 想把“cv”模块放在这里:

/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) :

而我的默认 Python 解释器正在这里查看(感谢 PyCharm 告诉我):

/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.

将 cv2.so 和 cv.py 文件移动到第二个位置,这对我来说很有效。我不知道这是否是最干净的方式。

Hope it can help some googlers !

希望它可以帮助一些谷歌人!