Super simple to teach you to install opencv on the Raspberry Pi (2)
foreword
Notes for use: This article is mainly aimed at installing OpenCv for Python version 3.9.2. If your Python version is not Python 3.9.2 but below 3.8, you can click the tutorial here install OpenCv. In addition, some versions seem to be Python3.9.3, and the method in this article should not be applicable. |
---|
![]() |
1. How to install OpenCv based on python3.9.2?
1.1 Check your system digits and Python version, follow the tutorial
Because I directly downloaded the image of the official 32-bit operating system, I did not consider the number of system digits when I wrote the blog. I would like to inform you in advance that the author has not tried to install OpenCv on a 64-bit system, so I do not know how to install it. Whether there will be an error in the process and whether the installation can be successful, you can try it. Thanks to user qq_46120289 for raising this question
# 大家可以使用下方指令查看自己系统位数
getconf LONG_BIT
Because different Python versions operate differently, it is important to follow the tutorial.
1.2 Source change
You can follow my previous article to change the source, or follow my next operation to change the source. In fact, the previous method of changing the source is a bit troublesome. It is recommended that you use the next method of changing the source.
wget -qO- https://tech.biko.pub/resource/rpi-replace-apt-source-buster.sh | sudo bash
Copy the above code to the LX_ terminal ,
press Enter as shown in the figure below to confirm, and the source is changed successfully, as shown in the figure below
1.3 update
To update the new source content in the terminal, enter the following two lines of code to update, as shown in the figure below
sudo apt-get update
sudo apt-get upgrade
1.4 Installation dependencies
Enter the following contents in sequence in the terminal and install them one by one, as shown in the figure below
If there is a red error when you execute the third statement below, you can try the solution provided by the user I don’t know how to fall in love , and I would like to thank the user for sharing his experience!
If you downloadsudo apt-get install libqtgui4
an error ,
you can download it firstsudo apt-get install aptitude
and then entersudo aptitude install libqtgui4
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install python3-pyqt5
sudo apt install libqt4-test
Some things may not be installed during the process, don't worry, pay attention to the error message, if it is not a red error, don't worry, either it has already been installed, or there is a higher version, which will not affect the follow-up operation. As shown below
After the above operation is completed, sudo apt-get update again
1.5 Check the architecture of your Raspberry Pi to determine what package to download
Enter the following code in the LX_ terminal to view your own Linux architecture
uname -a
The Linux system in the above picture uses the armv7l architecture, in addition to the armv6l architecture, we will proceed to the next step after knowing our own system architecture
1.6 Download the whl package you need
This step is very simple, we are almost halfway done, come on!
Enter the website to download the packages we need. After entering the link, as shown in the figure below,
I will download and install OpenCv4.5.5.62 as an example. Of course, you can also install other versions according to your needs, provided that your Python version needs to be supported. Obviously
I need Download the whl file pointed by the arrow in the image above. How do I know which one to download? Let me talk about it here. The author here is explaining the whl package pointed by the arrow. Its OpenCV version is 4.5.5.62, and the required Python version is 3.9 [cp39 corresponds to Python3.9, cp37 corresponds to Python3.7], and the armv7l architecture is required Linux system
Next, we need to transfer the downloaded whl file to the Raspberry Pi. How to transfer depends on your own choice. I use VNC Viewer to transfer files here. Of course, you can also choose to use FileZilla to transfer. No matter what transfer is used, I hope you can transfer the file Put it on the desktop of the Raspberry Pi to facilitate the next step.
1.7 Install OpenCv
When you come here, you need to make sure that the preparatory work has been completed, otherwise an error may be reported. If the preliminary work has been completed, then the official installation of OpenCv
We need to use the following code to enter the desktop first
cd Desktop
Then enter pip install opencv. At this time, we enter it roughly like the picture below, because the OpenCv version number is long and error-prone, so just press the Tab key here to let the computer automatically complete it. After completion, it is
directly as shown in the second picture below. Press Enter to confirm, wait for one minute, and the following interface will appear, indicating that the installation is successful
2. Reported an error? !
happy? The installation is successful, but many people will soon find that an error is reported when importing OpenCv, but some will not report an error
2.1 The following error is reported
If nothing else, many people report errors as shown in the figure below when importing OpenCv
2.2 Why an error is reported
Because OpenCv is associated with numpy, the version needs to be consistent. Once you know the cause of the error, it’s easy to handle. First, look at your own numpy version (you can skip this step and update numpy directly. I’m here to show you)
You can start directly from here and enter the following command to update numpy
pip3 install -U numpy
Warning, red warning, but it can still be installed successfully. Next, look at the numpy version
OK. The numpy version has caught up. The next step is to test whether OpenCv is available
2.3 Testing OpenCv
Import the OpenCv test, and check whether its version number is correct
and no error is reported, and it is over! ! ! !
3. The author has something to say
If you think this article is helpful to you, can you give the author a thumbs up to encourage you? At the same time, it will help more people who need him