Python and OpenCV are used to develop services and applications using Computer Vision, Machine Learning (ML), and Artificial Intelligence (AI). It is also used to learn about these technologies.
Once the development environment is set up, you can develop with Python and OpenCV using almost the same code and procedures, regardless of your OS. For example, the book I’m currently referencing for this learning article is based on Ubuntu Linux, but I’m using both Windows and macOS. Nevertheless, the Python code in the books works the same as it is. Also, the way to install the necessary libraries is the same.
This article describes how to set up Python and OpenCV for Computer Vision, ML, and AI.
WSL: Setting up Windows Services for Linux
WSL is required to develop on Windows in the same manner as on Ubuntu or macOS. See the following article on how to set up WSL.
About Shells
In this article, we will run commands in a shell; on macOS, we will use the Terminal app; on Windows, we will use the WSL shell.
Install Python3
Python3 comes pre-installed on both Windows (WSL) and Ubuntu. However, even if it is not installed, it will be installed as one of the dependencies when pip3
is installed later.
For macOS, when Xcode is installed, Python, and NumPy are also installed during the setup process when Xcode is launched for the first time.
Install Mesa 3D Graphics Library (Windows and Ubuntu)
The Mesa project is an open-source version of OpenGL. It can be installed with apt as follows.
sudo apt install libgl1-mesa-dev
Install Pip3 (Windows and Ubuntu)
Pip is Python’s official package management tool. It is probably installed by default, but you can install it with apt
.
sudo apt install python3-pip
Install Tkinter
Tkinter is a library for handling GUI from Python. We often use Matplotlib from Python to display images, graphs, etc. You can install it with apt
.
sudo apt install python3-tk
Without it installed, you will encounter the following error message, causing Matplotlib’s GUI functions to malfunction.
Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
Install OpenCV
Install OpenCV and subsequently use pip3
to install Python packages, as we will utilize Python.
Input the following command to obtain a list of available versions.
pip3 install opencv-python==
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement opencv-python== (from versions: 3.4.0.14, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 3.4.13.47, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 3.4.18.65, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64, 4.6.0.66)
ERROR: No matching distribution found for opencv-python==
In this example, the latest version is 4.6.0.66
.
Install the latest version: 4.6.0.66
as found in STEP 1.
pip3 install opencv-python==4.6.0.66
Install OpenCV extensions
Install the OpenCV extension module using pip3
as with OpenCV.
Enter the following to get a list of available versions to install
pip3 install opencv-contrib-python==
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement opencv-contrib-python== (from versions: 3.4.11.45, 3.4.13.47, 3.4.14.51, 3.4.15.55, 3.4.16.59, 3.4.17.61, 3.4.17.63, 3.4.18.65, 4.4.0.46, 4.5.1.48, 4.5.2.52, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64, 4.6.0.66)
ERROR: No matching distribution found for opencv-contrib-python==
Install the same version as OpenCV. 4.6.0.66
is installable. It is the same version as OpenCV.
pip3 install opencv-contrib-python==4.6.0.66
Install Matplotlib
Matplotlib is a library used for drawing graphs in Python. If you try to use Matplotlib in an environment where Matplotlib is not installed, you will receive the following error.
from matplotlib import pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
Install with pip3
.
pip3 install matplotlib
You can also specify a version. If you do not specify a version, the latest version will be installed.
Install Python bindings for Qt
When using OpenCV cv2.imshow()
, etc., I get the following error if the Python bindings for Qt are not installed.
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/akira/.local/lib/python3.10/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb.
Aborted
Install the Python bindings for Qt. For example, install the Python3 binding for Qt5 with apt
as follows.
sudo apt install python3-pyqt5