Today code objective is very simple, based on a request I received from internet:
The drone is flying very happy, but if the camera detects a face, the drone will flip out !
Let’s take a look at the program working:
This one is very similar to the previous one. I also realized that I may need a better camera to record the live action side by side with the drone footage, but I think you get the idea. The command to make the drone flip is “flip x”, where “x” is the direction. In example:
"flip l" # flip left
"flip r" # flip right
"flip f" # flip forward
"flip b" # flip back
Here is the code:
As I promised last time, in next posts, I’ll analyze more in details how this works, and a couple of improvements that I can implement.
Back to some drone posts! I was kind of busy during the last weeks and now I can get back to write about the drone.
OK, in the last posts I described how to connect and work with the drone camera feed using OpenCV. Now with 2 extra lines of code we can also detect faces. Let’s take a look at the final sample.
In the previous image we can see 2 camera feeds. My computer webcam, where you can see how I hold the drone with the drone camera pointing to my face. And the drone camera feed, presented using OpenCV and drawing a frame over each detected face.
Let’s share some code insights:
As usual, I resize the camera feed to 320 x 240
The average processing time is between 40 and 70 FPS
I use a haar cascade classifier to detect the faces in each frame
Note: I need to write about Haar Cascades as part of my face detection post series.
In my next posts, I’ll add some drone specific behaviors for each face detected.
I’ve been looking to use the amazing Intel Neural Stick 2 for a while, and one of the 1st ideas that I have was to check how fast my Raspberry Pi 4 can run using this device.
The Intel team released a nice step by step process installation for Raspberry Pi. And it works great, there are a couple of minor glitches that you need to figure out, like the latest package version, everything else works great.
Once installed, the 1st python sample is a face recognition one. This sample analyzes a image file using OpenCV to detect faces, and creates a new output file with the detected images. As I said, is very straight forward.
So, I decided to create a new python sample to run live face detection using the camera feed and also display the FPS. This is the output code:
The code is very straight forward and the main matters are
It uses 2 models from the Intel Zoo to perform the face detection: face-detection-adas-0001.xml and face-detection-adas-0001.bin
Lines 22 and 23 are key to define that OpenCV will load and use the models in the Intel device
I use imutils to resize the image to 640×480. Feel free to use any other library for this, even OpenCV
Also, it works also with smaller resolutions, however 640×480 is good for this demo
And the final app running analyzing almost 8 frames per second (8 FPS).
Which is almost 10 times faster that the 0.7 FPS without Intel NCS2
And, I already wrote about running Visual Studio Code in the Raspberry Pi (see references) is an amazing experience. I did all my Python in VSCode coding remote accesing my device via VNC. Python runs like a charm!
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
The advances in Computer Vision are becoming more and more impressive. The suite I know best and with use more is Azure Cognitive services, however, there are surprises and advances that leave me with my mouth open.
This is the case of the work published by Aaron S. Jackson, Adrian encyclical, Vasileios Argyriou and Georgios Tzimiropoulos, where he explains how he can create a 3d model from a 2d photo. The best thing is to see it in action
I recommend you also see the video where they apply the algorithm in real-time to faces in a video.
Now is the time to try to explain, with my words of a 5-year-old boy, how this works. Behind this algorithm is a Convolutional Neural Network (CNN), which has been trained with 2D images with the results expected in 3D. The interesting thing about this model is that it has reached such a level of sophistication that it does not need a specific point of reference for a face, it works on any face.
With the 2D image information, it is possible to rebuild elements of the face, including parts that are not seen in the 2D image. In this way, and after much training CNN, achieve the results that can be seen in the live demo!
Los avances en Computer Vision son cada vez más impresionantes. La suite que mejor conozco y con la que trabajo son los servicios de Cognitive Services, sin embargo, hay sorpresas y avances que me dejan con la boca abierta.
Este es el caso del trabajo que han publicado Aaron S. Jackson, Adrian Bulat, Vasileios Argyriou and Georgios Tzimiropoulos, donde explica cómo puede crear un modelo 3D a partir de una foto 2D. Lo mejor es verlo en acción
Les recomiendo ver también el video donde aplica el algoritmo en tiempo real a faces en un video. Ahora es el momento de intentar explicar, con mis palabras de un niño de 5 años, como funciona esto.
Detrás de este algoritmo hay una Convolutional Neural Network (CNN), que ha sido entrenada con imágenes en 2D con los resultados esperados en 3D. Lo interesante de este modelo, es que ha llegado a un nivel de sofisticación tal, que no necesita un punto de referencia específico para un rostro, funciona sobre cualquier rostro. Con la información de la imagen 2D, es posible reconstruir elementos del rostro, inclusive de partes que no se ven en la imagen 2D. De esta forma, y después de MUCHO entrenar la CNN, logran los resultado que se pueden ver en la live demo!