MaixPy #6: Maixduino Face Detection | AI MicroPython

Face detection can be regarded as a specific case of object-class detection. In object-class detection, the task is to find the locations and sizes of all objects in an image that belong to a given class. Face detection answers two questions:

  1. Are there any human faces in the collected images or video?
  2. Where are the faces located? [2]

In this tutorial, I am going to deploy an AI model at the edge to detect human faces using the YOLO V2 algorithm.

What Will We Need

 

Maixduino Face Detection

Introduction to Yolo V2

You only look once (YOLO) is a state-of-the-art, real-time object detection system. The team that developed the algorith apply a single neural network to the full image. This network divides the image into regions and predicts bounding boxes and probabilities for each region. These bounding boxes are weighted by the predicted probabilities. [3]

In this case, we just have to run the model at the edge because it is pre-trained and has been tested with hundreds of faces before. All of the CNN’s weights have been adjusted, and all we have to do now is point the camera and wait for the model to draw a box around the discovered face(s).

In my research, I found a cool video on Youtube of the YOLO V2 nework in action, just to relax before we go into the good stuff. It’s not perfect, but boy is it fast. Sometimes it says that a bunch of walls are trains, but OK, we forgive it.

I don’t think it’s necessary for me to go into detail about the YOLO V2 CNN´s operation. These experiments are supposed to be practical, a way for us to apply the knowledge to real-life projects. I will start a different series in the future related to ANN (Artifitial Neural Networks) and there it will be explained from scratch.

Aplications

Face detection technology can be applied to fields like:

  • Security : Can be used to auto-focus cameras or to count how many people have entered an area
  • Entertainment : It can do facial motion capture that can then be incorporated in computer animation for movies, games or avatars
  • Personal safety : It can be used to help determine which parts to blur in an image to assure privacy

All of this to to provide surveillance and tracking of people in real time. (Some can say it isnt such a good ideia)

Face Detection vs Face Recognition

Face detection and recognition are usually used together, but they are different concepts that will provide different end applications.

  • Face detection is used to find faces in an image or video without “knowing who that person is”
  • Face recognition involves the gadering of details of the face, making a more detailed assessment of the “human”

Maixduino YOLO V2 Face Detection Code

Import the modules:

import sensor
import image 
import lcd 
import KPU as kpu

In order:

  1. Initialize the lcd
  2. Initialize the camera
  3. Set the camera to default format (RGB565)
  4. The resolution is QVGA that is 320×240
  5. Start to run, it is not necessary to call it in the current version, the camera will automatically start to run after the above settings are completed
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
#sensor.run(1)

There are two ways to “put” the model in the Maixduino:

  1. Download it using the kflash tool to the 0x300000 address
  2. Put it in the SD Card
task = kpu.load(0x300000)
#task = kpu.load("/sd/face.kmodel")

The anchor point parameters are consistent with the model parameters. This parameter of a model is fixed and bound to the model (it is determined when the model is trained) and cannot be changed to other values

anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)

Because the model of YOLO V2 is used, it has a dedicated function interface. We have to use init_yolo2 to initialize the model.

a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)

In the loop, The YOLO v2 model runs a deep learning CNN on an input image to produce network predictions. When the probability is higher than the predefined in the function (kpu.init_yolo2), the network draws a rectangle on the face. It also prints the parameters of the model to the serial terminal: the position of the box on the screen, its dimensions, etc.

while True:
        img = sensor.snapshot()
        code = kpu.run_yolo2(task, img)
        if code:
            for i in code:
                print(i)
                a = img.draw_rectangle(i.rect())
        a = lcd.display(img)
a = kpu.deinit(task)

Maixduino YOLO V2 Face Detection Experiment

Steps

  1. Download the face_model_at_0x300000.kfpkg here (Sipped official website). If the link isn’t available, I will leave it in the materials section.
  2.  Flash the model to the Maixduino board with the kflash_gui tool to the 0x300000 address (details on how to obtain the tool can be found in the MaixPy # 1 Tutorial)
  3. I reduced the Baud Rate. It is a way for the download to be successful.
  4. And finally, if everything goes according to plan, we just have to open and run the Face_Detection.py file that can be found bellow in the materials section or go here for a more complex code. (Github)

Final Code

Just a few considerations:

  • If the model runs without error, it is better to be in a place with sufficient light.
import sensor
import image
import lcd
import KPU as kpu

lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)

task = kpu.load(0x300000)
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
while True:
        img = sensor.snapshot()
        code = kpu.run_yolo2(task, img)
        if code:
            for i in code:
                print(i)
                a = img.draw_rectangle(i.rect())
        a = lcd.display(img)
a = kpu.deinit(task)

 

Video:

Maixduino Experiment Materials

References

[1] https://www.techtarget.com/searchenterpriseai/definition/face-detection

[2] https://en.wikipedia.org/wiki/Face_detection

[3] https://pjreddie.com/darknet/yolov2/

[4] https://www.datasciencecentral.com/face-detection-explained-state-of-the-art-methods-and-best-tools/