很少說,直接上乾貨!html
說明:python
Anaconda2-5.0.0-Windows-x86_64.exe安裝下來,默認的Python2.7git
Anaconda3-4.2.0-Windows-x86_64.exe安裝下來,默認的Python3.5github
Anaconda3-5.0.0-Windows-x86_64.exe安裝下來,默認的Python3.6微信
參考session
一、 如下是在Windows下Anaconda2里正確下載安裝OpenCV(離線方式)機器學習
下載地址 ide
由於python裝的是Anaconda2版本,因此對應着選擇,就安裝post
https://www.lfd.uci.edu/~gohlke/pythonlibs/學習
PS C:\Anaconda2\Lib\site-packages> pip install opencv_python-2.4.13.2-cp27-cp27m-win_amd64.whl Processing c:\anaconda2\lib\site-packages\opencv_python-2.4.13.2-cp27-cp27m-win_amd64.whl Installing collected packages: opencv-python Successfully installed opencv-python-2.4.13.2 PS C:\Anaconda2\Lib\site-packages>
測試一下,輸python
進入python,輸入import cv2
回車,不報錯就說明安裝配置成功了
PS C:\Anaconda2\Lib\site-packages> python Python 2.7.13 |Anaconda, Inc.| (default, Sep 19 2017, 08:25:59) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import cv2 >>>
對於這種方式,我嘗試了,是能夠行得通的。
二、 如下是在Windows下Anaconda3里正確下載安裝OpenCV(離線方式)
由於python裝的是Anaconda3版本,因此對應着選擇,就安裝
PS C:\Anaconda3\Lib\site-packages> pip install opencv_python-3.3.1-cp36-cp36m-win_amd64.whl opencv_python-3.3.1-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform. PS C:\Anaconda3\Lib\site-packages>
若是還出現這個狀況,則換個版本試試。這個很簡單,嘗試這個版本
PS C:\Anaconda3\Lib\site-packages> pip install opencv_python-3.3.0-cp36-cp36m-win_amd64.whl Processing c:\anaconda3\lib\site-packages\opencv_python-3.3.0-cp36-cp36m-win_amd64.whl Installing collected packages: opencv-python Successfully installed opencv-python-3.3.0 PS C:\Anaconda3\Lib\site-packages>
成功!這樣的安裝方式是沒問題的。
import cv2成功。
三、如下是在Windows下Anaconda3裏的envs里正確下載安裝OpenCV(離線方式)
PS C:\Anaconda3\envs\python35\Lib\site-packages> pip install opencv_python-3.3.0-cp36-cp36m-win_amd64.whl Requirement already satisfied: opencv-python==3.3.0 from file:///C:/Anaconda3/envs/python35/Lib/site-packages/opencv_python-3.3.0-cp36-cp36m-win_amd64.whl in c:\anaconda3\lib\site-packages PS C:\Anaconda3\envs\python35\Lib\site-packages>
同樣的,由於這個envs環境是經過以下建立獲得的,因此,是寄生在Anaconda3裏。
這種方式,也是成功的。
若是你們中間有遇到以下的問題,則對應解決。
C:\Users\lenovo>python Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import cv2 RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: numpy.core.multiarray failed to import >>> >>> import numpy >>> exit() C:\Users\lenovo> C:\Users\lenovo>pip install numpy --upgrade Exception: Traceback (most recent call last): File "C:\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main status = self.run(options, args) File "C:\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run wb.build(autobuilding=True) File "C:\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build self.requirement_set.prepare_files(self.finder) File "C:\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files ignore_dependencies=self.ignore_dependencies)) File "C:\Anaconda3\lib\site-packages\pip\req\req_set.py", line 487, in _prepare_file req_to_install, finder) File "C:\Anaconda3\lib\site-packages\pip\req\req_set.py", line 428, in _check_skip_installed req_to_install, upgrade_allowed) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 465, in find_requirement all_candidates = self.find_all_candidates(req.name) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 423, in find_all_candidates for page in self._get_pages(url_locations, project_name): File "C:\Anaconda3\lib\site-packages\pip\index.py", line 568, in _get_pages page = self._get_page(location) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 683, in _get_page return HTMLPage.get_page(link, session=self.session) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 811, in get_page inst = cls(resp.content, resp.url, resp.headers) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 731, in __init__ namespaceHTMLElements=False, TypeError: parse() got an unexpected keyword argument 'transport_encoding' C:\Users\lenovo>
若是運行下來,仍是沒獲得解決,則
再嘗試下,
問題出現的緣由是numpy的版本較高,不能與opencv2兼容
解決辦法: pip install -U numpy
便可將numpy恢復到合適的版本(這裏爲1.9.2)
再 import cv2 就不會出現上面的問題了。
成功!
有時候,你可能得卸載Anaconda3裏已經安裝好了的opencv,再來安裝。(爲何呢?是由於你可能沒看仔細,下錯版本了)
成功!
一樣,對於若是你是Anaconda3-4.2.0-Windows-x86_64.exe安裝下來,默認的Python3.5,則以下
成功!
四、如下是在Windows下Anaconda3里正確下載安裝OpenCV(在線方式)(注意這裏有坑,必定要按照個人來)
(C:\Anaconda3) C:\Users\lenovo>pip install opencv-python Collecting opencv-python Cache entry deserialization failed, entry ignored Downloading opencv_python-3.3.0.10-cp35-cp35m-win_amd64.whl (39.7MB) 100% |████████████████████████████████| 39.7MB 25kB/s Requirement already satisfied (use --upgrade to upgrade): numpy>=1.11.1 in c:\anaconda3\lib\site-packages (from opencv-python) Installing collected packages: opencv-python Successfully installed opencv-python-3.3.0.10 You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (C:\Anaconda3) C:\Users\lenovo>
驗證下,出現以下的錯誤
(C:\Anaconda3) C:\Users\lenovo>pip install opencv-python Collecting opencv-python Cache entry deserialization failed, entry ignored Downloading opencv_python-3.3.0.10-cp35-cp35m-win_amd64.whl (39.7MB) 100% |████████████████████████████████| 39.7MB 25kB/s Requirement already satisfied (use --upgrade to upgrade): numpy>=1.11.1 in c:\anaconda3\lib\site-packages (from opencv-python) Installing collected packages: opencv-python Successfully installed opencv-python-3.3.0.10 You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (C:\Anaconda3) C:\Users\lenovo> (C:\Anaconda3) C:\Users\lenovo>python Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import cv3 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'cv3' >>> import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Anaconda3\lib\site-packages\cv2\__init__.py", line 9, in <module> from .cv2 import * ImportError: DLL load failed: 找不到指定的模塊。 >>>
嘗試解決
>>> import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Anaconda3\lib\site-packages\cv2\__init__.py", line 9, in <module> from .cv2 import * ImportError: DLL load failed: 找不到指定的模塊。 >>> pip install -U numpy File "<stdin>", line 1 pip install -U numpy ^ SyntaxError: invalid syntax >>> exit() (C:\Anaconda3) C:\Users\lenovo>pip install -U numpy Cache entry deserialization failed, entry ignored Collecting numpy Downloading numpy-1.13.3-cp35-none-win_amd64.whl (13.1MB) 100% |████████████████████████████████| 13.1MB 74kB/s Installing collected packages: numpy Found existing installation: numpy 1.11.1 Uninstalling numpy-1.11.1: Successfully uninstalled numpy-1.11.1 Successfully installed numpy-1.13.3 You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (C:\Anaconda3) C:\Users\lenovo> (C:\Anaconda3) C:\Users\lenovo>
能夠看到,若是你是直接這麼來安裝,是錯誤的。爲何呢?按照個人來,先卸載吧。
因而,我把它給卸載了,帶你們來從新作一遍。
解決辦法:
https://github.com/skvark/opencv-python/issues/36
什麼意思,很簡單。
(1)下載安裝 Visual C++ Redistributable for Visual Studio 2015 不難,別嫌麻煩。
(2) 把下載的python3.dll放到你所安裝Anaconda3的目錄下便可。
個人是這樣的。
而後,再嘗試pip install opencv-python執行完後,再import cv2就能夠成功了
成功!
五、也能夠直接在Pycharm裏搜索並安裝Opencv
這個很少贅述
總結
不少人寫的對於OpenCV都是冰山一角,其實沒有你們想的那麼複雜,只是沒有多嘗試罷了。
不管是Python2仍是Python3系列,均可以安裝,且離線和在線安裝成功。
提醒的是,注意對應本身的版本去安裝。
同時,你們能夠關注個人我的博客:
http://www.cnblogs.com/zlslch/ 和 http://www.cnblogs.com/lchzls/ http://www.cnblogs.com/sunnyDream/
詳情請見:http://www.cnblogs.com/zlslch/p/7473861.html
人生苦短,我願分享。本公衆號將秉持活到老學到老學習無休止的交流分享開源精神,匯聚於互聯網和我的學習工做的精華乾貨知識,一切來於互聯網,反饋回互聯網。
目前研究領域:大數據、機器學習、深度學習、人工智能、數據挖掘、數據分析。 語言涉及:Java、Scala、Python、Shell、Linux等 。同時還涉及日常所使用的手機、電腦和互聯網上的使用技巧、問題和實用軟件。 只要你一直關注和呆在羣裏,天天必須有收穫
對應本平臺的討論和答疑QQ羣:大數據和人工智能躺過的坑(總羣)(161156071)