From 2019 to 2020, COVID-19 has swept the world and brought great inconvenience to our lives. In the face of the epidemic, we must strengthen the awareness of public safety, and remember to always wear a mask when leaving home! But for careless people who often forget to wear a mask, what are the solutions? How about making a device that reminds people to wear a mask when going out?
This project uses the object classification function of HuskyLens to recognize whether a person wear a mask in the shooting range, and remind people who have forgotten to wear a mask through lights and servo.
If it is needed to judge whether there is a mask in the image, you need to classify the image according to the presence of the mask: people with mask and people without mask. Our project is to use the object classification function of the visual sensor--HuskyLens to realize the mask recognition function.
Object classification, that is, training the system to recognize various objects, such as cats, dogs, cars, etc.
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) The Principle of KNN Algorithm
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) The KNN Algorithm in HuskyLens
In the object classification algorithm of HuskyLens, the K value is 1. Therefore, when HuskyLens predicts new data, it compares the data with all the training data, and uses the classification of the training data with the highest similarity as the output result.
This function can learn multiple photos of different objects, and then use the built-in machine learning algorithm for training. After the training, when the learned objects appear again in the HuskyLens camera screen, HuskyLens can recognize them and display their ID numbers. The more you learn about the same object, the more accurate the recognition.
The default setting is to learn multiple. Here we take the recognition of whether a worker wears a helmet as an example.
(1) Dial the function button to the left or right until the word "Object Classification" is displayed at the top of the screen.
(2) Long press the function button to enter the parameter setting of the object classification function.
(3) Dial the function button until "Learn Multiple" is displayed, then short press the function button, and dial to the right to turn on the "Learn Multiple" switch, that is, progress bar turns blue and the square icon on the progress bar moves to the right. Then short press the function button to confirm this parameter.
(4) Dial the function button to the left until "Save & Return" shows. And the screen prompts "Do you want to save the parameters?" Select "Yes" in default, now short-press the function button to save the parameters and return automatically.
Learning and Recognition
You can use the following pictures to test.
STEP1 Learning objects:
Point the orange frame in the center of the HuskyLens screen at the object to be learned (the worker with a helmet on the left in the picture above), long press the "learning button" to learn from different angles and distances, after completing the learning of the first object, release the "learning button", the screen will prompt: "Press again to continue! Press other buttons to finish". If you want to learn the next object, you can continue by pressing the "learn 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 not operate and wait for the end of the countdown.
In this lesson, you need to continue to learn the next object (the worker without a helmet on the right in the above picture), so press the "learning button" before the countdown ends, and then point the orange frame in the center of the HuskyLens screen at the object, long press the "learning button" to complete the learning of the second object, and so on.
The ID displayed is consistent with the order of learning, that is, ID will be displayed as “ID1”, “ID2”, “ID3”, and so on, and different colors correspond to different colors of frames.
STEP2 Object Recognition:
When HuskyLens encounters the learned object again, its ID number will be displayed on the screen. As shown in the figure below, when HuskyLens recognizes that the worker is wearing a helmet, it displays ID1, while the worker is not wearing a helmet, it displays ID2.
Q1: Can the object classification algorithm obtain the relative position of the object on the HuskyLens screen?
A1: No. Under the object classification algorithm, the position of the output frame is fixed, and its x and y center coordinates on the screen remain unchanged, so HuskyLens cannot obtain the relative position of the object on the screen. But you can learn different positions of the object as different IDs, and judge the position by IDs. For example, in an unmanned vehicle, take the left, middle, and right sides of the road as ID1, 2, and 3 respectively. By judging the IDs, you can know the position of the car relative to the road.
Q2: How to improve the accuracy of recognition under the object classification algorithm?
A2: Long press the "learning button", you can record the target picture from multiple angles and distances to improve the recognition accuracy.
We will complete the task in two tasks. First, we will learn to use the object classification function of HuskyLens to recognize the presence of masks, based on this, add light strips and servos on this basis to achieve the reminder effect according to their status.
Task 1: Show whether the one wears a mask
Use the HuskyLens sensor to learn the three types of IDs: background, face with mask, and faces without mask. Write a program to rename the IDs so that whether a person is wearing a mask can be recognized.
Task 2: Make a mask reminder
Based on task 1, build a structural model, add light strips and servo to make a mask reminder.
HuskyLens is used as a visual sensor and micro:bit is used as a control board. They communicate via I2C. The connection diagram is as follows:
Select the HuskyLens object classification function, long press the "learning button" to learn multiple background images, and get ID1.
Then learn the face without a mask and get ID2.
Finally, learn the face with the mask and get ID3. You can also spread the mask flat for learning.
Open Mind+ (version 1.62 or above), switch to "Offline", click "Extension", click “micro:bit” under “Board”, click “HuskyLens AI Camera” under “Sensor”.
Here are the instructions mainly used.
① Initialize only once between the beginning of the main program and looping executions. You can select I2C or Soft-serial, and no need to change I2C address. Please note that the “Output protocol”of your HuskyLens sensor should be set to be consistent with the program, otherwise, data cannot be read.
② You can switch to other algorithms freely, but please note that you can run only one algorithm at each time, and it takes some time to switch algorithms.
③ The main controller requests HuskyLens to store data in the “Result” once(stored in the memory variable of the main board, and once requested, it will refresh the data in the memory once), then the data can be obtained from the “Result”. The latest data can be got from the “Result”only when this module is called.
④ Check whether there is frame or arrow in the screen from the requested “Result”, including the learned(id>0) and unlearned, and returns 1 if there is one or more.
⑤ Check whether the IDx has been learned from the requested “Result”.
⑥ Check if the IDx requested from the “Result”is in the screen. The frame refers to the algorithm of the frame on screen, arrow refers to the algorithm of the arrow on screen. Select arrow when the current is only line-tracking algorithm, for others, choose frame.
Based on task 1, connect the servo to the P8 port of the expansion board, and the light strip to the P12 port of the expansion board.
After connecting the circuit, make a simple model and fix the hardware in the proper position of the model.
Since the recognition of the presence or absence of masks has been realized in task 1, we only need to set the state of the servo and the light strip based on task 1.
In this project, we learned the object classification function of the HuskyLens sensor and made a family-use mask reminder with the servo, LED strips, and paperboard to remind people to take protective measures when going out in this special period of the epidemic.
1. Understand the working principle of object classification;
2. Learn how to use HuskyLens object classification
3. Use HuskyLens as an input device in combination with micro:bit board and other hardware to complete the project
1. In this project, we only made a simple model to simulate the function and effect of the project, and hardware such as electric appliance can also be used to make the project apply in real life.
2. Masks on the market are not the same in material, properties, and colors. Gas masks are more professional protective equipment than normal masks. We can learn more IDs to improve the project.