Python 2.x 环境下卸载 OpenCV 2.x 安装 OpenCV 3.x
https://pypi.org/project/opencv-python/
1. OpenCV on Wheels
Unofficial pre-built OpenCV packages for Python.
2. Installation and Usage
-
If you have previous/other manually installed (= not installed via pip) version of OpenCV installed (e.g. cv2 module in the root of Python’s site-packages), remove it before installation to avoid conflicts.
-
Select the correct package for your environment:
There are four different packages and you should select only one of them. Do not install multiple different packages in the same environment. There is no plugin architecture: all the packages use the same namespace (cv2
). If you installed multiple different packages in the same environment, uninstall them all with pip uninstall
and reinstall only one package.
有四种不同的包,您应该只选择其中一种。不要在同一环境中安装多个不同的软件包。没有插件架构:所有软件包都使用相同的命名空间 (cv2
)。如果您在同一环境中安装了多个不同的软件包,请使用 pip uninstall
卸载它们并仅重新安装一个软件包。
a. Packages for standard desktop environments (Windows, macOS, almost any GNU/Linux distribution) (桌面环境)
- run
pip install opencv-python
if you need only main modules - run
pip install opencv-contrib-python
if you need both main and contrib modules (check extra modules listing from OpenCV documentation)
b. Packages for server (headless) environments (服务器环境)
These packages do not contain any GUI functionality. They are smaller and suitable for more restricted environments.
这些包不包含任何 GUI 功能。它们更小,适用于更受限制的环境。
- run
pip install opencv-python-headless
if you need only main modules - run
pip install opencv-contrib-python-headless
if you need both main and contrib modules (check extra modules listing from OpenCV documentation)
Since all packages use the same cv2
namespace explained above, uninstall the other package before switching for example from opencv-python
to opencv-contrib-python
.
由于所有包都使用上面解释的相同 cv2
命名空间,因此在切换之前卸载另一个包,例如从 opencv-python
切换到 opencv-contrib-python
。
- Import the package:
import cv2
All packages contain haarcascade files. cv2.data.haarcascades
can be used as a shortcut to the data folder. For example:
cv2.CascadeClassifier(cv2.data.haarcascades + “haarcascade_frontalface_default.xml”)
- Read OpenCV documentation
https://docs.opencv.org/master/
wheel [wiːl]:n. 车轮,方向盘,转动 vt. 转动,使变换方向,给...装轮子 vi. 旋转,突然转变方向,盘旋飞行
headless ['hedlɪs]:adj. 不在意的,无头脑的,无领导者的
3. Python 2.x 环境下卸载 OpenCV 2.x 安装 OpenCV 3.x
3.1 Python 2.x 环境下卸载 OpenCV 2.x
strong@foreverstrong:~$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'2.4.9.1'
>>> exit()
strong@foreverstrong:~$
strong@foreverstrong:~$ pip list | grep opencv
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
warnings.warn(warning, RequestsDependencyWarning)
You are using pip version 18.0, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
strong@foreverstrong:~$
strong@foreverstrong:~$ pip3 list | grep opencv
opencv-contrib-python (3.3.0.10)
opencv-python (3.4.0.12)
You are using pip version 8.1.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
strong@foreverstrong:~$
strong@foreverstrong:~$ pkg-config --modversion opencv
2.4.9.1
strong@foreverstrong:~$
To install the latest version of OpenCV be sure that you have removed the library from the repository with sudo apt-get autoremove libopencv-dev python-opencv
and follow the steps below.
sudo apt-get autoremove python-opencv
strong@foreverstrong:~$ sudo apt-get autoremove python-opencv
[sudo] password for strong:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
python-opencv
0 upgraded, 0 newly installed, 1 to remove and 620 not upgraded.
After this operation, 1,383 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 317156 files and directories currently installed.)
Removing python-opencv (2.4.9.1+dfsg-1.5ubuntu1.1) ...
strong@foreverstrong:~$
strong@foreverstrong:~$ sudo apt-get autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 620 not upgraded.
strong@foreverstrong:~$
3. Python 2.x 环境下安装 OpenCV 3.x
pip install opencv-python==3.4.1.15
pip install opencv-python==3.4.3.18
strong@foreverstrong:~$ sudo pip2 install opencv-python==3.4.1.15
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
warnings.warn(warning, RequestsDependencyWarning)
The directory '/home/strong/.cache/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.
The directory '/home/strong/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting opencv-python==3.4.1.15
Downloading https://files.pythonhosted.org/packages/0f/87/46f41c6673106030d835342e65133085c22cb36549a9ab883bb3cd3f00bb/opencv_python-3.4.1.15-cp27-cp27mu-manylinux1_x86_64.whl (24.9MB)
100% |████████████████████████████████| 24.9MB 1.7MB/s
Requirement already satisfied: numpy>=1.11.1 in /usr/local/lib/python2.7/dist-packages (from opencv-python==3.4.1.15) (1.16.3)
Installing collected packages: opencv-python
Successfully installed opencv-python-3.4.1.15
strong@foreverstrong:~$
strong@foreverstrong:~$ pip list | grep opencv
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
warnings.warn(warning, RequestsDependencyWarning)
opencv-python 3.4.1.15
You are using pip version 18.0, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
strong@foreverstrong:~$
strong@foreverstrong:~$ pip3 list | grep opencv
opencv-contrib-python (3.3.0.10)
opencv-python (3.4.0.12)
You are using pip version 8.1.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
strong@foreverstrong:~$
strong@foreverstrong:~$ pkg-config --modversion opencv
2.4.9.1
strong@foreverstrong:~$
strong@foreverstrong:~$ python
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.1'
>>> exit()
strong@foreverstrong:~$
4. Supported Python versions
Python 2.7 is the only supported version in 2.x series. Python 3.x releases follow Numpy releases. For example Python 3.3 is no longer supported by Numpy so support for it has been dropped in opencv-python
, too.
Python 2.7 是 2.x 系列中唯一受支持的版本。Python 3.x 版本遵循 Numpy 版本。例如,Numpy 不再支持 Python 3.3,因此对它的支持也已经在 opencv-python
中删除了。