How to install OpenCV in Python on Ubuntu 20.04?

OpenCV is Open Source Computer Vision Library. It mainly aimed at real time computer vision. OpenCV is a cross-platform and can use free under the open-source Apache 2 License. Let’s see How to install OpenCV in Python on Ubuntu 20.04?

Supporting Operating Systems

OpenCV runs on Windows, Linux, macOS, OpenBSD, FreeBSD, NetBSD and on mobile operating systems i.e. Android, IOS, Blackberry.

For latest releases of OpenCV, GitHub and SourceForge are the best sources. OpenCV uses CMake.

Step By Step Installation

If you have any other previous version of OpenCV then remove it prior to start installation to avoid conflicts.

Run: 
   $ pip3 uninstall opencv
   (for Python3)
or
   $ pip uninstall opencv
   (for Python2)

Make sure pip3 (in case of Python3 on Linux) or pip (in case of Python2 on Linux) should be updated.

(Note: I am going to use pip3 for the current installation guide. Whilst the users with Python2 should use pip)

Run:
   $ pip3 install --upgrade pip3
or
   $ pip install --upgrade pip

There are four options for standard packages of OpenCV of your choice as

  • Only main module package
  • Full package containing main modules and contrib/extra modules
  • Headless main modules
  • Headless full package containing both main modules and contrib/extra modules
  For main module, run:
     $ pip3 install opencv-python
  or 
  For full package (recommended and mostly used to install on Ubuntu/Windows/macOS)
     $ pip3 install opencv-contrib-python

Please note that all the headless packages are running on servers directly after installation such as on Docker and other cloud environments. These headless version have no GUI for OpenCV i.e. all based on command line.

For AI research in modern fashion, Caer is a lightweight and higher performance python library which is GPU accelerated. It provides maximum flexibility and speed to use OpenCV.

  Run:
      $ pip3 install caer

Video containing the installation of OpenCV and Caer Python packages.

Version Check

In Jupyter Notebook or in Python shell run the following to check if it works:

>> import cv2 
>> print(cv2.__version__)

>>import caer
>> print(caer.__version__)

if there is no error then installation setup is successful.

Conclusion

Hence, OpenCV is ready now to use in Python IDEs or in Jupyter Notebook by importing cv2 and caer.

Leave a Reply