Now that I started to understand how UDP works, I also did my research to find which are the best options to access an UDP video feed. Lucky for me, there are plenty of resources about doing this task using my old friend OpenCV.
Most of the OpenCV documentation is written in C++. However at the end, it all goes down to these basic lines of code
# open UDP
videoUDP = 'udp://192.168.10.1:11111'
cap = cv2.VideoCapture(videoUDP)
# read a frame from the feed
ret, frame = cap.read()
img = cv2.resize(frame, (320, 240))
# diplay the frame in openCV video window
Note: At the references section below I shared some of my posts with my experiences on how to install OpenCV in Windows 10.
Let’s go back to our sample Python App. Using the previous sample that display the battery level, I changed this code to be alive all the time and displaying the video feed in a 320*240 window.
After some posts about how to setup a Raspberry Pi, today I’ll share the steps I follow to install OpenCV.
Disclaimer: if you are looking for a detailed step by step on how to install or even build OpenCV in a Raspberry Pi, I strongly recommend to read the post “Install OpenCV on Raspberry Pi 4” by Adrian Rosebrock.
Ok, so let’s start. I assume that you read my posts and your Raspbian image is up and running.
Install Python 3 and Update device
1st step will be to install Python 3 with the following command
This process will take some minutes, so this is time 1 to get a coffee!
Install OpenCV and switch to right Raspberry Pi version!
And now the magic command to install OpenCV
sudo apt-get install libopencv-dev
And this process is the one who take most of the time, so coffee number 2. Take a look at all the dependencies for this
And after a couple of minutes the process is done. We can test the OpenCV version running 2 simple python commands. First let’s start python with the command
And then run the following lines
This should display the current OpenCV version.
However, with the latest version we have an error: ModuleNotFoundError: No module named ‘cv2’
The current installed version have some issues running in the raspberry py, so we need to make a downgrade to the version 184.108.40.206 with the command. We first uninstall the installed version (220.127.116.11) and install the specific version.
start to research the amazing world of Computer Vision, you find that there are
plenty of courses, tutorials, videos and more resources. Something is kind of “too
much”, and it’s not easy to choose where to start.
That’s why, when you arrive to one of the Adrian Rosebrock tutorials or articles, they’ll end in one of your favorites bookmarks. He has amazing detailed step by step tutorials, and I learned a lot of Raspberry Pi and OpenCV from his website.
A couple of
weeks ago, Adrian released an amazing resource for Computer Vision enthusiasts:
During the past months, I’ve been playing around with several Image Analysis tools. And ImageAI (see references) is one that deserves a full series of posts. Please take a look at the product and the source code in GitHub, and also please thank the one behind this: Moses Olafenwa (@OlafenwaMoses).
And now, my
2 cents. I’ve started to test ImageAI to create my own image detection models. Most
of the times, this is a hard path to do, however ImageAI show me an interesting
… with the latest release of ImageAI v2.1.0, support for training your custom YOLOv3 models to detect literally any kind and number of objects is now fully supported, …
mean that I can pick up my own set of images dataset and train on top of a YOLOv3
and use it as a trained model. Again, this is amazing.
So, I started to read the article [Train Object Detection AI with 6 lines of code, see references] where Olafenwa explains how to do this using a data set with almost 500 rows with images for Hololens and Oculus Rift.
The code is
very simple and easy to read. There are also examples on how to analyze a
single file, or a video, or even a camera feed. The output for the analysis can
be also in a new file, in a processed video or even a full log file with the
to read the code samples and I realized that I’m missing a scenario:
realtime feed from a webcam, analyze each webcam frame and if a device is
found, add a frame to the realtime feed to display this.
I use OpenCV
to access to my camera, and it took me some time to figure out how to convert my
OpenCV2 camera frame to the format needed by ImageAI. At the end, thanks to the
GitHub code I manage to create this (very slow but working) demo
As usual in
this scenario, now it’s time to improve the performance and start testing with
some tweaks to get a decent up and running App.
In my last post I share some lines of code which allowed me to run some of the face recognition demos 6 times faster. I added a Frames per Second (FPS) feature in my samples. Later, thinking about performance, I realize that I don’t need to work with a full HD picture (1920 x 1080), so I added some code to resize the photo before the face detection process.
However, while I was coding around this solution I also realized that I may want to initialize my camera to start in a lower resolution. So, I searched online on how to do this with OpenCV and I found 3 beautiful lines of code.
So, I manage to improve my processing code from 20FPS to +30FPS … which is very good ! Later on this posts I’ll try to do some similar FPS tests on a smaller device and I’ll see and share how this works.