安装并运行 Mac Python Open CV [英] Install and run Mac Python Open CV
问题描述
我安装了python,安装了python 3,使用homebrew安装了opencv,还通过GUI安装了python,遵循了几年前写的各种说明页面和评论.
我想使用 OpenCV 来检测最亮和最暗的像素,并在它们之间画线,根据每个单元格中的亮点和暗点将图像分割成单元格.我认为这是该软件的合理应用.
当我使用 python 进行任何安装并输入 import cv 时,我收到一条无模块"消息.
首先,您需要决定是要运行 Python2 还是 Python 3.我会提倡 Python 3,首先因为这显然是一个新项目,因此您可能以及使用最新最好的 Python,其次是因为 Python 2 将在 9 天后停止使用.
然后您需要决定是要使用 Apple 提供的 Python,在 /usr/bin
中还是 homebrew 提供的 Python.由于您已经安装了 homebrew Python,我建议使用 homebrew 一个,因为您可以删除整个内容并重新安装它,如果您在所有 Apple-提供的补丁、操作系统升级以及他们对 Python 的其他用途将不受影响.快乐的日子!
因此,您想使用 homebrew Python 3.现在检查 homebrew 告诉您什么,运行:
brew info python
<块引用>
未版本化符号链接 python
、python-config
、pip
等指向python3
、python3-config
、pip3
等,分别被安装到/usr/local/opt/python/libexec/bin
这意味着如果你想运行 python
和 pip
(没有版本号,而不是 python3
和 pip3
) 并期望启动 Python 3 及其对应的 pip
,您需要将 /usr/local/opt/python/libexec/bin
放在bash profile
中的 PATH(可能是 $HOME/.bash_profile
):
export PATH=/usr/local/opt/python/libexec/bin:$PATH
然后启动一个新终端并检查当您键入 python
时运行的是哪个 Python:
类型python
如果您在正确的轨道上,它必须报告如下:
python 是/usr/local/opt/python/libexec/bin/python
然后,您可以检查使用的是哪个 Python 版本:
python -V
它应该是:
Python 3.7.5
现在您可以使用以下命令安装 OpenCV:
pip install opencv-python
然后在解释器中加载它:
导入 cv2
<小时>
如果出于某种原因,您想运行旧的 v2.7 Python,或者因为安装冲突模块而冒着搞乱 macOS 安装的风险,请忽略我的回答.
I have python installed, python 3 installed, installed opencv using homebrew, also installed python through the GUI, followed various instructions pages and comments written several years ago.
I'd like to use OpenCV to detect brightest and darkest pixels and draw line between them to carve the image up into cells based on the bright and dark points in each cell. I assume this a reasonable application for this software.
When I go to python, any installation, and enter import cv I get a 'no module' message.
First, you need to decide whether you want to run Python2 or Python 3. I would advocate Python 3, firstly because this is clearly a new project so you may as well use the latest and greatest Python, and secondly since Python 2 is end-of-lifed in 9 days' time.
Then you need to decide if you want to use the Apple-supplied Python, in /usr/bin
or the homebrew supplied Python. As you have installed homebrew Python, I would advocate using the homebrew one because you can delete the whole thing and re-install it if you mess it up while all the Apple-supplied patches, OS upgrades and their other uses of their Python will remain unaffected. Happy days!
So, you want to use homebrew Python 3. Now check what homebrew tells you, by running:
brew info python
Unversioned symlinks
python
,python-config
,pip
etc. pointing topython3
,python3-config
,pip3
etc., respectively, have been installed into /usr/local/opt/python/libexec/bin
That means if you want to run python
and pip
(without a version number, as opposed to python3
and pip3
) and expect that to start Python 3 and its corresponding pip
, you need to put /usr/local/opt/python/libexec/bin
at the start of your PATH in your bash profile
(probably $HOME/.bash_profile
):
export PATH=/usr/local/opt/python/libexec/bin:$PATH
Then start a new Terminal and check which Python runs when you type python
:
type python
And it must report as follows if you are on the right track:
python is /usr/local/opt/python/libexec/bin/python
Then, you can check which Python version that is with:
python -V
and it should be:
Python 3.7.5
Now you can install OpenCV with:
pip install opencv-python
Then load it in the interpreter with:
import cv2
If, for some reason, you want to run the old v2.7 Python, or risk messing up the installation of your macOS by installing clashing modules, just disregard my answer.
这篇关于安装并运行 Mac Python Open CV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!