Face Detection

Before finding out how face recognition works, let’s find out how good are you at recognizing faces. Can you identify the age and gender of the people in this image?

portraits-children-boys-family

Yes, you can! You can identify that there are 3 males and 1 female in the photo. You can also perhaps guess their age (we believe you can. STEMpedians are smart B)). 

But can a machine recognize if there are any faces in the image and guess their gender, age, and emotion?

Face Detecttion Family

In this lesson, we will see how face detection and face recognition works. Let’s dive in! 

Topic Covered in the Lesson

  1. Face detection in image
  2. Face detection blocks in PictoBlox
  3. Facial recognition

Key Learning Outcomes

At the end of  the lesson, you’ll be able to:

  1. Understand how face detection works and what the method of face detection is.
  2. Understand how facial recognition works and what are its applications.
  3. Use the AI blocks in PictoBlox to identify the following features from the faces in images: Emotion, Face Landmarks, Face position in the image, Face recognition
  4. Make AI projects in PictoBlox to detect faces from the camera.
    Face Expression

Let’s begin! 

Face Detection

In the past few years, face recognition has become one of the most promising applications of computer vision. Face detection can be considered to be a substantial part of face recognition operations.

The method of face detection in pictures is complicated because, well, human faces are widely different from one another! They can have different poses, expressions, position and orientation, skin color, have glasses or facial hair or not, etc. Then are also differences in camera gain, lighting conditions, and image resolution.

face.detection 

How Do We Detect Faces?

There are a few methods that you can use to detect face such as

  1. Knowledge-Based
  2. Feature-Based

Knowledge-Based Detection

The knowledge-based method depends on a set of rules and is based on human knowledge to detect the faces.

E.g. A face often appears with two eyes that are symmetric to each other, a nose and a mouth.

Face detection

 

Pros
  1. Easy to come up with simple rules.
  2. Based on the coded rules, facial features in an input image are extracted first, and face candidates are identified.
Cons
  1. Difficult to translate human knowledge into rules precisely.
    1. Detailed rules fail to detect faces like there are two eyes in a person: According to this, no face will be identified in this image.
      Emma Watson
    2. General rules may find many false positives: Like there is an eye in a face. According to this, the face will be identified in this tattoo of a similar-looking eye.
  2. This approach alone is insufficient and unable to find many faces in multiple images.

Feature-Based Detection

Feature-based detection uses what are known as face landmarks to detect faces.

By default, there are 68 predefined landmark points. The following image shows all landmark points:

Face Landmarks

The feature-based method detects faces by extracting the face landmarks of the face.

Pros
  1. Features are invariant to pose and orientation change.
  2. This method has a higher chance of face detection. 
Cons
  1. Difficult to locate facial features due to noise in the images.
  2. Difficult to detect features in complex backgrounds. 

Download PictoBlox

STEP 1: Download the Pictoblox Installer (.exe) for Windows 7 and above (Release Notes).

STEP 2: Run the .exe file.

Some of the device gives the following popup. You don’t have to worry, this software is harmless. Click on More info and then click on Run anyway.

STEP 3: The rest of the installation is straightforward, you can follow the popup and check on the option appropriate for your need.

Your software is now installed!

STEP 1: Download the Pictoblox Installer (.dmg).

STEP 2: Run the .dmg file.

STEP 1: Download the Pictoblox Installer (.deb) for Ubuntu (Linux) (Release Notes).

STEP 2: Run the .deb file by clicking it or with the following command:

$: sudo dpkg -i PATH_TO_PICTOBLOX

STEP 3: Add User to the ‘dialout’ group (if not able to connect to the Serial port) with the following command:

$: sudo adduser ${USER} dialout

Your software is now installed!

Select the link according to your Android Version:

Install from App Store

Face Detection Blocks in PictoBlox

The Face Detection extension in PictoBlox has blocks dedicated for face detection.

Let’s first add the extension in our project:

evive Alert
Alert: Make sure you have an active internet connection to load the model.
  1. Create a new project in PictoBlox.
  2. Select evive as your board from the Board tab in the menu bar.
  3. Next, click on the Add Extension button and add Face Detection extension.
    Face Detection Extension
  4. The face detection models will be downloaded which may take few seconds to load.
    Downloading Models
  5. Once downloaded you will get a message saying “Model loaded successfully”. 

Face Expression Detector

In this project, we will make a script that detects the face using the camera and reports the expression of all the faces detected on the stage.

Face Expression

Let’s begin! 

Setting Up the Stage

  1. Add a new Square Box sprite and delete the Tobi Sprite.
    New Sprite
  2. Drag and drop a when flag clicked block into the scripting area.
  3. Snap a hide block below the when flag clicked block.
  4. Snap a turn () video on stage with () transparency block from the Face Detection palette below the hide block. Change the camera option to on and transparency to 0%.

The stage is set. Click the green flag to get the camera feed on the stage.

 

Face Detection Script 1

Recognize Image

  1. We will continue with the same script.
  2. Add a forever block from Control palette.
  3. Snap a analyse image from () block. Select the feed as the camera.

Face Detection Script 6

Looping Every Face

  1. Make a new variable called Face.
    Variable
  2. Snap a set () to () block below the analyse image from () block. Select the variable as Face and set the value as 0.
  3. Add a repeat until () block below the set () to () block. Add an () = () block from the Operators palette in the condition of the repeat until () block. In the first input, add a recognized face count block and in the second add the Face reporter block from the Variables palette.
  4. Snap a change () by () block in the repeat until () block loop. Select the variable as Face and set the value as 1.
  5. Add a create clone of () block below the change () by () block. This will create a clone for every face. The script till now is as follows:

Face Detection Script 5 

Display Bounding Box

Now we will write the script for each face detected.

  1. Drag when I start as a clone block in the scripting area. 
  2. Snap a set size to () % block from the looks palette.
  3. Next add get () of face () block from the face detection palette. Select the parameter as width and add Face reporter block in the face number input.
    Script
  4. Then, add a set x to () block. Add a get () of face () block in the input. Change the parameter to the x position and add the Face reporter block in the face number input.
  5. Next, add a set y to () block. Add a get () of face () block in the input. Change the parameter to the y position and add the Face reporter block in the face number input.
  6. Next, add a say () block. Add a get expression of face () block in the input. Add the Face reporter block in the face number input.
  7. Snap a show block. Your script for the clone is ready:

Clone Script

Deleting the clones

You have to delete the clones or the square box every time you draw new squares. Otherwise your stage will get filled with unwanted clones. Here is how you can do that:

  1. Add a broadcast () and wait block from the event palette in the main script below analyse image from () block. Edit the message as “Delete All Clones“.
    Face Detection Script
  2. Next add when I receive () block into the scripting area from event palette and select Delete All Clones message.
  3. Add delete this clone block from the control palette below the when I receive () block. This will delete all clone.
    Face Detection Script 2

Your script is now complete. Click the green flag to start the script.

Face Expression 

Next Session

Session 3

Human Body Detection

Download PictoBlox – Coding and AI Project Making App

Incase of any difficulty with PictoBlox, please feel free to write us at support@thestempedia.com

Copyright 2021 – Delhi Government All rights reserved | Managed by Valeur Fabtex Private Limited | Technology Partner – STEMpedia