AI Vision with PictoBlox, lesson 4 of 15

Face detection: a crown that follows your face

beginner7 min read

Introduction

Phone cameras draw a little box around every face before they take a photo. Video apps add funny hats and glasses that stay on your head when you move. How do they know where your face is?

They use face detection, and today you will too. You will make a crown sprite that sits on top of your head and follows you wherever you move.

The face detector scans the picture like Where's Wally?, looking for two eyes, a nose and a mouth in the right places. It gives back a box: x, y, width and height. The crown sprite just goes to the top of the box.

Working principle

  • Face detection finds faces and tells you their position (x and y) and size (width and height) on the stage.
  • Analyse image from (camera) takes the latest camera picture and runs the AI model on it. You need to run it again every time you want fresh answers, so it goes inside a forever loop.
  • Get # faces tells you how many faces it found.
  • Get (x position) of face (1) gives the position of the first face. Face 2 is the second face, and so on.
  • The position is the middle of the face. To put a crown on top, move it up by half the face's height.

What you'll need

  • PictoBlox with the Face Detection extension
  • A webcam
  • The Crown sprite from the sprite library (or draw your own hat)

Working in real life

  1. Start a new project and add the Face Detection extension.
  2. Choose the Crown sprite. Make it smaller if you need to (set its size to about 60%).
  3. Build the script below on the crown and click the green flag.
  4. Move your head around. The crown should follow you. Move out of the camera and it disappears.

Code

when green flag clicked
turn [on] video on stage with (0) % transparency
forever
  analyse image from [camera]
  if <(get # faces) > (0)> then
    show
    go to x: (get [x position] of face (1)) y: ((get [y position] of face (1)) + ((get [height] of face (1)) / (2)))
  else
    hide
  end
end

Try it

  • Make a second sprite, Tobi, that says how many faces it can see: say (join [I can see ] (get # faces)).
  • Make the crown grow or shrink with your face: set its size using the face's width. Come closer to the camera and watch it grow.
  • Ask a family member to join you, and give face 2 a different hat.

Check what you learnt

  1. What does face detection tell you?

  2. Why is 'analyse image from camera' inside a forever loop?

  3. The face position is the middle of the face. How do you put the crown on top?

Conclusion

  • Face detection finds every face in a picture and reports its position and size.
  • You must analyse the image again in a loop to keep up with movement.
  • With a little maths you can stick things exactly where you want on the face.

What next?

Your face can show feelings too. Next: Emotion mirror: is that a happy face?