Life2Coding
How to Install OpenCV 3.4.0 with Python 3 on Raspberry Pi 3

Introduction

OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. It is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications.

In this post, I will show you how to install OpenCV 3.4.0 with Python 3.5 on Raspberry Pi 3. First I will show you how to install OpenCV3 without any virtual environment and how you can also use a virtual environment to work with that build file as well.

Objectives:

Instruction to install OpenCV on Raspberry Pi.

  • OpenCV version: 3.4.0
  • Target platform: Raspberry Pi 3 B
  • OS: Raspbian Stretch
  • Language: Python 3

Video Youtube:

Steps:

There are some steps to install OpenCV properly on Raspberry Pi 3 with Python 3. I will show all the steps to get it working properly.

Step 1: Expand filesystem

Type the following command to expand the Raspberry Pi3 file system

Then select the following

  • Advanced Options > A1 Expand filesystem > Press “Enter”

It will show a message “The root partition has been resized”.

Then you need to reboot your pi using the following command.

Step 2: Free Up Some Space

The default OS will take around 15% if you are using 32GB card. But if you are using a 8GB memory card it might take 50% of all your space. So, it is better to remove some unused packages like LibreOffice and Wolfram engine to free up some space on your pi.

You can do it simply typing the following command on the terminal window.

Step 3: Install Dependencies

  • The first step is to update and upgrade any existing packages:

  • If you have been shown any error to fix you can type the following

  • Then reboot your pi.

After your pi boots up start the Terminal again. Do the following.

  • Install CMAKE developer packages

  • Install Image I/O packages

  • Install Video I/O packages

  • Install the GTK development library for basic GUI windows

  • Install optimization packages (improved matrix operations for OpenCV)

Step 4: Install Python 3, setuptools, dev and Numpy

  • Install Python 3 and numpy

Step 5: Download the OpenCV 3.4 and contrib extra modules

Step 6: Compile and Install OpenCV 3.4.0 for Python 3

Step 7: Swap Space size before compiling to add more virtual memory

It will enable OpenCV to compile with all four cores of the Raspberry PI without any memory issues.

Open your /etc/dphys-swapfile and then edit the CONF_SWAPSIZE  variable

It will open the nano editor for editing the CONF_SWAPSIZE. Change it like below:

Then save the changes you’ve made, press Ctrl + O. To exit nano, type Ctrl + X. If you ask nano to exit from a modified file, it will ask you if you want to save it. Just press N in case you don’t, or Y in case you do. It will then ask you for a filename. Just type it in and press Enter.

Then type the following lines to take it into effect

Step 7: Finally Ready to be Compile

Type the following command to compile it using 4 cores of pi

Step Optional: Compile with a single core of Pi

If you face any error while compiling due to memory issue you can start the compilation again with only one core using the following command

Step 8: Install the build on raspberry pi

After the successful build install the build using the following command

Step 9: Verify the OpenCV build

After running make install, OpenCV + Python bindings should be installed in usr/local/lib/python3.5/dist-packages or usr/local/lib/python3.5/site-packages.

You need to use the site-packages or dist-packages. Look where it has been created and use that site-packages or dist-packages. In my case it is in dist-packages.

Again, you can verify this with the ls command:

Look for a name like cv2.so and if it is not there then look for a name like cv2.cpython-35m-arm-linux-gnueabihf.so (name starting with cv2. and ending with .so). It might happen due to some bugs in Python binding library for Python 3.

We need to rename cv2.cpython-35m-arm-linux-gnueabihf.so to cv2.so using the following command:

Step 10: Testing OpenCV 3.4.0 install

Step Optional: Remove the zip files to free up some space:

Step 11: Don’t forget to change your swap size back!

Open your /etc/dphys-swapfile  and then edit the CONF_SWAPSIZE  variable

It will open the nano editor for editing the CONF_SWAPSIZE. Change it like below:

Then save the changes you’ve made, press Ctrl + O. To exit nano, type Ctrl + X. If you ask nano to exit from a modified file, it will ask you if you want to save it. Just press N in case you don’t, or Y in case you do. It will then ask you for a filename. Just type it in and press Enter.

Then type the following lines to take it into effect

Step 12 (Additional): Setting OpenCV for a virtual environment (Python 3)

Next section is for adding access from a virtual environment.

  • Make sure that you have installed venv for Python 3.

  • Make a virtual environment for OpenCV3 with Python3

  • To activate the venv you made, execute

Let’s make a symbolic link inside of your venv package folder.

Here, ‘~/cvpi’ is the virtual environment directory

The result will look like this.

Don’t forget to install numpy for a new venv

Step 13: Testing OpenCV 3.4.0 in the virtual environment

Now, check that you can use cv2 INSIDE of the virtual environment.

Activate and Deactivate your Virtual Environment

Activate

Deactivate

 

 

life2coding_icon [] How to Install OpenCV 3.4.0 with Python 3 on Raspberry Pi 3

Life2Coding

Technology Related Blog at Life2Coding
Feel free to contact us for your any kind of technical problems. We are here to help you.
life2coding_icon [] How to Install OpenCV 3.4.0 with Python 3 on Raspberry Pi 3

29 thoughts on “How to Install OpenCV 3.4.0 with Python 3 on Raspberry Pi 3

  1. omar

    hello, having a problem about Step 7: Finally Ready to be Compile just output
    “make: *** No target specified and no makefile found. stop”

    How can i solve this problem

    1. Life2Coding Post author

      Check the video tutorial (Link Above attached). I think your directory is different. Try to go to the exact directory.

  2. jim

    This is failing when installing on a fresh install of stretch-lite :sudo pip3 install numpy The message is “sudo: pip3: command not found” I’m not sure what the work around is.

  3. Rafael

    Hello Excellent work. I have a question, it is possible to have OpenCV installed simultaneously for the two Python 2.7 & 3.5, greetings.

  4. Alexander

    Is there a way to copy the compiled files onto an other Raspberry(same model)? I want to avoid recompilation on a different pi

  5. rubentxo

    I want to thank for your step by step tutorial! It’s the best one!
    Great work!!! I’ll follow your blog!

    Thanks!

  6. Francesco Garavaglia

    Hi!
    I’m having problems with Step 10: Testing OpenCV 3.4.0 install
    This is what I get:

    pi@raspberrypi:~ $ python3
    Python 3.5.3 (default, Jan 19 2017, 14:11:04)
    [GCC 6.3.0 20170124] on linux
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    File “/home/pi/.local/lib/python3.5/site-packages/cv2/__init__.py”, line 4, in
    from .cv2 import *
    ImportError: libQtGui.so.4: cannot open shared object file: No such file or directory

    Please Help me!

    1. Life2Coding Post author

      Watch the youtube video tutorial properly. I think you missed any of the steps that why it is not getting the OpenCV Library

      1. Francesco Garavaglia

        I retried, but nothing changes. I saw that the unique difference occurs during “Step 6: Compile and Install OpenCV 3.4.0 for Python 3” when I got this:

        — Python 3:
        — Interpreter: /usr/bin/python3 (ver 3.5.3)
        — Libraries: /usr/lib/arm-linux-gnueabihf/libpython3.5m.so (ver 3.5.3)
        — numpy: /home/pi/.local/lib/python3.5/site-packages/numpy/core/include (ver 1.14.2)
        — packages path: lib/python3.5/dist-packages

        Looking at numpy I got site-packages instead of dist-packages. I think that’s the problem!
        What do you think? Sorry, but I’m a newbie 🙂

          1. Nithin Bharadwaj

            HI Franscesco,

            Could you please elaborate on how you solved the above issue? I am getting the same error, and am not able to resolve it. Please help!

  7. Matt Miller

    Thanks for making a Python3 set of instructions
    I used make instead of make -j4 as I’ve had problems in the past compiling previous versions of openCV and it worked 1st time.

  8. Steven Blue

    at the end of step 6 i get (configuring incomplete, errors occured!)
    what can i do about that???

    1. Life2Coding Post author

      Have you give the “..” at the end as well. Try to use the same text from the step 6 to configure it

      1. Steven Blue

        yes i type the “..” and the process starts properly but at the end i take thar errors…..when i go backwards to see the process i saw some “not found” files like “linux/videodev.h”, “linux/videodev2.h”,”libgphoto2″, and some other files also

  9. hung

    Hi I Install Image I/O packages

    sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev -y

    An error occurred…….

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package libpng12-dev is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Unable to locate package libjasper-dev
    E: Package ‘libpng12-dev’ has no installation candidate

    How to solve??

  10. hilda

    at step 1 i got (Your partition layout is not currently supported by this tool. You are probably using NOOBS, in which case your root filesystem is already expanded anyway)
    because i installed using NOOBS , should i install raspbian stretch ? please help me, thankyou

  11. Nithin Bharadwaj

    Hi,
    I am receiving the same error. I did a clean installation of OpenCV 3.4 on Raspberry Pi 3. After the installation, I went into the Python3 virtualenv and checked for OpenCV installation by doing, “import CV2” and then a “cv2.__version__”. It worked when I was inside the OpenCV build folder. Now, I came out of it and am in the home directory, and I get a syntax error for “Import cv2”. Please help.

  12. sleman

    i followed your steps in this tutorial to use opencv in face recognition but when i use the command
    recognizer = cv2.createLBPHFaceRecognizer()
    or
    recognizer = cv2.face.LBPHFaceRecognizer_create()
    it gives me errors

  13. Vaisakh

    [ 65%] Linking CXX executable ../../bin/opencv_traincascade
    ../../lib/libopencv_imgcodecs.so.3.4.0: undefined reference to WebPFlipBuffer'
    ../../lib/libopencv_core.so.3.4.0: undefined reference to
    carotene_o4t::absDiff(carotene_o4t::Size2D const&, unsigned char const*, int, unsigned char const*, int, unsigned char*, int)’
    ../../lib/libopencv_imgcodecs.so.3.4.0: undefined reference to WebPDeallocateAlphaMemory'
    ../../lib/libopencv_core.so.3.4.0: undefined reference to
    carotene_o4t::absDiff(carotene_o4t::Size2D const&, unsigned short const*, int, unsigned short const*, int, unsigned short*, int)’
    ../../lib/libopencv_core.so.3.4.0: undefined reference to carotene_o4t::absDiff(carotene_o4t::Size2D const&, signed char const*, int, signed char const*, int, signed char*, int)'
    ../../lib/libopencv_imgcodecs.so.3.4.0: undefined reference to
    WebPAvoidSlowMemory’
    ../../lib/libopencv_core.so.3.4.0: undefined reference to carotene_o4t::absDiff(carotene_o4t::Size2D const&, float const*, int, float const*, int, float*, int)'
    ../../lib/libopencv_imgcodecs.so.3.4.0: undefined reference to
    WebPCopyDecBuffer’
    ../../lib/libopencv_imgcodecs.so.3.4.0: undefined reference to WebPAllocateDecBuffer'
    ../../lib/libopencv_imgcodecs.so.3.4.0: undefined reference to
    WebPCopyDecBufferPixels’
    ../../lib/libopencv_core.so.3.4.0: undefined reference to carotene_o4t::absDiff(carotene_o4t::Size2D const&, short const*, int, short const*, int, short*, int)'
    ../../lib/libopencv_core.so.3.4.0: undefined reference to
    carotene_o4t::absDiff(carotene_o4t::Size2D const&, int const*, int, int const*, int, int*, int)’
    ../../lib/libopencv_imgcodecs.so.3.4.0: undefined reference to `VP8DecompressAlphaRows’
    collect2: error: ld returned 1 exit status
    apps/traincascade/CMakeFiles/opencv_traincascade.dir/build.make:389: recipe for target ‘bin/opencv_traincascade’ failed

    i got this error when i was installing opencv on my rpi b+ model running with rasbian stretch. please help me

Leave a Reply

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