Installing OpenCV 4.0 on Google Coral Dev board

Balaji NJL
4 min readApr 26, 2019

--

Recently Google released TPU chip enabled devices under brand name ‘Coral’. The goal of these devices is to deploy AI/ML based applications onto the Edge to provide edge analytics capabilities, such as inferencing, instead of sending complete data stream to the Cloud. We are testing this device at our company to help our customer deploy edge devices to meet their business needs (low latency, lower bandwidth cost, and lower cloud compute and storage costs). Edge TPU chip based devices aka Coral devices are great fit for edge processing scenarios that we are currently testing (e.g. Vision Analytics). Coral devices are Linux based (Mendel OS), supports Python, C++, TensorFlow Lite and AutoML Vision Edge. For more details please refer to Google Coral website.

In this article, I will explain how to install OpenCV 4.0 on Coral Dev Board. This is based on OpenCV 4.0 installation steps as mentioned in PyImageSearch blog . I have made few minor changes to make it work on Coral Dev board.

Coral Dev Board— Image Copyright Google LLC

Coral dev board is very similar to the size of RaspberryPi. It has 8 GB eMMC and 1 GB LPDDR4. To compile OpenCV from source it doesn’t have enough memory and space on the device.

Minimum required space to install OpenCV is 3 GB and so I recommend using an external SD card. Attach a 16GB SD card and format it to use ext file system.

I am assuming that you have already installed the latest firmware on the dev board and you are able to connect to device using ssh or via serial interface.

Connect to device using ssh (or serial interface) and let us create 1GB temporary swap for the build

# Create temporary 1G swap for the build
$ sudo fallocate -l 1G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile

Update the system

$ sudo apt-get update
$ sudo apt-get upgrade

Install the pre-reqs as mentioned in the PyImageSearch blog

$ sudo apt-get install build-essential cmake unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-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 libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install python3-dev

Now mount the SD card

# to list all disks
$ fdisk -l
# if your SD card is /dev/sdc1
$ sudo mount /dev/sdc1 /mnt

Download OpenCV and contrib modules

$ cd /mnt
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip
$ mv opencv-4.0.0 opencv
$ mv opencv_contrib-4.0.0 opencv_contrib

As suggested, create Python3 virtual environment

$ cd ~
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pip

Edit ~/ .bashrc file and add following lines at the end

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

source the ~/ .bashrc file

$ source ~/.bashrc

Create virtual environment and install NumPy

$ mkvirtualenv cv -p python3
$ workon cv
# Install NumPy
$ pip install numpy

Now time to compile OpenCV

# Make sure you are still in cv virtual environment
$ cd /mnt/opencv
$ mkdir build
$ cd build

I used following CMake for my requirements, you can add or remove packages as necessary

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/mnt/opencv_contrib/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D ENABLE_FAST_MATH=1 \
-D ENABLE_NEON=ON -D WITH_LIBV4L=ON \
-D WITH_V4L=ON \
-D BUILD_EXAMPLES=ON ..

We can compile now, it took about 4 hours for me.

$ make

Time to install OpenCV

$ sudo make install
$ sudo ldconfig

Link it to the virtual environment

$ workon cv
$ python --version
Python 3.5
$ ls /usr/local/python/cv2/python-3.5
cv2.cpython-35m-x86_64-linux-gnu.so
$ cd /usr/local/python/cv2/python-3.5
$ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/python/cv2/python-3.5/cv2.so cv2.so

Finally run a quick test

$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'4.0.0'
>>> quit()

Now you can remove the SD card. If everything goes as expected, it should look as follows.

OpenCV 4 on Coral Dev board

Blog and code used with permission from Adrian @PyImageSearch.

--

--