Install OpenCV Python on Raspberry Pi 3

In this tutorial, I will show you how to install OpenCV Python on Raspberry Pi 3. We will be installing OpenCV realease 3.4.1 with Python 3 on latest Pi operating system, Raspbian Stretch. You can follow the video tutorial along with this post here.

Before starting the tutorial, I assume that you have Raspbian Stretch OS pre-installed on your Pi and you are able to access it either using SSH, Remote Desktop or conventional full-PC setup consisting monitor and keyboard. If you don’t have OS installed or aren’t able to access it, you can follow my previous post on headless setup of Raspberry Pi.

That being said, let’s get started to install OpenCV Python on Raspberry Pi.

Step 1: Make Pi up-to-date

Update existing packages.

sudo apt-get update

Upgrade existing Pi software.

sudo apt-get upgrade

Update Raspberry Pi firmware.

sudo rpi-update

Step 2: Install Dependencies

sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran

Step 3: Setup Python 3 development tools

sudo apt-get install python3 python3-setuptools python3-dev

Step 4: Setup pip tool

wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

Step 5: Grab OpenCV 3.4.1 and OpenCV-contrib archives

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.4.1.zip
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.4.1.zip
unzip opencv.zip
unzip opencv_contrib.zip

Step 6: Install numpy

sudo pip3 install numpy

Step 7: Build OpenCV

cd ~/opencv-3.4.1/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.1/modules \
      -D ENABLE_PRECOMPILED_HEADERS=OFF \
      -D BUILD_EXAMPLES=ON ..

Step 8: Increase swap space

We are going to increase swap size from 100MB to 1024MB, to facilitate compilation of OpenCV on all four cores of Pi.

Open /etc/dphys-swapfile using nano editor.  Change the value CONF_SWAPSIZE  variable to 1024.

Save the edited file and activate the new swap space using the following commands to restart the service

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Step 9: Compile  and Install OpenCV

We are going to compile OpenCV on all four cores. Issue the following command and compilation should succeed within 2 hours.

make -j4

All that is left is, installing OpenCV using the following commands.

sudo make install
sudo ldconfig

Step 10: Test OpenCV Installation

Fire up the Python 3 interpreter on command line and run the following Python code.

import cv2
cv2.__version__

The python code should output ‘3.4.1’, which indicates that the installation is perfectly fine.

Remove the downloaded zip files to free up space

cd ~
rm -rf opencv.zip opencv_contrib.zip

Revert back the swap size to its original value of 100MB by editing the /etc/dphys-swapfile. And, restart the service by issuing commands.

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

 

Congratulations for your OpenCV-Python installation on Raspberry Pi 3. 

 

You may also like...

14 Responses

  1. Thank you
    I have successfully installed opencv on my raspberry pi
    what python code do i need use to activate the camera for object detection

  2. Shawnee says:

    This is the right site for anybody who really wants to understand this
    topic. You know a whole lot its almost tough to argue with you
    (not that I actually would want to…HaHa).
    You definitely put a new spin on a subject that has been written about for years.

    Great stuff, just wonderful!

  3. romeo says:

    Congratulations, the installation succeeded perfectly even with VERSION = “9 (stretch)”, mounted on model name: ARMv7 Processor rev 4 (v7l) …. (a clarification: the make -J4 command took 12 hours of work)

  4. Truzen says:

    Thank you so much for this guide. I tried following other sites, but would always crash out around 54% in the build process. I think the swap file change is what did it.

  5. Bao Pham says:

    Hi guy, i commented on Youtube, I receive an error when run make -j4.

  6. Hi, I am getting a failure after I do the sudo nano /etc/dphys-swapfile and changing the value of CONF_SWAPSIZE 1024 , I exit enter control/Y and Enter when it asks to save file name, then I do .. sudo /etc/init.d/dphys-swapfile stop and everything is fine, but doing .. sudo /etc/init.d/dphys-swapfile start creates a failure and when I begin the.. make -j4 , it freezes up and I can see many warning messages and I cannot finish. If you need more precise info let me know but I cannot get past this. Any help would be gratefully received. Thanks.

  7. fef says:

    after we do make-j4 it stops at 81 percent.what should we do?

  8. Raxit Siddhpura says:

    its not working at percentage of 79 its stop and give error of makefile:163:all please help me to solve this

  9. w domoney says:

    My build on Buster crashes out

    make[2]: *** [modules/python3/CMakeFiles/opencv_python3.dir/build.make:63: modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o] Error 1
    make[1]: *** [CMakeFiles/Makefile2:16386: modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….
    [ 81%] Linking CXX executable ../../bin/example_cpp_segment_objects
    [ 81%] Linking CXX executable ../../bin/example_cpp_connected_components
    [ 81%] Linking CXX executable ../../bin/example_cpp_openni_capture
    [ 81%] Built target example_cpp_segment_objects
    [ 81%] Built target example_cpp_connected_components
    [ 81%] Built target example_cpp_openni_capture
    make: *** [Makefile:163: all] Error 2

  10. Sumit says:

    make -j4 is hanging the pi after 40-44% completion and not responding. What to do now

  11. mouna says:

    Hi,I am trying to install opencv on raspberry pi but I found a problem in the step9.This is the error fatal error:stdlib.h:no such file or directory thanks

  12. Nagen Swamy says:

    Many thanks for the Installation steps, followed all steps as mentioned, it went Perfect no issues.

    Well articulated and described

  13. HIREN says:

    HI
    IF we want to install virtualenv virtualenvwrapper then
    and what is function of virtualenv virtualenvwrapper
    either we unable to install

  14. Very educative post, learned a lot. So glad I discovered
    your blog, and managed to learn new things. Keep posting informative articles, it is really helpful.

    King regards,
    Thomassen Zacho

Leave a Reply

Your email address will not be published. Required fields are marked *

15 − 2 =