Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

macos - Install and run Mac Python Open CV

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 to python3, 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...