Installing OpenCV 3.4.1 using Python 3 on Raspbian Stretch

UPDATED OCT 2020

See this link for a much better procedure:

https://medium.com/@aadeshshah/pre-installed-and-pre-configured-raspbian-with-opencv-4-1-0-for-raspberry-pi-3-model-b-b-9c307b9a993a

 

This is left for archiving purposes:

As usual, I will keep this short and sweet.

  1. Make sure you have Raspbian Stretch installed and upadted. Alsi rpi-update!
  2. edit /etc/php.conf and add
    1. [global]
      extra-index-url=https://www.piwheels.org/simple
  3. Download OpenCV source from opencv.org and compile it
    1. unzip
    2. cd opencv-x.x.x
    3. mkdir build
    4. cd build
    5. CMake or cmake-gui
    6. make
    7. make install
  4. Create a virtual environment
    1. $ sudo apt install virtualenv python3-virtualenv -y
      $ virtualenv -p /usr/bin/python3 testpip
      $ source testpip/bin/activate
      (testpip) $ pip install numpy
      ...
      (testpip) $ deactivate
      $ rm -fr testpip/
  5. Install packages from piwheels into that environment
    1. pip install numpy
    2. pip install opencv-python
    3. pip install opencv-contrib-python
  6. Verify installation
    1. $ python
      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
      >>> import numpy

      Should give no errors

If you get this error when installing opencv-python

alpha@pi2:~/PYTHON/demo $ pip install opencv-python
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting opencv-python
Could not find a version that satisfies the requirement opencv-python (from versions: )
No matching distribution found for opencv-python

You should make sure that you are in the virtual environment created above, easy to forget if you switch terminals!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.