Mac上安装python-opencv

如何正确安装OpenCV历来是一个堪称玄学的问题,在成功安装OpenCV的道路上经历了种种艰辛,这真的是我最恶心的一次安装经历。下载源码并编译的方法就不说了,下面介绍较简易的使用Homebrew安装的方法。

第一步:安装OpenCV

直接使用命令brew install opencv是不行的,因为homebrew找不到它,会产生如下报错:

1
2
3
4
5
6
7
8
9
10
11
12
➜ ~ brew install opencv
Error: No available formula with the name "opencv"
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
These formulae were found in taps:
homebrew/science/opencv homebrew/science/opencv3
To install one of them, run (for example):
brew install homebrew/science/opencv
==> You haven't updated Homebrew in a while.
A formula for opencv might have been added recently.
Run `brew update` to get the latest Homebrew updates!

你需要先将opencv所在的science分类加入homebrew的搜索列表中再尝试下载

1
2
$ brew tap homebrew/science
$ brew install opencv

请留意安装过程中的输出信息,最好保存一下,之后可能有用,确保opencv正确安装后就可以删掉了。

安装过程中自动为你安装好了以下库:

1
2
3
4
5
6
7
8
9
10
11
==> Installing dependencies for homebrew/science/opencv: eigen, ilmbase,
==> Installing homebrew/science/opencv dependency: eigen
🍺 /usr/local/Cellar/eigen/3.2.8: 368 files, 3.5M
==> Installing homebrew/science/opencv dependency: ilmbase
🍺 /usr/local/Cellar/ilmbase/2.2.0: 362 files, 5.6M
==> Installing homebrew/science/opencv dependency: openexr
🍺 /usr/local/Cellar/openexr/2.2.0: 131 files, 11.4M
==> Installing homebrew/science/opencv dependency: homebrew/python/numpy
🍺 /usr/local/Cellar/numpy/1.11.1: 430 files, 9M
==> Installing homebrew/science/opencv
🍺 /usr/local/Cellar/opencv/2.4.13: 276 files, 35.8M

其中已为你安装好numpy,其版本为1.11.1,如果你此前已经通过pip安装过numpy了,那需要很多额外步骤。

进入python环境,测试opencv是否正确安装:

1
2
3
4
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2

python找不到opencv,这就蛋疼了。翻看opencv的安装记录,有如下信息:

1
2
3
4
5
6
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/mac/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/mac/Library/Python/2.7/lib/python/site-packages/homebrew.pth

Homebrew的site-packages不在python的sys.path路径中,所以python无法导入安装好的opencv模块,如果想要python能够找到,执行下面的命令:

1
2
$ mkdir -p /Users/mac/Library/Python/2.7/lib/python/site-packages
$ echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/mac/Library/Python/2.7/lib/python/site-packages/homebrew.pth

再尝试import cv2,可能会有如下报错:

1
2
3
4
RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import

这是由于numpy版本过低导致的,可能是你系统中已经安装的numpy版本不能满足opencv的需求。并且没有提示找不到模块,所以python能正确找到opencv了。尝试sudo pip uninstall numpy,可能会有下面的报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip/commands/uninstall.py", line 76, in run
requirement_set.uninstall(auto_confirm=options.yes)
File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 336, in uninstall
req.uninstall(auto_confirm=auto_confirm)
File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 742, in uninstall
paths_to_remove.remove(auto_confirm)
File "/Library/Python/2.7/site-packages/pip/req/req_uninstall.py", line 115, in remove
renames(path, new_path)
File "/Library/Python/2.7/site-packages/pip/utils/__init__.py", line 267, in renames
shutil.move(old, new)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
copystat(src, dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-8Z9Jmh-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy-1.8.0rc1-py2.7.egg-info'
The directory '/Users/mac/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

并且,在使用sudo pip install matplotlib时也有类似的报错。可以改成sudo pip install --ignore-installed matplotlib试试。

这是由于System Integrity Protection导致的,需要关闭保护SIP,否则即时加了sudo也没用,尝试卸载和升级都需要关闭保护SIP。后面会介绍一种更简单的方法,这一段可略过。具体操作如下:

  1. 重启电脑,电脑启动的时候按住command+R
  2. 等画面上显示苹果logo后会看到[OS X 工具程式]窗口,选择终端,要把键盘按下去,不要轻按
  3. 终端打开后输入csrutil disable,之后重启

重启之后再升级或重装:

1
sudo pip install --upgrade numpy

另一种更简单的方式和安装opencv时一样隐藏在安装信息中,就在opencv路径信息的上一段里,执行命令:

1
echo 'import sys; sys.path.insert(1, "/usr/local/lib/python2.7/site-packages")' >> /Users/mac/Library/Python/2.7/lib/python/site-packages/homebrew.pth

由于mkdir -p /Users/mac/Library/Python/2.7/lib/python/site-packages之前已经创建过了,可以不再创建。执行完命令后,重启一下终端,再进入python环境测试是否能正确导入:

1
2
3
>>> import cv2
>>> import numpy
>>> from matplotlib import pyplot

本机异常已消除,若还有其他异常,请另行寻找解决方法。以下是本机安装的版本:

1
2
3
4
5
6
>>> print cv2.__version__
2.4.13
>>> print numpy.__version__
1.11.1
>>> print matplotlib.__version__
1.3.1

希望这篇文章能帮助你脱离玄学的道路。