Table of Contents

addclassfromcamera()

Function Definition: addclassfromcamera(label_number = 1, label_name = "Jarvis")

Parameters

NameTypeDescriptionExpected ValuesDefault Value
label_numberintLabel number for which the information is stored.1-101
label_namestringLabel name of the face.String"Jarvis"

Description

The function allows the user to add a particular face into the database from the camera feed. The user can specify the name of the face with the argument as well. This addition of the face in the database is also stored inside the PictoBlox file while saving.

Face Recognition Process

Face recognition has the ability to recognize a face and then match it with the data of the faces stored in a database. Face recognition is an extremely useful application of artificial intelligence that can identify an individual by analyzing their facial patterns and textures.

 

Example

The example demonstrates how face recognition works with analysis on the camera.

The example demonstrates the application of face recognition with camera feed. Following are the key steps happening:

  1. Initializing the program with parameters for the sprite and face detection library.
  2. Saving the face showing in the camera as class 1.
  3. Running face recognition and reporting whether class 1 is detected or not.

Code

sprite = Sprite('Tobi')

fd = FaceDetection()
import time

fd.setthreshold(0.5)
fd.video("on", 0)
fd.enablebox()
time.sleep(2)

fd.deleteallclass()

# Adding face 1 to database
fd.addclassfromstage(1, "Face 1")

while True:
  fd.recognisefromcamera()
  
  if fd.isclassdetected(1):
    sprite.say("Face 1 Recognised")
  else:
    sprite.say("Face 1 Missing")

Output

Read More
Learn about AI-based face expression detection, a technology that uses artificial intelligence algorithms and computer vision techniques to analyze images or videos of human faces and recognize emotions or expressions.

Introduction

AI-based face expression detection refers to the use of artificial intelligence algorithms and computer vision techniques to analyze images or videos of human faces and recognize the emotions or expressions being displayed. The technology can detect and analyze subtle changes in facial features, such as eye movement, mouth shape, and eyebrow position, to determine whether a person is happy, sad, angry, surprised, or expressing other emotions.

Discover the various fields that utilize this technology, including psychology, marketing, and human-computer interaction. Additionally, read about the logic and code behind face detection with a camera feed, including the initialization of parameters, face detection library, loop execution, and if-else conditions. Explore how the technology continuously analyzes emotions, and how the humanoid responds with different facial expressions and movements.

Code

sprite = Sprite('Tobi')

fd = FaceDetection()

quarky = Quarky()

import time

humanoid = Humanoid(7, 2, 6, 3, 8, 1)

# Turn the video ON with 0% transparency
fd.video("ON", 0)
fd.enablebox()

# Run this script forever
while 1:
  fd.analysecamera()          # Analyse image from camera 
  sprite.say(fd.expression()) # Say the face expressions
  
  if fd.isexpression(1, "happy"): # if face expression is happy
    quarky.showemotion("happy")   # show happy emotion on Quarky
    humanoid.action("dance2", 1000, 1)
    
  if fd.isexpression(1, 'sad'):
    quarky.showemotion("crying")
    humanoid.action("updown", 1000, 1)
    
  if fd.isexpression(1, 'surprise'):
    quarky.showemotion('surprise')
    humanoid.action("moonwalker", 1000, 1)
    
  if fd.isexpression(1, 'angry'):
    quarky.showemotion('angry')    
    humanoid.action("flapping2", 1000, 1)
  else:
    humanoid.home()
    
# Comment the above script, uncomment the below script and 
# run this script to clear the stage and quarky display

fd.disablebox()
fd.video("off")    
quarky.cleardisplay()

Logic

The example demonstrates how to use face detection with a camera feed. Following are the key steps happening:

  1. The code is using face detection to recognize facial expressions and control a humanoid and a display device called Quarky accordingly.
  2. Then, the program turns on the video with 0% transparency and enables the bounding box for face detection.
  3. The code then enters an infinite loop where it continuously analyzes the image from the camera using face detection and says the detected facial expressions.
  4. The code then checks if the expression is happy, sad, surprised, or angry using the if statement. If the expression is happy, the Quarky device displays a happy emotion, and the humanoid performs the “dance2” action for specific time. Similarly, for sad, surprised, and angry expressions, Quarky displays the respective emotion, and the humanoid performs the associated action.
  5. If no facial expression is detected, the humanoid is set to its “home” position. Finally, if the program needs to be stopped.

Output

Read More
This project uses face recognition and an IR sensor to identify and authenticate authorized people and open the door accordingly. Learn how to create this system with Python code and hardware.

The project uses face recognition to identify authorized people and opens the door accordingly.

Circuit

We are using 2 devices in this project:

  1. IR Sensor: The IR sensor provides information if there is an obstacle in front or not. The IR sensor connections are as follows:
    1. GND Pin connected to GND of the Quarky Expansion Board.
    2. VCC Pin connected to VCC of the Quarky Expansion Board.
    3. Signal Pin connected to D3 of the Quarky Expansion Board.
  2. Servo Motor: The servo motor controls the Door of the IoT house which is connected to the servo port of 5 of the Quarky Expansion Board.

Alert: Make sure you have the Door Servo Motor calibrated.

Face Recognition

We will be using Face Detection extension for making the face recognition application.

Storing the Face Authorised for IoT House

This code is used to add a new face to a system:

  1. The first step is to create a Sprite object with the nameTobi‘. Then, a Face Detection object is created. The time library is imported.
  2. Then, a function called addFace() is defined. This function allows us to add a new face to the system. First, the video feed from the camera is turned on. Then, the camera is analyzed for a face. If one face has been detected, the user is asked to select a slot (1 to 10) and enter a name for the new face which is then added to the system. Finally, the video feed from the camera is turned off.
  3. The code runs a loop, which checks if thea key has been pressed. If it is, the addFace() function is called.
#Create a new Sprite object with the name 'Tobi'
sprite = Sprite('Tobi')

#Create a new Face Detection object
fd = FaceDetection()

#Import the time library
import time

#Set the threshold for face detection to 0.5
fd.setthreshold(0.5)
#Turn off the video feed from the camera
fd.video("off", 0)
#Enable the box to be drawn around the detected face
fd.enablebox()

#Define a function that adds a new face to the system
def addFace():
  #Create a flag to keep track if a new face has been added
  faceFlag = 0

  #Turn on the video feed from the camera
  fd.video("on", 0)
  time.sleep(1)

  #Keep looping until a new face has been added
  while faceFlag == 0:
    #Analyse the camera for a face
    fd.analysecamera()

    #Check if one face has been detected
    if fd.count() == 1:
      #Ask the user which slot the face should be added to
      sprite.input("Select the slot (1 to 10)?")
      #Store the slot number the user provided
      faceSlot = sprite.answer()

      #Ask the user to enter a name for the new face
      sprite.input("Enter the name of the face")
      #Store the name the user provided
      faceName = sprite.answer()

      #Add the face to the system with the provided slot number and name
      fd.addclassfromcamera(faceSlot, faceName)

      #Set the faceFlag to 1 to stop the loop
      faceFlag = 1

  #Turn off the video feed from the camera
  fd.video("off", 0)

#Keep running the loop forever
while True:
  #Check if the 'a' key has been pressed
  if sprite.iskeypressed("a"):
    #If yes, call the addFace() function
    addFace()

Working of an IR Sensor

An Infrared sensor is a type of sensor that senses if something is close to it or not. The IR stands for Infrared sensor. Infrared is the light out of our visible spectrum.

An IR sensor has a white LED (transmitter) and a photodiode (receiver). The transmitter emits IR light, and the receiver detects reflected light from objects within the sensor’s range, which can be adjusted with a potentiometer. The sensor is indicated by two LED indicators, a power LED which is always on, and a signal LED which is on when an object is detected and off when nothing is detected.

The signal LED has two states or situations:

  1. ON (Active) when it detects an object
  2. OFF (Inactive) when it doesn’t detect any object

Python Code for IR & Face Recognition-Based Smart Door Opening System

This code creates a program that can add a new face to the system, and then recognize and authenticate the user:

  1. It libraries of Sprite, FaceDetection, Quarky, Expansion, and IoTHouse to perform these tasks. It also imports the time library for timing purposes.
  2. The program sets the threshold for face detection to 0.5, turns off the video feed from the camera, and enables the box to be drawn around the detected face.
  3. It also moves a servo on the expansion board to position 5 and moves it to 100 degrees to close the door.
  4. It defines two functions called addFace() and authenticate().
  5. The authenticate() function turns on the video feed from the camera, recognizes the face in the camera, and speaks out the name of the recognized user if the face has been recognized. It then returns 1 to indicate the user has been authenticated.
  6. The program then keeps running the loop forever. It checks if thea key has been pressed and if yes, calls the addFace() function.
  7. It also checks if the IR sensor is active and if yes, calls the authenticate() function. If the user has been authenticated, it moves the servo to 0 degrees to open the door and then back to 100 degrees to close the door after some time.
#Create a new Sprite object with the name 'Tobi'
sprite = Sprite('Tobi')

#Create a new Face Detection object
fd = FaceDetection()

#Import the time library
import time

#Create a new Quarky object
quarky = Quarky()

#Create a new Expansion object
expansion = Expansion()
house = IoTHouse()

#Set the threshold for face detection to 0.5
fd.setthreshold(0.5)
#Turn off the video feed from the camera
fd.video("off", 0)
#Enable the box to be drawn around the detected face
fd.enablebox()

#Move a servo on the expansion board to position 5 and move it to 100 degrees
expansion.moveservo(5, 100);

#Define a function that adds a new face to the system
def addFace():
  #Create a flag to keep track if a new face has been added
  faceFlag = 0

  #Turn on the video feed from the camera
  fd.video("on", 0)
  time.sleep(1)

  #Keep looping until a new face has been added
  while faceFlag == 0:
    #Analyse the camera for a face
    fd.analysecamera()

    #Check if one face has been detected
    if fd.count() == 1:
      #Ask the user which slot the face should be added to
      sprite.input("Select the slot (1 to 10)?")
      #Store the slot number the user provided
      faceSlot = sprite.answer()

      #Ask the user to enter a name for the new face
      sprite.input("Enter the name of the face")
      #Store the name the user provided
      faceName = sprite.answer()

      #Add the face to the system with the provided slot number and name
      fd.addclassfromcamera(faceSlot, faceName)

      #Set the faceFlag to 1 to stop the loop
      faceFlag = 1

  #Turn off the video feed from the camera
  fd.video("off", 0)


#Define a function that authenticates the user
def authenticate():
  #Turn on the video feed from the camera
  fd.video("on", 0)
  time.sleep(1)

  #Recognise the face in the camera
  fd.recognisefromstage()

  #Check if one or more face has been detected
  if fd.count() > 0:
    #Loop through all the detected faces
    for i in range(1, fd.count() + 1):
      #Check if the face has been recognised
      if fd.getclassname(i) != "unknown":
        #Speak out the name of the recognised user
        sprite.say("Authorised - " + fd.getclassname(i), 2)

        #Turn off the video feed from the camera
        fd.video("off", 0)

        #Return 1 to indicate the user has been authenticated
        return 1
  
  #Turn off the video feed from the camera
  fd.video("off", 0)
  #Return 0 to indicate the user has not been authenticated
  return 0

#Keep running the loop forever
while True:
  #Check if the 'a' key has been pressed
  if sprite.iskeypressed("a"):
    #If yes, call the addFace() function
    addFace()

  #Check if the space key has been pressed
  if house.irstatus("D3"):
    #If yes, call the authenticate() function
    if authenticate() == 1:
      #Move the servo to 0 degrees
      expansion.moveservo(5, 0)
      time.sleep(2)
      #Move the servo back to 100 degrees
      expansion.moveservo(5, 100)

 

Output

Read More
All articles loaded
No more articles to load