python3切换cv2版本(python-opencv)

【背景】用源码编译安装的opencv-python 3.4.2 ( https://blog.csdn.net/qxqxqzzz/article/details/100730688)

突然某天版本变成了 4.1.1:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.1'
>>> cv2.__file__
'/home/xxx/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so'

原因可能是PYTHONPATH变量被修改,或者安装了opencv-python-headless,imgaug等软件,或者安装其他软件过程中自动升级了。但我不想升级,升级之后有些api不能使用(如:https://blog.csdn.net/qxqxqzzz/article/details/101201844)。所以改回原来版本:

【环境】

$ sudo lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial
$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

【切换前:安装的软件列表】 注意其中的

opencv-python                 4.1.1.26
opencv-python-headless        4.1.1.26

$ pip list
Package                       Version
----------------------------- -----------
absl-py                       0.8.0
apturl                        0.5.2
astor                         0.8.0
attrs                         19.3.0
backcall                      0.1.0
bcolz                         1.2.1
beautifulsoup4                4.4.1
bleach                        3.1.0
blinker                       1.3
Bottleneck                    1.2.1
Brlapi                        0.6.4
certifi                       2019.9.11
chardet                       2.3.0
checkbox-support              0.22
command-not-found             0.3
cryptography                  1.2.3
cycler                        0.10.0
Cython                        0.29.13
decorator                     4.4.0
defer                         1.0.6
defusedxml                    0.6.0
descartes                     1.1.0
entrypoints                   0.3
fastai                        0.7.0
feather-format                0.4.0
feedparser                    5.1.3
gast                          0.2.2
google-pasta                  0.1.7
graphviz                      0.13
grpcio                        1.24.1
guacamole                     0.9.2
h5py                          2.10.0
html5lib                      0.999
httplib2                      0.9.1
idna                          2.0
imageio                       2.5.0
imgaug                        0.3.0
importlib-metadata            0.23
ipykernel                     5.1.3
ipython                       7.8.0
ipython-genutils              0.2.0
ipywidgets                    7.5.1
isoweek                       1.3.3
jedi                          0.15.1
Jinja2                        2.8
joblib                        0.13.2
jsonschema                    3.1.1
jupyter                       1.0.0
jupyter-client                5.3.4
jupyter-console               6.0.0
jupyter-core                  4.6.1
Keras-Applications            1.0.8
Keras-Preprocessing           1.1.0
kiwisolver                    1.1.0
language-selector             0.1
louis                         2.6.4
lxml                          3.5.0
Mako                          1.0.3
Markdown                      3.1.1
MarkupSafe                    0.23
matplotlib                    3.0.3
mistune                       0.8.4
mizani                        0.5.4
more-itertools                7.2.0
nbconvert                     5.6.1
nbformat                      4.4.0
networkx                      2.3
notebook                      6.0.1
numpy                         1.17.3
oauthlib                      1.0.3
onboard                       1.2.0
opencv-python                 4.1.1.26
opencv-python-headless        4.1.1.26
opt-einsum                    3.1.0
padme                         1.1.1
palettable                    3.3.0
pandas                        0.24.2
pandas-summary                0.0.7
pandocfilters                 1.4.2
parso                         0.5.1
patsy                         0.5.1
pexpect                       4.0.1
pickleshare                   0.7.5
Pillow                        6.2.1
pip                           19.2.3
plainbox                      0.25
plotnine                      0.5.1
progressbar                   2.5
progressbar2                  3.46.1
prometheus-client             0.7.1
prompt-toolkit                2.0.9
protobuf                      3.10.0
ptyprocess                    0.5
pyarrow                       0.15.0
pyasn1                        0.1.9
pycups                        1.9.73
pycurl                        7.43.0
Pygments                      2.4.2
pygobject                     3.20.0
PyJWT                         1.3.0
pyparsing                     2.0.3
pypng                         0.0.20
pyrsistent                    0.15.5
python-apt                    1.1.0b1
python-dateutil               2.8.0
python-debian                 0.1.27
python-systemd                231
python-utils                  2.3.0
pytz                          2014.10
PyWavelets                    1.0.3
pyxdg                         0.25
PyYAML                        5.1.2
pyzmq                         18.1.0
qtconsole                     4.5.5
reportlab                     3.3.0
requests                      2.9.1
scikit-image                  0.15.0
scikit-learn                  0.21.3
scipy                         1.3.1
seaborn                       0.9.0
Send2Trash                    1.5.0
sessioninstaller              0.0.0
setuptools                    39.1.0
Shapely                       1.6.4.post2
simplegeneric                 0.8.1
six                           1.12.0
sklearn                       0.0
sklearn-pandas                1.8.0
ssh-import-id                 5.5
statsmodels                   0.10.1
system-service                0.3
tensorboard                   1.10.0
tensorboardX                  1.8
tensorflow-estimator          2.0.0
tensorflow-gpu                1.10.0
termcolor                     1.1.0
terminado                     0.8.2
testpath                      0.4.2
torch                         0.3.1
torchtext                     0.4.0
torchvision                   0.2.2.post3
tornado                       6.0.3
tqdm                          4.36.1
traitlets                     4.3.2
ubuntu-drivers-common         0.0.0
ufw                           0.35
unattended-upgrades           0.1
unity-scope-calculator        0.1
unity-scope-chromiumbookmarks 0.1
unity-scope-colourlovers      0.1
unity-scope-devhelp           0.1
unity-scope-firefoxbookmarks  0.1
unity-scope-gdrive            0.7
unity-scope-manpages          0.1
unity-scope-openclipart       0.1
unity-scope-texdoc            0.1
unity-scope-tomboy            0.1
unity-scope-virtualbox        0.1
unity-scope-yelp              0.1
unity-scope-zotero            0.1
urllib3                       1.13.1
usb-creator                   0.3.0
virtualenv                    15.0.1
wcwidth                       0.1.7
webencodings                  0.5.1
Werkzeug                      0.16.0
wheel                         0.29.0
widgetsnbextension            3.5.1
wrapt                         1.11.2
xdiagnose                     3.8.4
xkit                          0.0.0
XlsxWriter                    0.7.3
zipp                          0.6.0

【切换之前:PYTHONPATH环境变量】

$ echo $PYTHONPATH
/home/yangxxx/software/caffe/caffe/python:/usr/lib/python2.7/dist-packages/numpy_new:

【切换之前python3的安装路径及包路径】分别是

/usr/bin/python3

/home/xxx/.local/lib/python3.5/site-packages

$ whereis python3
python3: /usr/bin/python3 /usr/bin/python3.5 /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python3.5m /usr/lib/python3 /usr/lib/python3.5 /etc/python3 /etc/python3.5 /usr/local/lib/python3.5 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python3 /usr/share/man/man1/python3.1.gz

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

# 任意import一个包试试看site-packages所在目录
>>> import numpy
>>> numpy.__file__
'/home/xxx/.local/lib/python3.5/site-packages/numpy/__init__.py'

【切换之前】此时通过pkg-config能看到原来安装的opencv:

$ pkg-config opencv --modversion
3.4.2

说明安装的老版本opencv没有丢。根据原帖(https://blog.csdn.net/qxqxqzzz/article/details/100730688),opencv 3.4.2安装在 

/usr/local/lib/  下(不确定的话可以进入该目录,看是否有libopencv_aruco.so.3.4.2,libopencv_core.so.3.4.2等一系列文件 )。下面开始切换回老版本

【切换方法】

# 修改PYTHONPATH
$ sudo vi ~/.bashrc

# 加入以下内容:
export PYTHONPATH=/usr/bin/python3:/usr/local/lib:/home/xxx/.local/lib/python3.5/site-packages # 分别是 python3安装目录,opencv安装目录,python包所在目录。不确定这样设置对切换cv2版本一定有用,仅供参考
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig # “/usr/local/lib/ ” 部分根据自己的opencv-python安装目录确定
export LD_LIBRARY_PATH=/usr/local/lib # “/usr/local/lib/ ” 部分根据自己的opencv-python安装目录确定

# :wq 回车保存

# 使刚才的修改生效
$ source ~/.bashrc

# 卸载 opencv-python-headless, 这个是关键!!!!
$ pip3 uninstall opencv-python-headless

【切换之后】恭喜!老版本回来了!

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
cv>>> cv2.__version__
'3.4.2'
>>> cv2.__file__
'/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so' # 和之前不一样了!!

如上所示,此时引用的cv2路径也发生了变化!

【切换之后】查看PYTHONPATH

$ echo $PYTHONPATH
/usr/bin/python3:/usr/local/lib:/home/XXX/.local/lib/python3.5/site-packages

查看软件安装列表:

虽然列表里的opencv-python还是4.1.1版本,但是python3中的cv2版本已经切换回3.4.2老版本。

$ pip3 list
Package                       Version
----------------------------- -----------
absl-py                       0.8.0
apturl                        0.5.2
astor                         0.8.0
attrs                         19.3.0
backcall                      0.1.0
bcolz                         1.2.1
beautifulsoup4                4.4.1
bleach                        3.1.0
blinker                       1.3
Bottleneck                    1.2.1
Brlapi                        0.6.4
certifi                       2019.9.11
chardet                       2.3.0
checkbox-support              0.22
command-not-found             0.3
cryptography                  1.2.3
cycler                        0.10.0
Cython                        0.29.13
decorator                     4.4.0
defer                         1.0.6
defusedxml                    0.6.0
descartes                     1.1.0
entrypoints                   0.3
fastai                        0.7.0
feather-format                0.4.0
feedparser                    5.1.3
gast                          0.2.2
google-pasta                  0.1.7
graphviz                      0.13
grpcio                        1.24.1
guacamole                     0.9.2
h5py                          2.10.0
html5lib                      0.999
httplib2                      0.9.1
idna                          2.0
imageio                       2.5.0
imgaug                        0.3.0
importlib-metadata            0.23
ipykernel                     5.1.3
ipython                       7.8.0
ipython-genutils              0.2.0
ipywidgets                    7.5.1
isoweek                       1.3.3
jedi                          0.15.1
Jinja2                        2.8
joblib                        0.13.2
jsonschema                    3.1.1
jupyter                       1.0.0
jupyter-client                5.3.4
jupyter-console               6.0.0
jupyter-core                  4.6.1
Keras-Applications            1.0.8
Keras-Preprocessing           1.1.0
kiwisolver                    1.1.0
language-selector             0.1
louis                         2.6.4
lxml                          3.5.0
Mako                          1.0.3
Markdown                      3.1.1
MarkupSafe                    0.23
matplotlib                    3.0.3
mistune                       0.8.4
mizani                        0.5.4
more-itertools                7.2.0
nbconvert                     5.6.1
nbformat                      4.4.0
networkx                      2.3
notebook                      6.0.1
numpy                         1.17.3
oauthlib                      1.0.3
onboard                       1.2.0
opencv-python                 4.1.1.26
opt-einsum                    3.1.0
padme                         1.1.1
palettable                    3.3.0
pandas                        0.24.2
pandas-summary                0.0.7
pandocfilters                 1.4.2
parso                         0.5.1
patsy                         0.5.1
pexpect                       4.0.1
pickleshare                   0.7.5
Pillow                        6.2.1
pip                           19.2.3
plainbox                      0.25
plotnine                      0.5.1
progressbar                   2.5
progressbar2                  3.46.1
prometheus-client             0.7.1
prompt-toolkit                2.0.9
protobuf                      3.10.0
ptyprocess                    0.5
pyarrow                       0.15.0
pyasn1                        0.1.9
pycups                        1.9.73
pycurl                        7.43.0
Pygments                      2.4.2
pygobject                     3.20.0
PyJWT                         1.3.0
pyparsing                     2.0.3
pypng                         0.0.20
pyrsistent                    0.15.5
python-apt                    1.1.0b1
python-dateutil               2.8.0
python-debian                 0.1.27
python-systemd                231
python-utils                  2.3.0
pytz                          2014.10
PyWavelets                    1.0.3
pyxdg                         0.25
PyYAML                        5.1.2
pyzmq                         18.1.0
qtconsole                     4.5.5
reportlab                     3.3.0
requests                      2.9.1
scikit-image                  0.15.0
scikit-learn                  0.21.3
scipy                         1.3.1
seaborn                       0.9.0
Send2Trash                    1.5.0
sessioninstaller              0.0.0
setuptools                    39.1.0
Shapely                       1.6.4.post2
simplegeneric                 0.8.1
six                           1.12.0
sklearn                       0.0
sklearn-pandas                1.8.0
ssh-import-id                 5.5
statsmodels                   0.10.1
system-service                0.3
tensorboard                   1.10.0
tensorboardX                  1.8
tensorflow-estimator          2.0.0
tensorflow-gpu                1.10.0
termcolor                     1.1.0
terminado                     0.8.2
testpath                      0.4.2
torch                         0.3.1
torchtext                     0.4.0
torchvision                   0.2.2.post3
tornado                       6.0.3
tqdm                          4.36.1
traitlets                     4.3.2
ubuntu-drivers-common         0.0.0
ufw                           0.35
unattended-upgrades           0.1
unity-scope-calculator        0.1
unity-scope-chromiumbookmarks 0.1
unity-scope-colourlovers      0.1
unity-scope-devhelp           0.1
unity-scope-firefoxbookmarks  0.1
unity-scope-gdrive            0.7
unity-scope-manpages          0.1
unity-scope-openclipart       0.1
unity-scope-texdoc            0.1
unity-scope-tomboy            0.1
unity-scope-virtualbox        0.1
unity-scope-yelp              0.1
unity-scope-zotero            0.1
urllib3                       1.13.1
usb-creator                   0.3.0
virtualenv                    15.0.1
wcwidth                       0.1.7
webencodings                  0.5.1
Werkzeug                      0.16.0
wheel                         0.29.0
widgetsnbextension            3.5.1
wrapt                         1.11.2
xdiagnose                     3.8.4
xkit                          0.0.0
XlsxWriter                    0.7.3
zipp                          0.6.0

【致谢】

https://blog.csdn.net/qxqxqzzz/article/details/102790135

https://blog.csdn.net/lch_vison/article/details/79112693

你可能感兴趣的:(Python,#,CV-基础,#,DL-基础)