Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

Building OpenCV for Python 3.5

Chris Lovett edited this page Jun 12, 2018 · 1 revision

by Bret Stateham and Chris Lovett

If you need to build OpenCV for Python 3.5 you can follow the instructions here. For example, you will need to do this if you are using a Raspberry Pi Zero because Miniconda is not available on that device. You will start with a clean image of the Raspbian Stretch OS because it comes with Python 3.5 built in then you will get the required tools, build OpenCV and test it out. All this should take about 2.5 hours and most of that is just waiting for the OpenCV build to finish.

  1. Download the latest Raspbian Stretch image

  2. Flash this image onto your SD card using Etcher.io

  3. Log in as the user pi with the password raspberry

  4. Increase the swap space on Raspberry Pi Zero

    If you have a Raspberry Pi Zero you need to increase the swap file size in order to avoid running out of memory during compilation. It would not hurt to do this on any Raspberry Pi, but it is necessary on Pi Zero. This is done by editing the swap space configuration:

    sudo nano /etc/dphys-swapfile
    

    Find the following line (and uncomment it if necessary) and increase the default value to at least 1024:

    CONF_SWAPSIZE=1024
    

    Save the file and reload the swap space configuration by type the following commands:

    sudo /etc/init.d/dphys-swapfile stop
    sudo /etc/init.d/dphys-swapfile start
    
  5. Update the os:

    sudo apt-get update
    sudo apt-get upgrade -y
    
  6. Verify your Python 3 version by running:

    python3 --version
    

    You should see this output:

    Python 3.5.3
    
  7. Install pre-reqs needed to build OpenCV 3.3:

    sudo apt-get install -y build-essential cmake pkg-config
    sudo apt-get install -y libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev libdc1394-22-dev
    sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
    sudo apt-get install -y libxvidcore-dev libx264-dev
    sudo apt-get install -y libgtk2.0-dev libgtk-3-dev
    sudo apt-get install -y libatlas-base-dev gfortran
    sudo apt-get install -y python3.5-dev 
    sudo apt-get install python3-setuptools
    sudo easy_install3 pip
    
  8. Install Jinja2

    sudo pip3 install jinja2
    
  9. Install numpy

    sudo pip3 install numpy
    
  10. Download and extract OpenCV source:

    cd ~
    wget -O opencv3.3.0.zip 'https://github.com/Itseez/opencv/archive/3.3.0.zip'
    unzip opencv3.3.0.zip
    cd opencv-3.3.0
    wget -O opencv_contrib.zip 'https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip'
    unzip opencv_contrib.zip
    
  11. Create and change into the ~/opencv-3.3.0/build directory

    mkdir build
    cd build
    
  12. Run cmake with the proper options:

    Note: The very last line of the command points to the parent folder .. of the ~/opencv-3.3.0/build folder. Make sure to include the .. at the end of the command.

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.3.0/modules \
      -D BUILD_EXAMPLES=OFF \
      -D BUILD_TESTS=OFF \
      -D BUILD_PERF_TESTS=OFF \
      -D BUILD_opencv_python2=0 \
      -D BUILD_opencv_python3=1 \
      ..
    
  13. The CMake command outputs a big configuration summary. Note specifically that the Python3 section must be fully populated with pointers to your python 3.5 install. This ensures the build will create the Python 3.5 module after it has built all the OpenCV native libraries, the Python module is called "cv2".

    --
    --   Python 3:
    --     Interpreter:                 /usr/bin/python3 (ver 3.5.3)
    --     Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython3.5m.so (ver 3.5.3)
    --     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.14.3)
    --     packages path:               /home/pi/.local/lib/python3.5/site-packages
    --
    
  14. Note also that the Install path is set to /usr/local, this is so that the ELL C++ tutorial can find your Open CV includes and libraries there.

    --   Install path:                  /usr/local
    
  15. Run make

    make
    
  16. Run make install to install what you just built into your OS folders so that python3 can find it:

    sudo make install
    
  17. Test it and you should see the output "3.3.0" :

    (py34)pi@raspberrypi:~ $ python3 -c "import cv2; print(cv2.__version__)"
    3.3.0
    

Done!

You can now build and run the ELL tutorials that use python and since you built OpenCV for the system provided Python 3.5 without using Miniconda, you can simply skip any step in the tutorial that says things like "source activate py34". You can also build the ELL C++ Tutorials and it will automatically find this new build of OpenCV you created since the make install placed the C++ include files in /usr/include and the libraries in /usr/lib.

Troubleshooting

pip3 errors

If you run into trouble with pip3 not working properly you can run it with this command instead python3 -m pip ....

ImportError: No module named 'cv2'

This means the OpenCV build did not build the python "cv2" module. Check your CMake command line matches what is shown above.

Clone this wiki locally