How To Install OpenCV for Python?

OpenCV is a Python library used to process images which also provides computer vision and machine learning. The OpenCV is a 3rd party library where it is not provided via Python installation by default. By using OpenCV text, face, and plate recognition, extracting humans, detecting suspicious objects are very easy to implement. The OpenCV library can be installed for Python via the package managers for Linux and pip command for Linux and Windows operating systems.

Install OpenCV with pip Command for Python2

The OpenCV can be installed by using the pip command for Python2. The OpenCV package is named as opencv-python .

$ sudo pip install opencv-python

Install OpenCV with pip3 Command for Python3

The OpenCV can be also installed for Python3. The pip3 command can be used to install OpenCV for Python3. The OpenCV python package is named as opencv-python .

$ sudo pip3 install opencv-python
Install OpenCV with pip3 Command for Python3

Upgrade OpenCV with pip Command

The already install OpenCV package can be updated by using the pip and pip3 commands. Use pip or pip3 commands with the upgrade option to update existing OpenCV installation.

$ sudo pip upgrade opencv-python
$ sudo pip3 upgrade opencv-python

Install OpenCV with apt Package Manager

The OpenCV can be also installed via different package managers. For the deb based distributions like Debian, Ubuntu, Mint, or Kali the package name is python3-opencv . The apt command is used to install python3-opencv package.

$ sudo apt install python3-opencv

Upgrade OpenCV with apt Package Manager

The installed OpenCV package can be updated using the apt package manager like below. Keep in mind that the OpenCV should be installed with apt package manager previously.

$ sudo apt update 
$ sudo apt dist-upgrade

Importing and Using OpenCV

The OpenCV library is named as cv2 and should be imported with the same name. In the following example, we import the OpenCV library with the cv2 name and then print the OpenCV version.

import cv2

print(cv2.__version__)
Importing and Using OpenCV

Leave a Comment