Walk-through of OpenCV AI Kit using the MobileNet detection algorithm
This article builds upon my previous article where I went over a simple example of how to displaying the video feed from the OAK-1.
In this article I am going to show how to incorporate the MobileNet Detection Network. The Luxonis documentation is really good and what I am going to do is combine a couple of their tutorials and provide some explanatory commentary which I hope will make the process a little clearer. The hello-world.py
Did not include the labels and confidence, so my version of hello-world.py
, does include those so we can see how to pull those out of the detections.
The two tutorials I am going to combine and walk through are:
I have also put together a YouTube walk through as well if you interested in that. You can find that on YouTube here.
OAK Processing Pipeline
Recall from the previous article, that the OAK using a processing pipeline on the device. The pipeline processes Nodes in the order the Nodes are linked together.
In this article we are going to add a MobileNetDetectionNetwork Node to the pipeline from the previous article.
MobileNetDetectionNetwork Node
This node performs the MobileNetDetection algorithm on the input data and returns a collection of detections.
The ‘input’ should take the output of the ColorCamera so the MobileNet network can try to detect objects. We use the ‘preview’ output of the ColorCamera Node.
The ‘out’ values are a collection of ‘ImgDetection’ objects. Each detection has the following properties:
- label — MobileNet label index
- confidence — Confidence, or probability of that label
- xmin,ymin,xmax,ymax — Upper left and lower right corners of the bounding box. These values are normalized values and therefore we have to multiply the values by the shape of the frame, which for MobileNet is 300x300.
For us to display the detections we need to read the frames as we did in the previous article, and use the ImgDetection data to write the additional information and then display the frame.
MobileNet OAK Pipeline
Implementation
You can find this particular examples and others in my ‘DepthAI Sandbox Repo’. Look in the tutorials
directory.
However here is the version of the hello-world.py
as of the writing of this article.
To run the script, after you activate your Python virtual environment just execute:
python hello_world.py
You should see something like the following:
RaspberryPi
I typically run these scripts from my Mac, however I have also tested this running from a RaspberryPi. There are a couple of things to know if you want to run from your Raspberry Pi.
- You will need to install OpenCV. I have a repo where I keep scripts that can build and/or install OpenCV on a RaspberryPi. You can find that script in this repo. The file I typically run is
pi_install_imagelibs.sh.
This script will still other image libraries that I use often — so feel free to use this as a guide. - After you install OpenCV you will need to pip install:
- depthai
- blobconverter
If you try to run the script and you get an error like:
RuntimeError: Failed to find device (ma2480), error message: X_LINK_DEVICE_NOT_FOUND
Then you need to run:
echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="03e7", MODE="0666"' | sudo tee /etc/udev/rules.d/80-movidius.rules
sudo udevadm control --reload-rules && sudo udevadm trigger
As was discussed in this troubleshooting page.
Wrap Up
If you would like to see what else I am up to on the OAK, feel free to check out my sandbox repo.
I also have a YouTube video walk through where I demonstrate the script.
The OAK-1 is a really impressive device and I am looking forward to exploring more of the its capability.