Install OpenCV on Raspberry Pi in Less Than 10 Minutes
by Nishantk06 in Circuits > Raspberry Pi
8306 Views, 3 Favorites, 0 Comments
Install OpenCV on Raspberry Pi in Less Than 10 Minutes
Installing opencv on raspberry can be quite challenging and time-taking, if you do it the usual way. It takes around 3 long hours just to compile the opencv source code on raspberry pi 3 model b+.
Buttt, stick with me and in this tutorial, we are going to install opencv on your raspberry pi in less than 10 minutes.
We are going to use a pre-compiled opencv source code from piwheels and thus going to save a ton of time and energy on this.
Supplies
you will need:
- raspberry pi setup with installed OS
- laptop or a desktop(only if you want to install via ssh or vnc)
- stable internet connection
Connect Your Pi to Internet
I assume that you already have a supported OS installed on your pi.
Now connect your pi to your wifi network.
Check for Python3
This method for some reason doesn't work with python 2.7.
So open the terminal and check for python3
To check write the command
python3 --version
If python3 is installed on your pi then the version number should appear after running this command as shown in the image.
If it is installed then you are welcome to go ahead with this tutorial or else please install python3 and come back.
Install Dependencies
We need to install some dependencies before installing OpenCV.
Simply run these commands one by one and you are good to go.
I will mention the approximate time that it took me on raspberry pi 3 model b+ below every command.
Timings may vary for you depending upon your internet speed and the board you have.
sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
38 seconds
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test
1 minute and 42 seconds
sudo apt-get install libatlas-base-dev
31 seconds
sudo apt-get install libjasper-dev
20 seconds
Now, we are going to download the latest version of pip on your pi. Simply run this command to download the latest version of pip
wget https://bootstrap.pypa.io/get-pip.py
8 seconds
Now, run this command to install the latest version of pip.
sudo python3 get-pip.py
37 seconds
Now, you have installed all the required dependencies. Jump to the next step to install openCV.
Installing OpenCV
Now, since you have installed all the required dependencies, we only need to run one more command to install OpenCV from piwheels.
sudo pip3 install opencv-contrib-python==3.4.6.27
22 seconds
Congrats, you have successfully installed OpenCV on your pi. Your terminal should look similar to the above image now.
Testing OpenCV on Your Raspberry Pi
To test whether OpenCV is now installed to our Raspberry Pi, we will make use of our Python 3 installation.
Launch into the Python terminal by running the command below.
python3
While we are within Python, we can now import the OpenCV Python module using the command below.
By importing the module, we can first check to see if OpenCV will even load on our Pi.
import cv2
With the OpenCV module now imported, we should be able to retrieve its version.
To retrieve OpenCV’s version, use the following command.
cv2.__version__
If everything is now working as intended and OpenCV has been successfully installed to your Raspberry Pi, you should see text like the following appear in the command line.
'3.4.6'
Hopefully, at this point, you will now have OpenCV up and running.
Thank you,
Nishant