如何在 OSX 上正确安装 Python 以与 OpenCV 一起使用?

发布于 2024-11-30 10:04:59 字数 416 浏览 1 评论 0原文

过去几天我一直在尝试让 opencv 与我安装的 Python 2.7 一起工作。每当我尝试“导入 cv”时,我总是收到一条错误消息,提示未找到 opencv 模块。

然后我决定尝试使用 Macports 安装 opencv,但这不起作用。

接下来,我尝试了 Homebrew,但也不起作用。

最终,我发现我应该这样修改 PYTHONPATH: 导出 PYTHONPATH="/usr/local/lib/python2.6/site-packages/:$PYTHONPATH"

我的问题是我没有找到 /usr/local/lib/python2.*...etc 该文件夹根本不存在

所以我的问题是这样的: 如何在 OS X Snow Leopard 上正确安装 Python 以使其与 opencv 一起使用?

多谢,

I spent the past couple of days trying to get opencv to work with my Python 2.7 install. I kept getting an error saying that opencv module was not found whenever I try "import cv".

I then decided to try installing opencv using Macports, but that didn't work.

Next, I tried Homebrew, but that didn't work either.

Eventually, I discovered I should modify the PYTHONPATH as such:
export PYTHONPATH="/usr/local/lib/python2.6/site-packages/:$PYTHONPATH"

My problem is that I didn't find /usr/local/lib/python2.*...etc
The folder simply doesn't exist

So my question is this:
How do I properly install Python on OS X Snow Leopard for it to work with opencv?

Thanks a lot,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野稚 2024-12-07 10:04:59

我自己花了几天时间在这上面。对我来说,问题是 OpenCV 安装程序没有找到正确的 python 安装。尽管我已经使用自制软件升级了 python 并且使用了 python 的 virtualenv,但它还是默认为 MacOS 安装的版本。我在这里收集了大部分设置的要点:
https://gist.github.com/4150916

使用 homebrew 获取所有依赖项,然后下载OpenCV tarball 并自行编译,确保指定所有与 python 相关的配置选项。

假设一个名为“opencv”的 virtualenv...

cd OpenCV-2.4.3/
mkdir release
cd release
cmake -D PYTHON_EXECUTABLE=$WORKON_HOME/opencv/bin/python \
 -D PYTHON_PACKAGES_PATH=$WORKON_HOME/opencv/lib/python2.7/site-packages \
 -D INSTALL_PYTHON_EXAMPLES=ON\
 -D PYTHON_INCLUDE_DIR=/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Headers\
 -D PYTHON_LIBRARY=/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib\
..
make -j8
make install

I spent a couple days on this myself. For me, the problem was that that OpenCV installer was not finding the right python installation. It was defaulting to the MacOS-installed version despite the fact that I had upgraded python with homebrew and was using a virtualenv for python. I have collected most of my setup in a gist here:
https://gist.github.com/4150916

Use homebrew to get all the dependencies, but then download the OpenCV tarball and compile yourself being sure to specify all the python related configuration options.

Assuming a virtualenv named 'opencv'...

cd OpenCV-2.4.3/
mkdir release
cd release
cmake -D PYTHON_EXECUTABLE=$WORKON_HOME/opencv/bin/python \
 -D PYTHON_PACKAGES_PATH=$WORKON_HOME/opencv/lib/python2.7/site-packages \
 -D INSTALL_PYTHON_EXAMPLES=ON\
 -D PYTHON_INCLUDE_DIR=/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Headers\
 -D PYTHON_LIBRARY=/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib\
..
make -j8
make install
违心° 2024-12-07 10:04:59

您需要使用 python2.7 安装来安装该模块。将 PYTHONPATH 指向 2.6 下安装的东西以在 2.7 下运行是一个坏主意。

根据您想要的安装方式,执行 python2.7 setup.pyeasy_install-2.7 opencv 等操作进行安装。

fwiw,在 OS X 上,模块通常安装在 /System/Library/Frameworks/Python.framework/ 下,但您几乎不需要知道站点包中安装的任何内容的物理位置;如果 Python 在没有帮助的情况下无法找到它们,那么说明它们安装错误。

You need to install the module using your python2.7 installation. Pointing your PYTHONPATH at stuff installed under 2.6 to run under 2.7 is a Bad Idea.

Depending on how you want to install it, do something like python2.7 setup.py or easy_install-2.7 opencv to install.

fwiw, on OS X the modules are usually installed under /System/Library/Frameworks/Python.framework/ but you should almost never need to know where anything installed in your site packages is physically located; if Python can't find them without help you've installed them wrong.

会发光的星星闪亮亮i 2024-12-07 10:04:59

使用 Homebrew 安装 OpenCV

brew tap homebrew/homebrew-science
brew install opencv

设置 Python

取决于您的安装位置 - OS X 默认

cd /Library/Python/2.7/site-packages/

或 - Homebrew Python

cd /usr/local/lib/python2.7

然后创建符号链接

ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so

上述方法源自 博客文章

Installing OpenCV with Homebrew

brew tap homebrew/homebrew-science
brew install opencv

Setting up Python

Depending on your install location - OS X Default

cd /Library/Python/2.7/site-packages/

or - Homebrew Python

cd /usr/local/lib/python2.7

Then create the symbolic link

ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so

The above method sourced from a blog post.

眼泪淡了忧伤 2024-12-07 10:04:59

我搜索并尝试用 python3 安装 opencv3 3天。有些链接建议使用 Brew 和一些虚拟环境,有些链接建议安装 xcode 但在我的情况下都失败了。
不要使用linux步骤在Mac上安装opencv-python。 Mac 的问题是 Mac 已经安装了 Python 2.7。最重要的是,安装和链接所有站点包几乎没有问题,我们最终会遇到错误。

我将分享我所做的:在 Mac 上安装完整包 opencv3、numpy、matplotlib、notebook、spyder 等的简单步骤。

  1. 安装 anaconda,它会创建一个目录并安装其中的所有内容
    使用此链接 -> https://www.continuum.io/downloads
    下载 command-line-install

  2. 下载后,转到终端并下载 anaconda 的位置。
    $ bash Anaconda3-4.3.0-MacOSX-x86_64.sh

  3. 安装将要求您将路径附加到 .bash_profile >>>说是

  4. 转到主目录,运行 .bash_profile
    $ source .bash_profile

  5. 检查python,应该指向
    $ 哪个Python
    $ /.../anaconda/bin/python

  6. 最后一步
    $ pip install opencv-pyhton

$ python

$ import cv2

如果没有错误,我们就可以开始了。

I searched and tried installing opencv3 with python3 for 3 days. Some links suggest for Brew and some virtual env, some say install xcode but all failed in my case.
Dont use linux steps to instal opencv-python on Mac. Problem with Mac is Python 2.7 is already installed by Mac. On top of that installing and linking all site-packages is little problematic and we end up with errors.

I'll share what I did: easy steps to install complete package opencv3, numpy, matplotlib, notebook, spyder etc.. on Mac.

  1. Install anaconda, it creates a directory and install everything inside that
    use this link -> https://www.continuum.io/downloads
    download command-line-install

  2. After download, goto terminal and download location of anaconda.
    $ bash Anaconda3-4.3.0-MacOSX-x86_64.sh

  3. Installation will ask you to append path to .bash_profile >> say yes

  4. Goto home directory, run .bash_profile
    $ source .bash_profile

  5. check python, should be pointing to
    $ which python
    $ /.../anaconda/bin/python

  6. Last step
    $ pip install opencv-pyhton

$ python

$ import cv2

if no errors, we are good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文