Title: OpenVINO, OpenCV, and Movidius NCS on the Raspberry Pi - PyImageSearch

URL Source: https://pyimagesearch.com/2019/04/08/openvino-opencv-and-movidius-ncs-on-the-raspberry-pi/

Published Time: 2019-04-08T14:00:49+00:00

Markdown Content:
[![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/04/openvino_install_header.png)

Inside this tutorial, you will learn how to utilize the OpenVINO toolkit with OpenCV for faster deep learning inference on the Raspberry Pi.

Raspberry Pis are great — I love the quality hardware and the supportive community built around the device.

That said, for deep learning, the current Raspberry Pi hardware is inherently resource-constrained and you’ll be lucky to get more than a few FPS (using the RPi CPU alone) out of most state-of-the-art models (especially object detection and instance/semantic segmentation).

We know from my previous posts that Intel’s Movidius Neural Compute Stick allows for faster inference with the deep learning coprocessor that you plug into the USB socket:

*   [_Getting started with the Intel Movidius Neural Compute Stick_](https://pyimagesearch.com/2018/02/12/getting-started-with-the-intel-movidius-neural-compute-stick/)
*   [_Real-time object detection on the Raspberry Pi with the Movidius NCS_](https://pyimagesearch.com/2018/02/19/real-time-object-detection-on-the-raspberry-pi-with-the-movidius-ncs/)

Since 2017, the Movidius team has been hard at work on their Myriad processors and their consumer-grade USB deep learning sticks.

The first version of the API that came with the sticks worked well and demonstrated the power of the Myriad, but left a lot to be desired.

Then, the Movidius APIv2 was released and welcomed by the Movidius + Raspberry Pi community. It was easier/more reliable than the APIv1 but had its fair share of issues as well.

But now, it’s become _easier than ever_ to work with the Movidius NCS, _especially_ with OpenCV.

**Meet OpenVINO, an Intel library for hardware optimized computer vision designed to replace the V1 and V2 APIs.**

Intel’s shift to support the Movidius hardware with OpenVINO software makes the Movidius shine in all of its metallic blue glory.

OpenVINO is extremely simple to use — just set the target processor (a single function call) and let OpenVINO-optimized OpenCV handle the rest.

But the question remains:

> How can I install OpenVINO on the Raspberry Pi?

Today we’ll learn just that, along with a practical object detection demo (spoiler alert: it is dead simple to use the Movidius coprocessor now).

_**Update 2020-04-06:** There are a number of updates in this tutorial to ensure compatibility with OpenVINO 4.2.0._

**To learn how to install OpenVINO on the Raspberry Pi (and perform object detection with the Movidius Neural Compute Stick), _just follow this tutorial!_**

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

In this blog post, we’re going to cover three main topics.

1.  First, we’ll learn what OpenVINO is and how it is a very welcome paradigm shift for the Raspberry Pi.
2.  We’ll then cover how to install OpenCV and OpenVINO on your Raspberry Pi.
3.  Finally, we’ll develop a real-time object detection script using OpenVINO, OpenCV, and the Movidius NCS.

_**Note:**_ _There are many Raspberry Pi install guides on my blog, most unrelated to Movidius. Before you begin, be sure to check out the available install tutorials on my **[OpenCV installation guides](https://pyimagesearch.com/opencv-tutorials-resources-guides/)** page and choose the one that best fits your needs._

Let’s get started.

### What is OpenVINO?

[![Image 3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20733'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/04/openvino_install_intel_webpage.jpg)

**Figure 1:** The Intel OpenVINO toolkit optimizes your computer vision apps for Intel hardware such as the Movidius Neural Compute Stick. Real-time object detection with OpenVINO and OpenCV using Raspberry Pi and Movidius NCS sees a significant speedup. ([source](https://software.intel.com/en-us/openvino-toolkit))

Intel’s OpenVINO is an acceleration library for optimized computing with Intel’s hardware portfolio.

OpenVINO supports Intel CPUs, GPUs, FPGAs, and VPUs.

Deep learning libraries you’ve come to rely upon such as TensorFlow, Caffe, and mxnet are supported by OpenVINO.

[![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20272'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/04/openvino_install_whats_inside.jpg)

**Figure 2:** The Intel OpenVINO Toolkit supports intel CPUs, GPUs, FPGAs, and VPUs. TensorFlow, Caffe, mxnet, and OpenCV’s DNN module all are optimized and accelerated for Intel hardware. The Movidius line of vision processing units (VPUs) are supported by OpenVINO and pair well with the Raspberry Pi. (source: [OpenVINO Product Brief](https://www.intel.in/content/www/in/en/internet-of-things/solution-briefs/openvino-toolkit-product-brief.html))

**Intel has even optimized OpenCV’s DNN module to support its hardware for deep learning.**

In fact, many newer smart cameras use Intel’s hardware along with the OpenVINO toolkit. OpenVINO is **edge computing** and **IoT** at its finest — it enables resource-constrained devices like the Raspberry Pi to work with the Movidius coprocessor to perform deep learning at speeds that are useful for real-world applications.

We’ll be installing OpenVINO on the Raspberry Pi so it can be used with the Movidius VPU (Vision Processing Unit) in the next section.

Be sure to read the [OpenVINO product brief PDF for more information](https://www.intel.in/content/www/in/en/internet-of-things/solution-briefs/openvino-toolkit-product-brief.html).

### Installing OpenVINO’s optimized OpenCV on the Raspberry Pi

In this section, we’ll cover prerequisites and all steps required to install OpenCV and OpenVINO on your Raspberry Pi.

Be sure to read this entire section before you begin so that you are familiar with the steps required.

Let’s begin.

#### Hardware, assumptions, and prerequisites

In this tutorial, I am going to assume that you have the following hardware:

*   **Raspberry 4B or 3B+** (running Raspbian Buster)
*   **Movidius NCS 2** (or Movidius NCS 1)
*   **PiCamera V2** (or USB webcam)
*   **32GB microSD card _with_ Raspbian Stretch freshly flashed**
*   **HDMI screen + keyboard/mouse** (at least for the initial WiFi configuration)
*   **5V power supply** (I recommend a 2.5A supply because the Movidius NCS is a power hog)

If you don’t have a microSD with a fresh burn of Raspbian Stretch, you may [download it here](https://www.raspberrypi.org/downloads/raspbian/). I recommend the full install:

[![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20651'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/09/install_opencv4_buster_download_os.jpg)

**Figure 3:** Download Raspbian Buster for OpenVINO on your Raspberry Pi and Movidius NCS.

From there, use [balenaEtcher](https://www.balena.io/etcher/) (or a suitable alternative) to flash the card.

Once you’re ready, insert the microSD card into your Raspberry Pi and boot it up.

Enter your WiFi credentials and enable SSH, VNC, and the camera interface.

From here you will need one of the following:

*   **_Physical access_** to your Raspberry Pi so that you can open up a terminal and execute commands
*   **_Remote access_** via SSH or VNC

I’ll be doing the majority of this tutorial via SSH, but as long as you have access to a terminal, you can easily follow along.

_**Can’t SSH?**_ If you see your Pi on your network, but can’t ssh to it, you may need to enable SSH. This can easily be done via the Raspberry Pi desktop preferences menu or using the `raspi-config` command.

After you’ve changed the setting and rebooted, you can test SSH directly on the Pi with the localhost address.

Open a terminal and type `ssh pi@127.0.0.1` to see if it is working. To SSH from another computer you’ll need the Pi’s IP address — you can determine the IP address by looking at your router’s clients page or by running `ifconfig` to determine the IP on/of the Pi itself.

_**Is your Raspberry Pi keyboard layout giving you problems?**_ Change your keyboard layout by going to the Raspberry Pi desktop preferences menu. I use the standard US Keyboard layout, but you’ll want to select the one appropriate for you.

#### Step #1: Expand filesystem on your Raspberry Pi

To get the OpenVINO party started, fire up your Raspberry Pi and open an SSH connection (alternatively use the Raspbian desktop with a keyboard + mouse and launch a terminal).

If you’ve just flashed Raspbian Stretch, I always recommend that you first check to ensure your filesystem is using all available space on the microSD card.

To check your disk space usage execute the `df -h` command in your terminal and examine the output:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        30G  4.2G   24G  15% /
devtmpfs        434M     0  434M   0% /dev
tmpfs           438M     0  438M   0% /dev/shm
tmpfs           438M   12M  427M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           438M     0  438M   0% /sys/fs/cgroup
/dev/mmcblk0p1   42M   21M   21M  51% /boot
tmpfs            88M     0   88M   0% /run/user/1000

As you can see, my Raspbian filesystem has been automatically expanded to include all 32GB of the micro-SD card. This is denoted by the fact that the size is 30GB (nearly 32GB) and I have 24GB available (15% usage).

**If you’re seeing that you aren’t using your entire memory card capacity, below you can find instructions on how to expand the filesystem.**

Open up the Raspberry Pi configuration in your terminal:

$ sudo raspi-config

And then select the _“Advanced Options”_ menu item:

[![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20453'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-rpi-raspiconfig.jpg)

**Figure 4:** Selecting the “Advanced Options” from the `raspi-config` menu to expand the Raspbian file system on your Raspberry Pi is important before installing OpenVINO and OpenCV. Next, we’ll actually expand the filesystem.

Followed by selecting _“Expand filesystem”_:

[![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20453'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2018/08/install-opencv4-rpi-expandfs.jpg)

**Figure 5:** The Raspberry Pi “Expand Filesystem” menu allows us to take advantage of our entire flash memory card. This will give us the space necessary to install OpenVINO, OpenCV, and other packages.

Once prompted, you should select the first option, _**“A1. Expand File System”**_, _**hit Enter**_ on your keyboard, arrow down to the _**“<Finish>”**_ button, and then reboot your Pi — you will be prompted to reboot. Alternatively, you can reboot from the terminal:

$ sudo reboot

Be sure to run the `df -h` command again to check that your file system is expanded.

#### Step #2: Reclaim space on your Raspberry Pi

One simple way to gain more space on your Raspberry Pi is to delete both LibreOffice and Wolfram engine to free up some space on your Pi:

$ sudo apt-get purge wolfram-engine
$ sudo apt-get purge libreoffice\*
$ sudo apt-get clean
$ sudo apt-get autoremove

After removing the Wolfram Engine and LibreOffice, **you can reclaim almost 1GB!**

#### Step #3: Install OpenVINO + OpenCV dependencies on your Raspberry Pi

This step shows some dependencies which I install on every OpenCV system. While you’ll soon see that OpenVINO is already compiled, I recommend that you go ahead and install these packages anyway in case you end up [compiling OpenCV from scratch](https://pyimagesearch.com/opencv-tutorials-resources-guides/) at any time going forward.

Let’s update our system:

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

And then install developer tools including [CMake](https://cmake.org/):

$ sudo apt-get install build-essential cmake unzip pkg-config

Next, it is time to install a selection of image and video libraries — these are _key_ to being able to work with image and video files:

$ 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

From there, let’s install GTK, our GUI backend:

$ sudo apt-get install libgtk-3-dev

And now let’s install a package which may help to reduce GTK warnings:

$ sudo apt-get install libcanberra-gtk\*

The asterisk ensures we will grab the ARM-specific GTK. It is required.

Now we need two packages which contain numerical optimizations for OpenCV:

$ sudo apt-get install libatlas-base-dev gfortran

And finally, let’s install the Python 3 development headers:

$ sudo apt-get install python3-dev

Once you have all of these prerequisites installed you can move on to the next step.

#### Step #4: Download and unpack OpenVINO for your Raspberry Pi

[![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20398'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/04/openvino_install_brief.jpg)

**Figure 6:** Download and install the OpenVINO toolkit for Raspberry Pi and Movidius computer vision apps (source: [Intel’s OpenVINO Product Brief)](https://www.intel.in/content/www/in/en/internet-of-things/solution-briefs/openvino-toolkit-product-brief.html).

From here forward, our install instructions are largely based upon Intel’s Raspberry Pi OpenVINO guide. There are a few “gotchas” which is why I decided to write a guide. We’ll also use virtual environments as PyImageSearch readers have come to expect.

Our next step is to download OpenVINO.

Let’s navigate to our home folder and create a new directory

$ cd ~

From there, go ahead and grab the OpenVINO Toolkit via `wget` :

$ wget https://download.01.org/opencv/2020/openvinotoolkit/2020.1/l\_openvino\_toolkit\_runtime\_raspbian\_p\_2020.1.023.tgz

_**Update 2020-04-06:** The Download URL has changed; the new URL is reflected._

Once you have successfully downloaded the OpenVINO toolkit, you can unarchive it using the following command:

$ tar -xf l\_openvino\_toolkit\_runtime\_raspbian\_p\_2020.1.023.tgz
...
$ mv l\_openvino\_toolkit\_runtime\_raspbian\_p\_2020.1.023 openvino

#### Step #5: Configure OpenVINO on your Raspberry Pi

Let’s use `nano` to edit our `~/.bashrc` . We will add a line to load OpenVINO’s `setupvars.sh` each time you invoke a Pi terminal. Go ahead and open the file:

$ nano ~/.bashrc

Scroll to the bottom and add the following lines:

\# OpenVINO
source ~/openvino/bin/setupvars.sh

Save and exit from the nano text editor.

Then, go ahead and `source` your `~/.bashrc` file:

$ source ~/.bashrc

#### Step #6: Configure USB rules for your Movidius NCS and OpenVINO on Raspberry Pi

OpenVINO requires that we set custom USB rules. It is quite straightforward, so let’s get started.

First, enter the following command to add the current user to the Raspbian “users” group:

$ sudo usermod -a -G users "$(whoami)"

Then logout and log back in. If you’re on SSH, you can type `exit` and then re-establish your SSH connection. Rebooting is also an option via `sudo reboot now` .

Once you’re back at your terminal, run the following script to set the USB rules:

$ cd ~
$ sh openvino/install\_dependencies/install\_NCS\_udev\_rules.sh

#### Step #7: Create an OpenVINO virtual environment on Raspberry Pi

Let’s grab and install pip, a Python Package Manager.

To install pip, simply enter the following in your terminal:

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py

We’ll be making use of virtual environments for Python development with OpenCV and OpenVINO.

If you aren’t familiar with virtual environments, please take a moment look at this [article on RealPython](https://realpython.com/blog/python/python-virtual-environments-a-primer/) or read the first half of [this blog post on PyImageSearch](https://pyimagesearch.com/2016/05/02/accessing-rpi-gpio-and-gpio-zero-with-opencv-python/).

Virtual environments will allow you to run independent, sequestered Python environments in isolation on your system. Today we’ll be setting up just one environment, but you could easily have an environment for each project.

Let’s go ahead and install `virtualenv` and `virtualenvwrapper` now — they allow for Python virtual environments:

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pip

To finish the install of these tools, we need to update our `~/.bashrc` again:

$ nano ~/.bashrc

Then add the following lines:

\# virtualenv and virtualenvwrapper
export WORKON\_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER\_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
VIRTUALENVWRAPPER\_ENV\_BIN\_DIR=bin

[![Image 9](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20422'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/04/openvino_install_bashrc_openvino_virtualenvwrapper.jpg)

**Figure 7:** Our Raspberry Pi `~/.bashrc` profile has been updated to accommodate OpenVINO and virtualenvwrapper. Now we’ll be able to create a virtual environment for Python packages.

Alternatively, you can append the lines directly via bash commands:

$ echo -e "\\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
$ echo "export WORKON\_HOME=$HOME/.virtualenvs" >> ~/.bashrc
$ echo "export VIRTUALENVWRAPPER\_PYTHON=/usr/bin/python3" >> ~/.bashrc
$ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
$ echo "VIRTUALENVWRAPPER\_ENV\_BIN\_DIR=bin" >> ~/.bashrc

Next, source the `~/.bashrc` profile:

$ source ~/.bashrc

**Let’s now create a virtual environment to hold OpenVINO, OpenCV and related packages:**

$ mkvirtualenv openvino -p python3

This command simply creates a Python 3 virtual environment named `openvino` .

You can (and should) name your environment(s) whatever you’d like — I like to keep them short and sweet while also providing enough information so I’ll remember what they are for.

#### Step #8: Install packages into your OpenVINO environment

Let’s install a handful of packages required for today’s demo script

$ workon openvino
$ pip install numpy
$ pip install "picamera\[array\]"
$ pip install imutils

Now that we’ve installed these packages in the `openvino` virtual environment, they are only available in the `openvino` environment. This is your sequestered area to work on OpenVINO projects (we use Python virtual environments here so we don’t risk ruining your system install of Python).

Additional packages for Caffe, TensorFlow, and mxnet may be installed via requirements.txt files using pip. You can read more about it at [this Intel documentation link](https://software.intel.com/en-us/articles/OpenVINO-ModelOptimizer#inpage-nav-2-2). This is _**not**_ required for today’s tutorial.

#### Step #9: Test your OpenVINO install on your Raspberry Pi

Let’s do a quick sanity test to see if OpenCV is ready to go before we try an OpenVINO example.

Open a terminal and perform the following:

$ workon openvino
$ source ~/openvino/bin/setupvars.sh
$ python
>>> import cv2
>>> cv2.\_\_version\_\_
'4.2.0-openvino'
>>> exit()

The first command activates our OpenVINO virtual environment. The second command sets up the Movidius NCS with OpenVINO and is very important. From there we fire up the Python 3 binary in the environment and import OpenCV.

**The version of OpenCV indicates that it is an OpenVINO optimized install!**

### Recommended: Create a shell script for starting your OpenVINO environment

In this section, we’ll create a shell script just like the ones that come on my **[Pre-configured and Pre-installed Raspbian .img](https://pyimagesearch.com/2016/11/21/raspbian-opencv-pre-configured-and-pre-installed/)**.

Open a new file named `start_openvino.sh` and place it in your `~/` directory. Insert the following lines:

#!/bin/bash
echo "Starting Python 3.7 with OpenCV-OpenVINO 4.2.0 bindings..."
source ~/openvino/bin/setupvars.sh
workon openvino

Save and close the file.

From here on, you can _activate your OpenVINO_ environment with **one simple command** (as opposed to two commands like in the previous step:

$ source ~/start\_openvino.sh
Starting Python 3.7 with OpenCV-OpenVINO 4.2.0 bindings...

### Real-time object detection with Raspberry Pi and OpenVINO

Installing OpenVINO was pretty easy and didn’t even require a compile of OpenCV. The Intel team did a great job!

Now let’s put the Movidius Neural Compute Stick to work using OpenVINO.

For comparison’s sake, we’ll run the MobileNet SSD object detector _with_ and _without_ the Movidius to benchmark our FPS. We’ll compare the values to previous results of using Movidius NCS APIv1 ([the non-OpenVINO method that I wrote about in early 2018](https://pyimagesearch.com/2018/02/19/real-time-object-detection-on-the-raspberry-pi-with-the-movidius-ncs/)).

Let’s get started!

#### Project structure

Go ahead and grab the _**“Downloads”**_ for today’s blog post.

Once you’ve extracted the zip, you can use the `tree` command to inspect the project directory:

$ tree
.
├── MobileNetSSD\_deploy.caffemodel
├── MobileNetSSD\_deploy.prototxt
├── openvino\_real\_time\_object\_detection.py
└── real\_time\_object\_detection.py

0 directories, 3 files

Our MobileNet SSD object detector files include the .caffemodel and .prototxt.txt files. These are pretrained (we will not be training MobileNet SSD today).

We’re going to review the `openvino_real_time_object_detection.py` script and compare it to the original real-time object detection script (`real_time_object_detection.py` ).

#### Real-time object detection with OpenVINO, Movidius NCS, and Raspberry Pi

To demonstrate the power of OpenVINO on the Raspberry Pi with Movidius, we’re going to perform **real-time deep learning object detection.**

The Movidius/Myriad coprocessor will perform the actual deep learning inference, reducing the load on the Pi’s CPU.

We’ll still use the Raspberry Pi CPU to process the results and tell the Movidius what to do, but we’re reserving deep learning inference for the Myriad as its hardware is optimized and designed for deep learning inference.

As previously discussed in the _“What is OpenVINO?”_ section, OpenVINO with OpenCV allows us to specify the processor for inference when using the OpenCV “DNN” module.

**In fact, it only requires _one line of code_ (typically) to use the Movidius NCS Myriad processor.**

**From there, the rest of the code is the same!**

On the PyImageSearch blog I provide a detailed walkthrough of all Python scripts.

This is one of the few posts where I’ve decided to _deviate from my typical format._

This post is first and foremost an _install + configuration_ post. Therefore I’m going to skip over the details and instead demonstrate the power of OpenVINO by highlighting new lines of code inserted into a [previous blog post](https://pyimagesearch.com/2017/09/18/real-time-object-detection-with-deep-learning-and-opencv/) (where all details are provided).

Please review that post if you want to get into the weeds with [_Real-time object detection with deep learning and OpenCV_](https://pyimagesearch.com/2017/09/18/real-time-object-detection-with-deep-learning-and-opencv/) where I demonstrated the concept of using OpenCV’s DNN module in **just 100 lines of code.**

Today, we’re adding just one line of code that performs computation (and a comment + blank line). This brings the **new total** to **_103_ lines of code** [without using the previous complex Movidius APIv1](https://pyimagesearch.com/2018/02/19/real-time-object-detection-on-the-raspberry-pi-with-the-movidius-ncs/) (**215 lines of code**).

If this is your first foray into OpenVINO, I think you’ll be just as astounded and pleased as I was when I learned how easy it is.

Let’s learn the changes necessary to accommodate OpenVINO’s API with OpenCV and Movidius.

Go ahead and open a file named `openvino_real_time_object_detection.py` and insert the following lines, paying close attention to **Lines 33-35** (highlighted in yellow):

\# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import time
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-p", "--prototxt", required=True,
	help="path to Caffe 'deploy' prototxt file")
ap.add\_argument("-m", "--model", required=True,
	help="path to Caffe pre-trained model")
ap.add\_argument("-c", "--confidence", type=float, default=0.2,
	help="minimum probability to filter weak detections")
ap.add\_argument("-u", "--movidius", type=bool, default=0,
	help="boolean indicating if the Movidius should be used")
args = vars(ap.parse\_args())

# initialize the list of class labels MobileNet SSD was trained to
# detect, then generate a set of bounding box colors for each class
CLASSES = \["background", "aeroplane", "bicycle", "bird", "boat",
	"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
	"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
	"sofa", "train", "tvmonitor"\]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

# load our serialized model from disk
print("\[INFO\] loading model...")
net = cv2.dnn.readNetFromCaffe(args\["prototxt"\], args\["model"\])

# specify the target device as the Myriad processor on the NCS
net.setPreferableTarget(cv2.dnn.DNN\_TARGET\_MYRIAD)

# initialize the video stream, allow the cammera sensor to warmup,
# and initialize the FPS counter
print("\[INFO\] starting video stream...")
vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)
fps = FPS().start()

# loop over the frames from the video stream
while True:
	# grab the frame from the threaded video stream and resize it
	# to have a maximum width of 400 pixels
	frame = vs.read()
	frame = imutils.resize(frame, width=400)

	# grab the frame dimensions and convert it to a blob
	(h, w) = frame.shape\[:2\]
	blob = cv2.dnn.blobFromImage(frame, 0.007843, (300, 300), 127.5)

	# pass the blob through the network and obtain the detections and
	# predictions
	net.setInput(blob)
	detections = net.forward()

	# loop over the detections
	for i in np.arange(0, detections.shape\[2\]):
		# extract the confidence (i.e., probability) associated with
		# the prediction
		confidence = detections\[0, 0, i, 2\]

		# filter out weak detections by ensuring the \`confidence\` is
		# greater than the minimum confidence
		if confidence > args\["confidence"\]:
			# extract the index of the class label from the
			# \`detections\`, then compute the (x, y)-coordinates of
			# the bounding box for the object
			idx = int(detections\[0, 0, i, 1\])
			box = detections\[0, 0, i, 3:7\] \* np.array(\[w, h, w, h\])
			(startX, startY, endX, endY) = box.astype("int")

			# draw the prediction on the frame
			label = "{}: {:.2f}%".format(CLASSES\[idx\],
				confidence \* 100)
			cv2.rectangle(frame, (startX, startY), (endX, endY),
				COLORS\[idx\], 2)
			y = startY - 15 if startY - 15 > 15 else startY + 15
			cv2.putText(frame, label, (startX, y),
				cv2.FONT\_HERSHEY\_SIMPLEX, 0.5, COLORS\[idx\], 2)

	# show the output frame
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF

	# if the \`q\` key was pressed, break from the loop
	if key == ord("q"):
		break

	# update the FPS counter
	fps.update()

# stop the timer and display FPS information
fps.stop()
print("\[INFO\] elasped time: {:.2f}".format(fps.elapsed()))
print("\[INFO\] approx. FPS: {:.2f}".format(fps.fps()))

# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

**Lines 33-35** (highlighted in yellow) are new. But only one of those lines is interesting.

On **Line 35**, we tell OpenCV’s DNN module to use the Myriad coprocessor using `net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)` .

The Myriad processor is built into the Movidius Neural Compute Stick. You can use this same method if you’re running OpenVINO + OpenCV on a device with an embedded Myriad chip (i.e. without the bulky USB stick).

For a detailed explanation on the code, be sure to [refer to this post](https://pyimagesearch.com/2017/09/18/real-time-object-detection-with-deep-learning-and-opencv/).

Also, be sure to refer to [this Movidius APIv1 blog post from early 2018](https://pyimagesearch.com/2018/02/19/real-time-object-detection-on-the-raspberry-pi-with-the-movidius-ncs/) where I demonstrated object detection using Movidius and the Raspberry Pi. It’s incredible that 215 lines of significantly more complicated code are required for the previous Movidius API, in comparison to 103 lines of much easier to follow code using OpenVINO.

I think those line number differences speak for themselves in terms of reduced complexity, time, and development cost savings, _**but what are the actual results?**_ **How fast is OpenVINO with Movidius?**

Let’s find out in the next section.

#### OpenVINO object detection results

![Image 10](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20350%20343'%3E%3C/svg%3E)

**Figure 8:** Object detection with OpenVINO, OpenCV, and the Raspberry Pi.

To run today’s script, first, you’ll need to grab the _**“Downloads”**_ associated with this post.

From there, unpack the zip and navigate into the directory.

Activate your virtual environment using the recommended method above:

$ source ~/start\_openvino.sh
Starting Python 3.7 with OpenCV-OpenVINO 4.2.0 bindings...

To perform object detection with OpenVINO, just execute the following command:

$ python openvino\_real\_time\_object\_detection.py
	--prototxt MobileNetSSD\_deploy.prototxt \\
	--model MobileNetSSD\_deploy.caffemodel
\[INFO\] loading model...
\[INFO\] starting video stream...
\[INFO\] elasped time: 55.35
\[INFO\] approx. FPS: 8.31

As you can see, we’re reaching **8.31FPS** over approximately one minute.

I’ve gathered additional results using MobileNet SSD as shown in the table below:

[![Image 11](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20314'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2019/04/openvino_install_benchmark.jpg)

**Figure 9:** A benchmark comparison of the MobileNet SSD deep learning object detector using OpenVINO with the Movidius Neural Compute Stick.

**OpenVINO and the Movidius NCS 2 are _very fast_, a _huge_ speedup from previous versions.**

It’s amazing that the results are > 8x in comparison to using only the RPi 3B+ CPU (no Movidius coprocessor).

The two rightmost columns (light blue columns 3 and 4) show the OpenVINO comparison between the NCS1 and the NCS2.

Note that the 2nd column statistic is with the RPi 3B (not the 3B+). It was taken in February 2018 using the previous API and previous RPi hardware.

So, what’s next?
----------------

![Image 12](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20500%20667'%3E%3C/svg%3E)

**Figure 10:** Check out my new book, [_Raspberry Pi for Computer Vision_](https://pyimagesearch.com/raspberry-pi-for-computer-vision/) to learn about computer vision and deep learning on embedded and internet of things (IoT) devices.

I’ve written a new book to maximize computer vision + deep learning capability on resource-constrained devices such as the Raspberry Pi single board computer (SBC).

Inside, you’ll learn and develop your skills using techniques that I’ve amassed through my years of working with computer vision on the Raspberry Pi, Intel Movidius NCS, Google Coral EdgeTPU, NVIDIA Jetson Nano, and more.

The book covers over 40 projects (including 60+ chapters) on embedded Computer Vision and Deep Learning.

A handful of the highlighted projects include:

*   **Traffic counting** and **vehicle speed** detection
*   Real-time face recognition
*   Building a **classroom attendance** system
*   Automatic **hand gesture recognition**
*   Daytime and nighttime **wildlife monitoring**
*   **Security** applications
*   Deep Learning **classification**, **object detection,** and **human pose estimation** on resource-constrained devices
*   _… and much more!_

As a bonus, included are [**pre-configured Raspbian .img files**](https://pyimagesearch.com/2016/11/21/raspbian-opencv-pre-configured-and-pre-installed/) (for the Raspberry Pi 4B/3B+/3B and Raspberry Pi Zero W) and [**pre-configured Jetson Nano .img files**](https://pyimagesearch.com/2020/03/04/nvidia-jetson-nano-img-pre-configured-for-deep-learning-and-computer-vision/) (for the NVIDIA Jetson Nano A02/B01) so you can skip the tedious installation headaches and get to the fun part (code and deployment).

If you’re just as excited as I am, grab the _free_ table of contents by [clicking here](https://pyimagesearch.com/raspberry-pi-for-computer-vision/):

[Send me the free PDF!](https://pyimagesearch.com/raspberry-pi-for-computer-vision/)

### Troubleshooting and Frequently Asked Questions (FAQ)

Did you encounter an error installing OpenCV and OpenVINO on your Raspberry Pi?

**Don’t become frustrated.**

The first time you install the software on your Raspberry Pi it can be very frustrating. The last thing I want for you to do is give up!

Here are some common question and answers — be sure to read them and see if they apply to you.

_**Q.**_ How do I flash an operating system on to my Raspberry Pi memory card?

_**A.**_ I recommend that you:

*   Grab a 32GB memory card. The SanDisk 32GB 98MB/s microSD cards work really well and are what I recommend.
*   Flash Raspbian Stretch with Etcher to the card. Etcher is supported by most major operating systems.
*   Insert the card into your Raspberry Pi and begin with the _“Assumptions”_ and _“Step 1” sections_ in this blog post.

_**Q.**_ Can I use Python 2.7?

_**A.**_ Python 2.7 reached [sunset on January 1st, 2020](https://www.python.org/doc/sunset-python-2/). I would not advise using it.

_**Q.**_ Why can’t I just apt-get install OpenCV and have OpenVINO support?

_**A.**_ Avoid this “solution” _at all costs_ even though it _might_ work. First, this method likely won’t install OpenVINO until it is more popular. Secondly, apt-get doesn’t play nice with virtual environments and you won’t have control over your compile and build.

_**Q.**_ The  `mkvirtualenv`  and `workon` commands yield a “command not found error”. I’m not sure what to do next.

_**A.**_ There a number of reasons why you would be seeing this error message, all of come from to **Step #4:**

1.  First, ensure you have installed `virtualenv`  and `virtualenvwrapper`  properly using the `pip`  package manager. Verify by running `pip freeze`  and ensure that you see both `virtualenv`  and `virtualenvwrapper`  are in the list of installed packages.
2.  Your `~/.bashrc`  file may have mistakes. Examine the contents of your `~/.bashrc`  file to see the proper `export`  and `source`  commands are present (check **Step #4** for the commands that should be appended to `~/.bashrc` ).
3.  You might have forgotten to `source`  your `~/.bashrc` . Make sure you run `source ~/.bashrc`  after editing it to ensure you have access to the `mkvirtualenv`  and `workon`  commands.

_**Q.**_ When I open a new terminal, logout, or reboot my Raspberry Pi, I cannot execute the `mkvirtualenv`  or `workon`  commands.

_**A.**_ If you’re on the Raspbian desktop, this will likely occur. The default profile that is loaded when you launch a terminal, for some reason, doesn’t source the `~/.bashrc`  file. Please refer to **#2** from the previous question. Over SSH, you probably won’t run into this.

_**Q.**_ When I try to import OpenCV, I encounter this message: `Import Error: No module named cv2` .

_**A.**_ There are _several_ reasons this could be happening and unfortunately, it is hard to diagnose. I recommend the following suggestions to help diagnose and resolve the error:

1.  Ensure your `openvino`  virtual environment is active by using the `workon openvino`  and `source setupvars.sh`  commands. If this command gives you an error, then verify that `virtualenv`  and `virtualenvwrapper`  are properly installed.
2.  Try investigating the contents of the `site-packages`  directory in your `openvino`  virtual environment. You can find the `site-packages`  directory in `~/.virtualenvs/openvino/lib/python3.5/site-packages/` . Ensure (1) there is a `cv2`  sym-link directory in the `site-packages`  directory and (2) it’s properly sym-linked.
3.  Be sure to `find`  the `cv2*.so`  file as demonstrated in **Step #6**.

_**Q.**_ **What if my question isn’t listed here?**

_**A.**_ Please leave a comment below or [send me an email](https://pyimagesearch.com/contact/). If you post a comment below, just be aware that _code doesn’t format well in the comment form and I may have to respond to you via email instead_.

Looking for more free OpenVINO content?
---------------------------------------

I have a number of [Intel Movidius / OpenVINO blog posts](https://pyimagesearch.com/category/movidius/) for your enjoyment here on PyImageSearch.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

Today we learned about Intel’s OpenVINO toolkit and how it can be used to improve deep learning inference speed on the Raspberry Pi.

You also learned how to install the OpenVINO toolkit, including the OpenVINO-optimized version of OpenCV on the Raspberry Pi.

We then ran a simple MobileNet SSD deep learning object detection model. **It only required one line of code** to set the target device to the Myriad processor on the Movidius stick.

We also demonstrated that the Movidius NCS + OpenVINO is quite fast, _dramatically_ outperforming object detection speed on the Raspberry Pi’s CPU.

And if you’re interested in learning more about how to build real-world computer vision + deep learning projects on the Raspberry Pi, be sure to check out my new book, _[Raspberry Pi for Computer Vision](https://pyimagesearch.com/raspberry-pi-for-computer-vision/)._

**To download the source code to this post (and be notified when future tutorials are published here on PyImageSearch), _just drop your email in the form below!_**

![Image 13](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!