Get Python* Setup for Single Board Computers with Intel® Neural Compute Stick 2 (Intel® NCS 2)

 

The Intel® Distribution of OpenVINO™ toolkit  includes a Python* API for developing applications for the Inference Engine. After you’ve set up Intel® OpenVINO™ toolkit on your ARM* device, you may want to build the API to enable you to use Python as your language of choice for your computer vision applications.

Before you begin make sure that you’ve followed the steps to setup your board with Ubuntu* and Intel® OpenVINO™ toolkit.

If you’ve already build Intel® OpenVINO™ toolkit from source on this device, then during the OpenCV* build process the Python API package should have been created. If dependencies were missing during the build process, then you will have to rebuild OpenCV with Python. This article will have youbuild the Python package for OpenCV, to allow Intel® OpenVINO™ toolkit to communicate using Python appropriately, and the Python API for Intel® OpenVINO™ toolkit.

Using Python*

Python* should be installed on your board, but you can follow these directions to check. This article uses Python 3.6, though Python 2.7 is supported. You can check if Python is currently installed by using the following command:

python3 –-version

If the command prints out the current version of python3, you’re set to go.

Note: This article assumes you are using Python 3.6, which uses the python3 command in many distributions to differentiate the version. If you’re using Python 2.7 you may need to use python instead of python3.

Installing Dependencies

Python-OpenCV has a few dependencies that need to be installed before continuing, but these may have been installed when building Intel® OpenVINO™ toolkit the first time. Otherwise, use your package manager (apt) to install these:

  • python3-dev
  • libgtk2.0-dev
  • ffmpeg-dev
  • libgstreamer-plugins-base1.0v-dev

Using pip, python’s package manager, also install numpy and cython.

sudo pip3 install numpy
sudo pip3 install cython

CMake* is also required. Refer to the ARM* 64 Single Board Computer and the Intel Neural Compute Stick 2 article to install CMake from source.

Building OpenCV* for Python*

If you’ve already cloned and built OpenCV*, then the wrappers should have built alongside the rest of the libraries as long as both Python* and the package-specific dependencies were installed. If the dependencies were unavailable, then the build system skips
building the Python code. If that is the case, follow the commands below to build OpenCV:

git clone https://github.com/opencv/opencv.git
cd opencv
mkdir build
cd build
cmake ..

In the CMake output, you should see a reference to your Python installation. If you don’t see this, then make sure you’ve installed python3-dev and numpy. Next, start building – the build system will skip any up to date already built packages. If you’ve already built OpenCV on this platform, then the system will only build the additional Python packages:

make
sudo make install

At this point, OpenCV should have been built with the Python wrappers. To confirm, try to import cv2 in the Python prompt. Use the python3 keyword to enter the prompt:

python3
>>>import cv2 as cv
>>>print(cv.__version__)

If the command prints, then you’ve successfully built the wrappers. If an error is thrown, make sure that you’ve installed the correct dependencies and try the build again.

Building Intel® OpenVINO™ toolkit with Python* API

If you’ve confirmed that the Python* can read OpenCV*, then you can build Intel® OpenVINO™ toolkit with the Python API.

Located in the <dldt>/inference-engine/ie_bridges/python folder is the sample set and source code for Python. Using pip, install the requirements:

pip3 install –r requirements.txt

If the requirements install without issue, then you can continue to the next step.
Otherwise, you may have to add an extra index url to your pip.conf file named https://www.piwheels.org/simple.
This repository includes ARM-specific packages made for Raspbian*, the Debian* derivative used for Raspberry Pi*. These packages can also be used for our purposes as they are made for ARM*. Refer to the pip documentation for directions on how to add another index URL correctly.

Scope to the <dldt>/inference-engine/build folder and then run the following commands to build the Python API:

cmake -DCMAKE_BUILD_TYPE=Release \
           -DENABLE_MKL_DNN=OFF \
           -DENABLE_CLDNN=OFF\
           -DENABLE_GNA=OFF \
           -DENABLE_SSE42=OFF \
           -DTHREADING=SEQ \
           -DENABLE_PYTHON=ON \
           -DPYTHON_EXECUTABLE=/usr/bin/python3 \
           -DPYTHON_LIBRARY=/usr/lib/aarch64/libpython3.6m.so \
           -DPYTHON_INCLUDE_DIR=/usr/include/python3.6 \
           ..
make

The new build should compile. When it finishes, you need to add the Python API libraries to the PYTHONPATH and LD_LIBRARY_PATH system variables. You can use the following commands in your shell, or add them to the end of your ~/.bashrc. Replace the path with the path to your OpenVINO directory:

export PYTHONPATH=$PYTHONPATH:~/OpenVINO/dldt/inference-engine/bin/intel64/Release/lib/python_api/python3.6
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/OpenVINO/dldt/inference-engine/intel64/Release/lib/python_api/python3.6

Note: These commands assume you’re using the Odroid-C2 running an ARM64 operating system. If you’re using a 32-bit ARMv7 operating system, replace intel64 with armv7

Verifying Installation

After this point, you should verify that the steps above work. Scope to the following folder:

<dldt>/inference-engine/ie_bridges/python/samples/benchmark_app/

This folder contains a script benchmark_app.py that provides the same functionality as the benchmark_app C++ program that is built in the Releases folder. You can use the following command to test the Python API using an Intel® NCS 2. Make sure you’ve installed the USB rules to connect your Intel® NCS 2 as shown in this article. The files referenced in this command are in the attached archive. Place them where appropriate and modify the command accordingly.

python3 benchmark_app.py –i ~/OpenVINO/president-reagan-62x62.png –m ~/OpenVINO/squeezenet1.1.xml –d MYRIAD

If you receive an error, check to see if any python packages are needed – you should be able to install those using pip to fix the errors. If the command finishes, then you’re set up to use Python with Intel® OpenVINO™ toolkit  on your ARM device using Intel® NCS 2.

For more complete information about compiler optimizations, see our Optimization Notice.