Day 6:Introduction to Face Detection

Lesson Description

Learn the fundamentals of object detection and its principles.discover the diverse applications of object detection in various fields.develop an object Detector through a hands-on activity to apply your knowledge.

Introduction

Face detection is the action of locating human faces in an image and optionally returning different kinds of face-related data.

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, positions, orientations, skin colour, glasses facial hair or not, etc.

 

Knowledge-Based Detection

The knowledge-based method depends on a set of rules and is based on human knowledge to detect the faces. For example, a face often appears with two eyes that are symmetric to each other, a nose, and a mouth.

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.
  2. Detailed rules fail to detect faces, like the rule that, ‘A person must have two eyes’: According to this rule, no face will be identified if the image has 1 eye.
  3. 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.
  4. 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. Face landmarks are a set of easy-to-find points on a face, such as the pupils or the tip of the nose.

By default, there are 68 predefined landmark points.

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 the noise in the images.
  2. Difficult to detect features in complex backgrounds.

Face Detection in PictoBlox

PictoBlox uses feature-based detection to detect faces. Face landmarks are a set of easy-to-find points on a face, such as the pupils or the tip of the nose. By default, there are 68 predefined landmark points. The following image shows all landmark points:

Face Landmarks

Users can detect facial expressions using machine learning and algorithms to see emotions and recognize 6 basic facial or universal expressions in human faces. It does this by analyzing faces in images or video through cameras.

Face Recognition in PictoBlox

Face recognition systems allow programs to identify a person by analyzing the face landmarks of her/his face in a digital image.

PictoBlox allows you to run face recognition using the following process:

  1. When a face is analyzed, its geometrical properties, size, and position are obtained.
  2. A structure is generated that forms a pattern that is later stored in a database.
  3. Once stored, the user’s facial record is compared with the biometric features of each face that is scanned and allows adding new templates to make the recognition more effective.
  4. If the image matches a search, the system will alert us that a match has been found.

Face Detection

Face Detection Blocks in PictoBlox

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

Let’s first add the extension to 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. Next, click on the Add Extension button and add the Face Detection extension.
  3. The face detection models will be downloaded, which may take a few seconds to load.
    Downloading Models
  4. Once downloaded, you will get a message saying “Model loaded successfully”.

Facial Detection Block

To execute face detection, we will analyse image from () blocks.

analyse image from ()

You can input the image in the following ways:

  1. Camera feed
  2. Stage

This block analyses the image and saves the face information locally, which can be accessed using other blocks similar to computer vision.

You have to run this block every time you want to analyse a new image from the camera or stage.

Controlling camera feed

You can control the camera feed on the stage using turn () video on stage with () % transparency block.

turn () video on stage with () % transparency

You can change the following parameters of the block:

  1. Camera state:
    1. OFF – The video feed on the stage will stop.
    2. ON – The video feed on the stage will start.
    3. ON flipped – The video feed on the stage will start with the video mirrored. So, your right hand will show left and vice versa.
  2. Transparency: This parameter makes the video translucent. O is defined as the camera feed shown on the stage, and 100 is the video that will fade out entirely from the stage.

Edit the parameter and try out this block yourself.

Example – Analyzing Image on Stage

In this topic, we will analyze the following image:

Boy and Girl

  1. Download the image from here: https://ai.thestempedia.com/wp-content/uploads/2022/02/Boy-and-Girl.jpg
  2. In PictoBlox, turn off the camera using turn () video on stage with () % transparency block. Then, click on the block to execute it.
    Hide camera
  3. Next, upload it as the backdrop.
    Upload Backdrop
  4. Select Tobi sprite and hide it.
  5. You will find the stage like this:
    Face detection stage
  6. Drag analyse image from () block, change the feed as stage and click on the block to analyse the image.
    analyse stage
  7. We will now see what we get when we analyse images in face detection.

Get # faces

Once you have analyzed the images, you can use the get # faces block to get the number of faces recognized in the image.

get # faces

For the image being analyzed, you will get 2.

Get expression of face ()

get expression of face () ssion of face ()

The get expression of face () block reports the recognized expression of the selected face. If the face selected is not detected, then it reports NULL.

Emotion

Example: For the first face in the analyzed image, the expression is Happy.

Is expression of face () ()

is expression of face () ()

The is expression of face () () block checks whether the selected face has a particular emotion or not. If the emotion matches, then the block returns true; else, false.

E.g. for face 1 in the image, the emotion is Happy.

is expression 2

Get () of face ()

get () of face ()

The get () of face () block reports the following attributes of the image:

  1. X position: X coordinate of the center of the image on the stage
  2. Y position: Y coordinate of the center of the image on the stage
  3. Width: Width of the face
  4. Height: Height of the face

Activity: Emotion 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.

Table of Contents