On my Custom Vision samples, I usually send an image to a CustomVision.ai
HTTP Endpoint, and I process the Json result. The results are very easy to
understand, however, I created a C# converter class to help with the Custom
To create this class I navigate: http://json2csharp.com/, and paste a sample
result and make some changes on the result. The output and useful class is this
It’s a very simple class, and the best way to describe it,
is to show an usage scenario
The main remarks points are
Lines 1 to 9, open a local file, create a HTTP client and make the HTTP Post request
Line 12, convert the json response (string) to a C# object and then get the best prediction
Where the best prediction is a single Linq code sorting the predictions by probability and selecting the 1t one.
Central is one of the amazing services we can use on Azure. I was wondering how
easy is to use a Raspberry Pi using Raspbian and Azure IoT and here is my
we had a device up to date using Raspbian, our next step will be to create an
Azure IoT Central application. The official step by step is the main resource
Azure IoT Central application (see references)
Once we have
our application, we can quickly create a new Raspberry Pi device and use it.
However, I’ll do an extra step, lessons learned as a handsome developer
[Device Templates] and create a new template
Raspberry Pi, I’ll name this [Raspberry Pi Dev]
So now, I
can add a new real device, in the Devices section from the left menu
create a new real device, is important to copy and save for later the
connection information. To access this, go to the top right [Connect] button
there, there is an official tutorial that explain how to send random telemetry information
with a Python script in a Raspberry Pi. I’ll use it as base for this scenario.
Raspberry Pi to your Azure IoT Central application (Python) (see references)
For this demo, I’ll add a custom telemetry property and a custom event to the device. Since I won’t use the device to track temperature, accelerometer, and more, I think it make sense to track some custom information.
So, I’ll go
back to my Device Template definition and I’ll add a new Telemetry, named [t1],
with the following information.
And now, I
can run a custom version of my script that will send new telemetry information,
for [t1]. Sample in line 18
After a couple
of minutes running the sample script, I can see the telemetry information for
T1. In this view, I enabled [Temperature] and [T1] to display the timeline.
step will be to add an event, which is also a very important uses case in Azure
IoT. Back in the Device Template, I add a new event named [event1]
And added some
extra lines of code to send also an event between telemetry, Line 22
following image, we can see how the events appears in the timeline, and we can
also get some extra details clicking on each event.
Next steps will be to integrate this with some image recognition scenarios.
to do some tests with the new Raspberry Pi 4 and the results are amazing. I’m
not a performance expert, so I decided to pick up some of the demos / apps I’ve
creating for the Raspberry Pi and run them in both models: Raspberry Pi 3 B+ and
Raspberry Pi 4.
I started with an amazing set of tutorials on how to perform Face Recognition from Adrian Rosebrock (see references). I’ve been using his Face Recognition python package for this scenarios and it’s an amazing one.
I added some code to a custom version of Adrian’s Face Recognition sample, and it looks great. The main idea was to track in real-time the current FPS (similar to the work I did with the Image AI and Hololens sample a couple of days ago, see references).
load a file with 15 trained faces and analyze frame by frame to
Detect faces in the frame.
If a face is detected, draw a frame around it.
For each detected frame analyze if the face is a trained face.
If the face is part of the trained dataset, the app will add the name of the person on top of the frame.
I display in real-time the FPS processed with a USB camera in a Raspberry Pi 3 B+. Doing a lot of tweaks and getting the best performance in the device I could never process 1FPS. The average processing data were between 0.6 and 0.9 FPS in a Raspberry Pi 3B+.
IMHO, these results are great for a small device like a Raspberry Pi 3B+. But now it was time to test it in the new Raspberry Pi 4. And an important note here is to remark that even if I did this tests in a Raspberry Pi 4 with 4GB of Rams, the performance results are similar to a RPI4 with just 1 GB of ram. We have more memory, however the processor improvements are quite significant in the new version.
I installed all the necessary software in the Raspberry Pi 4 and I got 3X better results. I’ve even tun this in a 1080p resolution to get a sense of the real processing time. The average processing data were between 2.3 and 2.4 FPS in a Raspberry Pi 4.
this scenario the Raspberry Pi 4 is almost 3 times faster than the Raspberry Pi
3. And again, these are amazing times for a 50USD device.
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.
And after yesterday’s post I realize that the code is working, but there is room for performance improvement. So, I went back to my 1st sample, the one for face detection and I added some code to get some times for Frames per Second (FPS).
In my initial code, the app was working processing almost 6 FPS. Then I started to read the code and think on improvements and I manage to get an amazing +30FPS.
So, before moving forward, I want to remark this StackOverflow post that quickly pointed me in the easiest way to do a StopWatch in Python.
My original code, was this one:
And then, I realize that I may use some of the OpenCV functions to increase the face detection process. I really don’t need to process a full HD image (1920 x 1080), I may resize the frame to a quarter size and work with this. That’s how, based on some of the samples, I got the following code:
The line 12 perform the initial resize and then I recalculate back the positions before drawing the face frame. This process works almost 6 times faster than the original one.
I’ll continue improving the code and samples, and sharing my learning path !
In my previous posts I explained how to detect faces and perform face recognition in python. Today I’ll explore another feature in the face_recognition package: Find Facial Features.
The live camera output will be something like this:
Note: Special thanks to my daughter who is always OK to help me with this.
The main sample in the source code uses a photo to detect facial features and creates a new one with the features detected. In the following sample, is amazing to check that it detect a far away face behind the main ones and also, somehow, it detect some landmarks behind my girl glasses:
I wanted to see how fast this library work to perform this with a live camera feed, and the results are very good.
I spend sometime figuring out the best way to draw lines with OpenCV, at the end the PolyLine() function is the one doing all the magic (lines 14 to 17). It took me sometime, to find the best way to deal with matrix transformations and some other performance tricks, but at the end I get this up and running in 25 lines which is kind of amazing. And the final code is very simple:
Yesterday I explained how to write a couple of lines in Python to perform live face detection in a webcam feed [Post]. Check the resources section to find more about the tools I’m using.
Today, I’ll add some more code to perform face recognition. And as usual, I’ll work with my kids to test this out. I’ll start adding 2 face encodings for Valentino and myself. The code is simple enough, and I use a simple 300×300 head-shot photo to train and get the face encoding.
The previous function returns an set of arrays with the face encodings and the face names. In the complete file, I’ll use this to analyze the camera frame (line 31) and later to check the matches for faces (lines 34 * 36)
Last lines are cosmetic to mostly draw the frames for the detected faces, and show the names.
I’ve write a lot about how to use AI models in C# to perform tasks like Face recognition, speech analysis, and more. During the Chicago CodeCamp, someone ask me about how to perform Face Recognition in Python. I didn’t have any working sample to showcase this, and I failed in try to write a 2 min app. So I added this into my ToDo list.
For this demo I’ll use Anaconda as the base Python distribution and Visual Studio Code as the code editor. There are several packages to perform face detection in Python. I’ll use a mix between OpenCV and Adam Geitgey Face Recognition package to use the camera and detect and recognize faces.
I’ll start by installing some packages to use in python app: dlib, openCV and face_recognition