# Safety Helmt Reminder | Study Pack of HUSKYLENS for micro:bit 09

### Lesson 9: Safety Helmet Reminder with an AI Machine Vision Sensor

On construction sites, wearing a safety helmet is a crucial safety measure that can protect workers from small falling objects. However, sometimes workers might forget to wear it. To address this issue, we can utilize an AI machine vision sensor HuskyLens, and micro:bit to create a safety helmet reminder device.

The object classification feature of AI machine vision sensor HuskyLens allows this device to identify workers who are not wearing safety helmets and prevent them from entering the construction site. Only workers who are correctly wearing safety helmets are allowed to enter. In this way, we can ensure that all workers entering the construction site are wearing safety helmets, thereby enhancing the safety of the site.

#### Learning Objectives

1. Understand the principle of KNN algorithm for object classification

2. Learn the object classification function of AI machine vision sensor HuskyLens

3. Use the object classification function of AI machine vision sensor HuskyLens to make a safety helmet reminder

#### Preparation

Note: The object classification card is the same as the face card.

### Operating Principle of Object Classification

KNN can be said to be one of the simplest classification algorithms. At the same time, it is also one of the most commonly used classification algorithms.

#### 1. What is KNN Algorithm

We can see KNN as a kind of rote-learning classifier. It will remember all the training data and will directly match the new data with them. If there are data with the same attributes, the classification of the training data will be used as the classification of the new.

#### 2. KNN Algorithm Principle

The full name of KNN is K Nearest Neighbors. There is no doubt that the value of K is definitely crucial. So what’s the nearest neighbor? Let's take a look at the picture.

The green point in the figure below is the data to be predicted. Assuming K=3, that is to say the KNN algorithm will find the three points closest to it (here we circle it) to see which kind is more. For example, as there are more blue triangles in the picture, and the new green points will be classified into it.

However, when K=5, the judgment will be different. In this case, there are more red circles, so the new green dots are classified as red circles. From this example, we can see that the value of K is very important.

The KNN algorithm will first train multiple data and extracts their characteristics. When predicting a new value, it judges which category x belongs to according to the category of the nearest K points. In the figure on the right, the yellow circle and the purple circle are the training data, and the red five-pointed star is the value to be predicted. When K=3 and K=6, what the predicted results respectively will be?

#### 3. KNN Arithmetic of AI machine vision sensor HuskyLens

In the object classification algorithm of AI machine vision sensorHuskyLens, the K value is 1. Therefore, when AI machine vision sensorHuskyLens predicts new data, it will compare the data with all the training data, and select the classification of the one with the highest similarity as its output result.

### Demonstration of AI Machine Vision Sensor HUSKYLENS Object classification function

If you want to judge whether a worker is wearing a helmet, you need to classify the image according to the characteristics of the helmet. One type is those wearing the helmet, the other is those not wearing. This project is to use the object classification function of AI machine vision sensor HuskyLens to perform safety helmet recognition function.

#### 1. Learn Objects

It can be tested by the following cards.

Firstly, align the big orange frame in the center of the AI machine vision sensor HuskyLens screen with the object to be learned (the worker with a helmet), press and hold the "learning button" to learn from different angles and distances. Then release the button after completing learning the first object. At this point, the screen will prompt: "Press the button again to continue! Press other buttons to end". If you want to continue to learn the next object, you can continue to learn the next object by pressing the "learning button" before the countdown ends. If you no longer need to learn other objects, just press the "function button" before the end of the countdown, or do nothing and wait for its end.

#### 2. Recognize Objects

In the course, we need to continue to learn the next object (the worker without helmet), so press the "learning button" before the countdown ends, and then align the orange frame in the center of the screen with the object to be learned, press and hold "Learning button" to complete learning the second object. So on and so forth.

The object ID is consistent with its input order. That is, the learned objects will be marked as "object: ID1", "object: ID2", "object: ID3", and so on in order. Moreover, different objects have different border colors.

When HuskyLens detect the learned object again, its ID number will be displayed on the screen. As shown in the figure below, when AI machine vision sensor HuskyLens recognizes that the worker is wearing a helmet, the screen displays ID1. If he does not wear it, it will display ID2.

#### 3. FAQ

Q: Can the object classification algorithm get the relative position of the object on the HuskyLens screen?

A: No, it can’t.

Under the object classification algorithm, the position of the output box is fixed, and its x and y center coordinates on the screen remain unchanged, so AI machine vision sensor HuskyLens cannot get the relative position of the object on the screen. But it can learn different positions of the object as different IDs, and judge the position by them. For example, in an unmanned vehicle, learn ID 1, 2, 3 as the left, middle, and right sides of the road respectively. By judging the ID, the position of the unmanned vehicle relative to the road can be known.

Q: How to improve the accuracy of recognition under the object classification algorithm?

A: Press and hold the "learning button", you can record the target picture from multiple angles and distances to improve the accuracy.

### AI Vision Project Practice

This project will be divided into two tasks. First, we will learn to use the object classification function of AI machine vision sensor HuskyLens to identify whether there is a helmet or not. Second, add LED strip lights and servos on this basis, and achieve the reminder effect according to their status.

Task 1: Show whether the one wears a safety helmet

Use the AI machine vision sensor HuskyLens sensor to learn three types of ID – the background, wearing a safety helmet, and not wearing a safety helmet. Write a program to rename the ID. In this way, we can identify whether the worker is wearing a safety helmet.

On the basis of task 1, add LED strip lights and servos to judge whether workers entering the construction site with safety helmets, and perform corresponding operations and reminders.

#### AI Vision Program Design

Function Description:

The object classification function can learn different photos of one object, and then use the built-in machine learning algorithm for training. After the training is completed, the algorithm will be named again. When the learned objects reappear on HuskyLens camera screen, HuskyLens can recognize them and display the algorithm name and its ID number.

Note: The more photos of a object AI machine vision sensor HuskyLens learns, the more accurate the recognition will be. The default setting is to learn more than one photo.

Learn and Recognize

Select the AI machine vision sensor HuskyLens object classification function, press and hold the "learning button" to learn multiple background pictures to get ID1.

Then learn the character wearing a helmet, long press the "learning button" to learn multiple characters with helmets to get ID2.

Finally, learn the character without a helmet to get ID3.

Flowchart Analysis:

#### The MakeCode Sample Program

Makecode Sample Program link : https://makecode.microbit.org/_eRKF0VEkYfRq

#### AI Vision Function Explain:

As the recognition of whether there is a helmet has been realized in task 1, we only need to add and set the state of the servos and the LED strip lights on its basis.

Flowchart Analysis

The MakeCode Sample Program

In the program, we need to use the function of the NeoPixel blocks. There are a special RGB light library. The method to add RGB LED strip library is as follows:

Makecode Sample Program link : https://makecode.microbit.org/_DqXPob8bJ2x9

### Project Review

In this project, we learned how to use the object classification function of AI machine vision sensor HuskyLens to made a safety helmet reminder with the servo and led strip. It not only allows workers to pay attention to wearing helmet, but also prevents workers who are not wearing helmets from entering the construction site.

### Project Expansion

In this project, we mainly used the object classification function to classify people wearing safety helmets into one category, and those who do not wear into the other category. In fact, there is a wide range of applications of object classification. Can you come up with other interesting projects by using the object classification function of HuskyLens?

Of course, you can continue to improve the helmet reminder and use the resources at hand to build a real model. The picture below is to use discarded corrugated paper and fix the hardware on it to make a safety helmet detection door on the construction site. Come and have a try!