☺️Lesson 6: Face recognition
Last updated
Last updated
The aim of this lesson is to teach Misty how to recognize faces and remember her friends. It's a wonderful and interesting skill that most users want to play with and use for more complex interactions. You can find more details about her computer vision capabilities in Vision or ask our humans.
Before exploring face recognition in Blockly it's important to understand some of the basics of working with face recognition. If you've already explore the Vision subsection in the Explore tab of Misty Studio, you most likely saw that there is a feature called Face Training.
Face training is a process in which your Misty builds up library of images with your face and maps them to create a unique FaceID associated with the name that you have written. You can start face training by clicking the start button, Misty will help you to guide you through it by displaying an orange label with a message. For instance, if it's too dark you are asked to move into the light. Misty's chest LED will turn green when your face is memorized. You can find a list of all the FaceIDs by clicking on the middle button 'Get Learned Faces', and you can also delete all of them by clicking on the last button ''Clear Faces'.
Once you've created your FaceID, Misty will be able recognize your face anywhere, this function is called face recognition. You can test if Misty recognizes you by clicking the 'Start Face Recognition' button under Face Recognition and you will see that your name will appear in the 'Label' field. The other fields will tell where you are in relation to Misty's camera.
A face recognition works just as any other event, so it means that you will need to put the run until stopped block at the end of your program, however the main difference between a face recognition event and a bump sensor event is that you will need to have a start face recognition block to trigger it. The next step is to add your face recognition event block from the 'Vision' tab in Misty blocks and choose the name you want to be recognized in the first text field and Misty will only recognize the face associated with that name. If you leave it empty as show in the code above, she will choose any face in her system, as if "any" was selected in the bump or touch sensor event blocks.
If you check the second check box Misty will repeat the face recognition event every time she detects a face. Misty checks her surroundings several times per second so in this case she would say "Hi, I'm Misty" every time she detects a face. You can found out more in Events when it's useful to check the second check box.
Now that you understand how face recognition events work you can start building a multiple face recognition event skill to determine how your Misty will react when she sees specific individuals. Let's try creating two events for two individuals.
Can you see how this code can be improved? Imagine that you don't have to recognize only two faces but ten. This program would occupy a lot of space! In this case theEvent Messageblock comes to the rescue yet again. To compact the program into one face recognition event block, you will need to start with a condition block found in the 'Logic tab in Basic Blocks and a text block. By using the condition block you can let Misty know that when she sees a specific person, she has to trigger a sequence associated with that person. To do that you will need to set your condition to '='.
To compact the program into one face recognition event block and create multiple outcomes, you will also need to use the if-do and a else-if-do blocks found in the 'Logic' tab in Basic Blocks and a text block. To create and else if condition you can click on the gear icon in the if-do block. These blocks will tell Misty that she has to perform a sequence only if she sees a specific person.
Now that you have all the condition blocks in place, try building your face recognition skill using the example below.
We can do even more to improve the program. When learning programming you can find this amazing concept: DRY (Don't Repeat Yourself). The idea with this concept is that we need to repeat our code as few times as possible and make it as general as possible.
In the example below you can see that we're combining our create text with block from the previous lesson and the condition blocks to generalize the program. By separating the greeting 'Hi' from the names you can have hundreds of people memorized and each of them will be greeted with a 'Hi' followed by their name. If you want to give a special greeting to your friends, then you can create event messages for their names and add your own unique sequence for each person. You can easily expand this to everyone. Try it out using the example below!