Skip to content

Instantly share code, notes, and snippets.

@bigrobinson
Forked from willprice/README.md
Last active August 11, 2019 22:38
Show Gist options
  • Save bigrobinson/821659409cf93bc304ffbb937d4bd6d3 to your computer and use it in GitHub Desktop.
Save bigrobinson/821659409cf93bc304ffbb937d4bd6d3 to your computer and use it in GitHub Desktop.
Install OpenCV 4.1.0 with Intel Openvino Inference Engine for Raspberry Pi 4 Raspbian Buster

Install OpenCV 4.1.0 on Raspbian Buster with Intel Inference Engine

First download Openvino and untar into directory as explained at

Openvino Installation for Raspbian

Get latest version of toolkit

OpenVINO 2019 R2

Extract into /opt/ directory

$ cd ~/Downloads/
$ sudo mkdir -p /opt/intel/openvino
$ sudo tar -xf l_openvino_toolkit_raspbi_p_<version>.tgz --strip 1 -C /opt/intel/openvino

Build OpenCV with OpenVINO Inference Engine

$ chmod +x *.sh
$ ./download-opencv.sh
$ ./install-deps.sh
$ ./build-opencv.sh
$ cd ~/opencv/opencv-4.1.0/build
$ sudo make install

Run test.py using both python 2 and 3 to verify that OpenCV python bindings were successfully installed.

$ wget "https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png" -O lenna.jpg
$ python2 test.py lenna.jpg
$ python3 test.py lenna.jpg

Run openvino_fd_myriad.py to verify that OpenCV successfully built with Intel Inference Engine. You will first have to download a model with weights in intermediate representation (IR).

$ wget --no-check-certificate https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin/face-detection-adas-0001/FP16/face-detection-adas-0001.bin
$ wget --no-check-certificate https://download.01.org/opencv/2019/open_model_zoo/R1/models_bin/face-detection-adas-0001/FP16/face-detection-adas-0001.xml
$ python3 openvino_fd_myriad.py

WARNING: Users of boards with 1GB of memory

Compiling is very memory intensive, you will likely need to increase your swap size. Assuming you have a reasonably large SD card (>16GB to be safe), follow the procedure below to increase your swap size from the default 100MB to 2GB

$ sudo dphys-swapfile swapoff
$ sudo sed -i 's:CONF_SWAPSIZE=.*:CONF_SWAPSIZE=2048:g' /etc/dphys-swapfile
$ sudo reboot
#!/usr/bin/env bash
set -ex
OPENCV_VERSION=4.1.0
pushd ~/opencv/opencv-$OPENCV_VERSION
mkdir -p build
pushd build
RPI_VERSION=$(awk '{print $3}' < /proc/device-tree/model)
if [[ $RPI_VERSION -ge 4 ]]; then
NUM_JOBS=$(nproc)
else
NUM_JOBS=1 # Earlier versions of the Pi don't have sufficient RAM to support compiling with multiple jobs.
fi
# -D ENABLE_PRECOMPILED_HEADERS=OFF
# is a fix for https://github.com/opencv/opencv/issues/14868
# -D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic
# is a fix for https://github.com/opencv/opencv/issues/15192
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_CXX_FLAGS="-march=armv7-a" \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-$OPENCV_VERSION/modules \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_DOCS=ON \
-D BUILD_EXAMPLES=ON \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_EXECUTABLE=$(which python2) \
-D ENABLE_NEON=ON \
-D DCPU_BASELINE="NEON" \
-D WITH_INF_ENGINE=ON \
-D INF_ENGINE_LIB_DIRS="/opt/intel/openvino/deployment_tools/inference_engine/lib/armv7l" \
-D INF_ENGINE_INCLUDE_DIRS="/opt/intel/openvino/deployment_tools/inference_engine/include" \
-D CMAKE_FIND_ROOT_PATH="/opt/intel/openvino" \
-D ENABLE_CXX11=ON \
..
make -j "$NUM_JOBS"
popd; popd
#!/usr/bin/env bash
set -ex
OPENCV_VERSION=4.1.0
cd ~
mkdir -p opencv && pushd opencv
wget -O "opencv-${OPENCV_VERSION}.tar.gz" "https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.tar.gz"
wget -O "opencv_contrib-${OPENCV_VERSION}.tar.gz" "https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.tar.gz"
tar -xvf "opencv-${OPENCV_VERSION}.tar.gz"
tar -xvf "opencv_contrib-${OPENCV_VERSION}.tar.gz"
popd
#!/usr/bin/env bash
set -ex
sudo apt-get purge -y libreoffice*
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
# For some reason I couldn't install libgtk2.0-dev or libgtk-3-dev without running the
# following line
# See https://www.raspberrypi.org/forums/viewtopic.php?p=1254646#p1254665 for issue and resolution
sudo apt-get install -y devscripts debhelper cmake libldap2-dev libgtkmm-3.0-dev libarchive-dev \
libcurl4-openssl-dev intltool
sudo apt-get install -y build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libgtk2.0-dev libgtk-3-dev \
libatlas-base-dev libblas-dev libeigen{2,3}-dev liblapack-dev \
gfortran python2.7-dev python3-dev libgflags
sudo pip2 install -U pip
sudo pip3 install -U pip
sudo pip2 install numpy
sudo pip3 install numpy
# FROM THE INTEL OPENVINO INSTALL GUIDE FOR RASPBIAN
import cv2 as cv
# Load the model.
net = cv.dnn.readNet('face-detection-adas-0001.xml',
'face-detection-adas-0001.bin')
# Specify target device.
net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
# Read an image.
frame = cv.imread('lenna.jpg')
if frame is None:
raise Exception('Image not found!')
# Prepare input blob and perform an inference.
blob = cv.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv.CV_8U)
net.setInput(blob)
out = net.forward()
# Draw detected faces on the frame.
for detection in out.reshape(-1, 7):
confidence = float(detection[2])
xmin = int(detection[3] * frame.shape[1])
ymin = int(detection[4] * frame.shape[0])
xmax = int(detection[5] * frame.shape[1])
ymax = int(detection[6] * frame.shape[0])
if confidence > 0.5:
cv.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(0, 255, 0))
# Save the frame to an image file.
cv.imwrite('out.png', frame)
import numpy as np
import cv2
import sys
if len(sys.argv) < 2:
print("USAGE: {} img-file".format(sys.argv[0]))
sys.exit(1)
# Load an color image in grayscale
img = cv2.imread(sys.argv[1],0)
cv2.imshow('image',img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment