1. 參考網路分享資料,安裝OpenCV
2. 裝好後,在Python 下,import cv2時,可能出現以下錯誤:
(py3cv4) pi@ath-rpi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named 'cv2'
>>>
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "
ImportError: No module named 'cv2'
>>>
很多教導安裝OpenCV的文章皆指出需在"/usr/local/lib/python3.5/site-packages/",出現cv2.so。我的RPI上,的確有這個檔案。
以python的錯誤訊息上網搜尋,推測可能是路徑參數不正確。先"import sys", 再執行"sys.path",可列出目前的python搜尋目錄:
(py3cv4) pi@ath-rpi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/pi/.virtualenvs/py3cv4/lib/python35.zip', '/home/pi/.virtualenvs/py3cv4/lib/python3.5', '/home/pi/.virtualenvs/py3cv4/lib/python3.5/plat-arm-linux-gnueabihf', '/home/pi/.virtualenvs/py3cv4/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/home/pi/.virtualenvs/py3cv4/lib/python3.5/site-packages']
>>>
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/pi/.virtualenvs/py3cv4/lib/python35.zip', '/home/pi/.virtualenvs/py3cv4/lib/python3.5', '/home/pi/.virtualenvs/py3cv4/lib/python3.5/plat-arm-linux-gnueabihf', '/home/pi/.virtualenvs/py3cv4/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-arm-linux-gnueabihf', '/home/pi/.virtualenvs/py3cv4/lib/python3.5/site-packages']
>>>
很奇怪,沒有"/usr/local/lib/python3.5/site-packages/",但有"/home/pi/.virtualenvs/py3cv4/lib/python3.5/site-packages",所以我的解決方式是在後者下ln一個連結,即可。
(py3cv4) pi@ath-rpi:~/.virtualenvs/py3cv4/lib/python3.5/site-packages $ ln /usr/local/lib/python3.5/site-packages/cv2.so ./
(py3cv4) pi@ath-rpi:~/.virtualenvs/py3cv4/lib/python3.5/site-packages $ ls
cv2.so pip setuptools
easy_install.py pip-18.1.dist-info setuptools-40.6.2.dist-info
numpy pkg_resources wheel
numpy-1.15.4.dist-info __pycache__ wheel-0.32.2.dist-info
(py3cv4) pi@ath-rpi:~/.virtualenvs/py3cv4/lib/python3.5/site-packages $ ls
cv2.so pip setuptools
easy_install.py pip-18.1.dist-info setuptools-40.6.2.dist-info
numpy pkg_resources wheel
numpy-1.15.4.dist-info __pycache__ wheel-0.32.2.dist-info
安裝教學文章內的最後一個驗證動作,正常出現了!
(py3cv4) pi@ath-rpi:~ $ python
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.0.0-alpha'
>>>
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.0.0-alpha'
>>>
參考資料: