# Deep Learning and Facial Recognition

Use the Machine Learning extension to achieve facial recognition function. When "woman" is recognized, the message "laugh" will be broadcast, and HaloCode will make a smiling face with its LED ring; otherwise, the message "angry" will be broadcast, and the LED ring of HaloCode will light up red. You can apply facial recognition to your smart-home system.

![](/files/-LpgA7Y19_Rdq6XA_UqJ)

**Training model**

1\. Under "Sprites", click "+" in the Blocks area to add "Teachable Machine" extension.

![](/files/-LpgA7Y2NYSFLaGnb-2h)

2\. Select Teachable Machine blocks, and click "Training model" to build a new model. In this example, we'll need 3 categories.

![](/files/-LpgA7Y3Nbv02vQwoYkR)

3\. Name the first category "woman". Print a photo of a woman, and place the photo in front of the camera. Click and hold the button "Learn", so that enough samples will be collected. (Change the angle of the photo to collect different samples; more samples come with better recognition result.)

![](/files/-LpgA7Y4pcxQYwGE55Z2)

4\. Likewise, we can collect samples for category "man" and "student".

![](/files/-LpgA7Y5WrGkOYbAp9dX)

5\. When the sample collecting process is done, click "Use the model".

![](/files/-LpgA7Y6OnEzophpBB6b)

**Add event and control**

6\. Drag an Events block when green flag clicked and a Control block if () then (). Then add a Teachable Machine (TM) block recognition result is (woman).

![](/files/-LpgA7Y7jr-OA7ZewGAx)

**Create broadcast messages**

7\. Click Events blocks, and create two messages, "laugh" and "angry".

![](/files/-LpgA7Y8ml9A9g8Zou8M)

8\. Add an Events block broadcast (laugh) and wait. (HaloCode will execute the script that is activated by the message "laugh", and then others.)

![](/files/-LpgA7Y9lsOxK9KyIqYb)

9\. Likewise, for "man" and "student", add corresponding Events block broadcast () and wait. Add a Control block forever to keep facial recognition function on.

![](/files/-LpgA7YAKJDOIOKbwCNA)

**Program HaloCode**

10\. Under "Devices", choose "HaloCode". Add an Events block when I receive() and two Lighting blocks light up LED () with color R() G() B() to light up the second and tenth LED, as the "eyes" of the smiling face.

![](/files/-LpgA7YBykb0hEUTvU6r)

11\. Add 5 more Lighting blocks light up LED () with color R() G() B() to light up the fourth to eighth LED as the "mouth". Adjust the RGB values to set the color to light pink.

![](/files/-LpgA7YCu0YBd-_A5sEH)

12\. Add a Control block wait (0.1) seconds and a Lighting block light off all LEDs.

![](/files/-LpgA7YDF1t99Vrfyqv-)

13\. Add an Events block when I receive (angry) and a Lighting block all LEDs light up (). Set the color to red. 0.1 second later, light off all LEDs.

![](/files/-LpgA7YEu0MQcSAb3BnM)

**Programming result**

![](/files/-LpgA7YFSHsj0Ui3n1Pl)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.pakronics.com.au/halocode-1/code-with-mu-a-simple-python-editor/examples/level-up/deep-learning-and-facial-recognition.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
