macOS: 安装OpenCV 3.2.0 和Python 3.6.0

2016年12月,OpenCV 3.2.0和Python 3.6.0发布,本文记述的是笔者在macOS10.12.5和Python 3.6.0的环境下,配置OpenCV 3.2.0的过程。坑还是有一些,花了半天的时间。这里把我解决的方案贴出来,仅供交流参考。
由于之前用的是Python 2.7.12和OpenCV 2.4.13的环境。而实际上,py3比py2更代表py的发展方向,所以很早我就想把之换过来了。这里关于Python 3.6.0的安装不再赘述,假定系统也更新到macOS10.12.5。
其实,整个过程就一句命令:brew install opencv3 --with-python3,但当我无数次在终端输入这句命令行的时候,让我绝望了无数次。

marlondeMBP:usr marlon$ brew install opencv3 --with-python3
==> Installing opencv3 from homebrew/science
==> Installing dependencies for homebrew/science/opencv3: gmp, libmpc, isl, gcc, cmake, pkg-config, eigen, libpng, libtiff, numpy
Error: Cannot link gmp
Another version is already linked: /usr/local/Cellar/gmp/6.1.1

gmp, libmpc, isl, gcc, cmake, pkg-config, eigen, libpng, libtiff, numpy这些都是安装OpenCV 3.2.0的依赖包。出现这个问题的原因是gmp的另一个版本(6.1.1)已经被链接,所以不能链接到gmp。解决的方案是,把gmp6.1.1及其链接删除。依此类推,删除上述其他依赖包及其链接。
然后,继续……

marlondeMBP:usr marlon$ brew install opencv3 --with-python3
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
awscli                                  conan                                   git-town                                lmdb                                    notmuch

==> Installing opencv3 from homebrew/science
==> Downloading https://github.com/opencv/opencv/archive/3.2.0.tar.gz
==> Downloading from https://codeload.github.com/opencv/opencv/tar.gz/3.2.0
######################################################################## 100.0%
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.
==> Trying a full download
==> Downloading from https://codeload.github.com/opencv/opencv/tar.gz/3.2.0
######################################################################## 100.0%
Error: opencv3: Does not support building both Python 2 and 3 wrappers

这个错误是因为opencv3不能同时绑定Python 2 and 3。解决的方案是,修改opencv3.rb。
在命令行中输入:brew edit opencv3,并找到以下四句代码(187-190行),把之注释掉。

if build.with?("python3") && build.with?("python")
  # Opencv3 Does not support building both Python 2 and 3 versions
  odie "opencv3: Does not support building both Python 2 and 3 wrappers"
end

注释掉之后变成:

# if build.with?("python3") && build.with?("python")
  # Opencv3 Does not support building both Python 2 and 3 versions
  # odie "opencv3: Does not support building both Python 2 and 3 wrappers"
# end

然后保存并退出编辑。
然后,继续…….

marlondeMBP:~ marlon$ brew install opencv3 --with-contrib --with-python3 --HEAD
Updating Homebrew...
==> Auto-updated Homebrew!
Updated Homebrew from daa67886 to ce80485a.
No changes to formulae.

==> Installing opencv3 from homebrew/science
==> Cloning https://github.com/opencv/opencv.git
Updating /Users/marlon/Library/Caches/Homebrew/opencv3--git
==> Checking out branch master
==> Cloning https://github.com/opencv/opencv_contrib.git
Updating /Users/marlon/Library/Caches/Homebrew/opencv3--contrib--git
==> Checking out branch master
Error: No such file or directory - /private/tmp/opencv3-20170602-12177-kl3ixn/3rdparty/ippicv/downloader.cmake

这是因为:3rdparty/ippicv/downloader.cmake文件被移走了!解决的方案是把–HEAD去掉,然后,继续……

marlondeMBP:~ marlon$ brew install opencv3 --with-contrib --with-python3
==> Installing opencv3 from homebrew/science
==> Downloading https://github.com/opencv/opencv/archive/3.2.0.tar.gz
Already downloaded: /Users/marlon/Library/Caches/Homebrew/opencv3-3.2.0.tar.gz
==> Downloading https://github.com/opencv/opencv_contrib/archive/3.2.0.tar.gz
Already downloaded: /Users/marlon/Library/Caches/Homebrew/opencv3--contrib-3.2.0.tar.gz
==> Downloading https://raw.githubusercontent.com/opencv/opencv_3rdparty/81a676001ca8075ada498583e4166079e5744668/ippicv/ippicv_macosx_20151201.tgz
Already downloaded: /Users/marlon/Library/Caches/Homebrew/opencv3--icv-macosx-20151201.tgz
==> cmake .. -DCMAKE_C_FLAGS_RELEASE=-DNDEBUG -DCMAKE_CXX_FLAGS_RELEASE=-DNDEBUG -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/opencv3/3.2.0 -DCMAKE_BUILD_TYPE=Release -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_VER
==> make
==> make install
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because opencv3 and opencv install many of the same files..

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/opencv3/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/opencv3/lib
    CPPFLAGS: -I/usr/local/opt/opencv3/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/opencv3/lib/pkgconfig


If you need Python to find bindings for this keg-only formula, run:
  echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
  mkdir -p /Users/marlon/.local/lib/python3.6/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/marlon/.local/lib/python3.6/site-packages/homebrew.pth
==> Summary
  /usr/local/Cellar/opencv3/3.2.0: 504 files, 66.8MB, built in 8 minutes 20 seconds

妈呀!终于成功了!但是……

marlondeMBP:~ marlon$ python
Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

为什么仍然找不到cv2的模块,原来是没有把OpenCV 3.2.0链接到系统默认的Python版本中。这里需要指定一个相互关联的路径,在终端中输入:

marlondeMBP:~ marlon$ echo /usr/local/opt/opencv3/lib/python3.6/site-packages >> /usr/local/lib/python3.6/site-packages/opencv3.pth

然而,还是木有成功啊……

marlondeMBP:~ marlon$ python
Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

仔细一看,原来笔者python3.6在anaconda中(因为是通过anaconda安装的python3.6),所以,应该更改关联路径:

marlondeMBP:~ marlon$ echo /usr/local/opt/opencv3/lib/python3.6/site-packages >>/Users/marlon/anaconda/lib/python3.6/site-packages/opencv3.pth
marlondeMBP:~ marlon$ python
Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'

这下,真的成功了!My God!
当然,这个过程中,还遇到其他各种各样小的问题,在这里不再一一赘述,欢迎探讨!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值