From face recognition access in smart campuses and visitor verification in intelligent buildings to dynamic security early warning in public areas, face recognition technology is the core support for multi-scenario intelligent management due to accurate identity recognition. In the previous class, we laid the technical foundation for camera-based object recognition. In this class, we'll focus on face recognition and apply it to smart access control .
Project Objectives
Knowledge Objectives
1.Understand face recognition workflow;
2.Learn ML process;
3.Use UNIHIKER K10 for face recognition.
Practical Objective
Use the UNIHIKER K10's built-in face detection to make an access control system. First, press Button A to register the user's face for system recognition. Then, press Button B. If the system recognizes the user, the door will open; if a non-user is detected, the buzzer will alarm.
Materials List
Preparations
Hardware
(1) Insert the servo into servo port S1 with correct polarity: align the yellow signal wire with the green pin on the expansion board.

(2) Stand the UNIHIKER K10 upright, and insert it into the expansion board slot with the screen facing forward.

(3) Connect the UNIHIKER K10 to the computer with a USB cable, and connect the expansion board to the computer with its power cable separately (the UNIHIKER K10 only needs to connect to the computer when uploading programs). Then switch the power to ON.

(4)Some voice prompts are required for this lesson. Please prepare them and save them to the TF card in advance.

Software
Open Mind+, switch to“Upload Mode”, connect the UNIHIKER K10 as shown in the figure below, and load the UNIHIKER K10 library.

Hands-on Practice
Next, we'll use the UNIHIKER K10's detection functions to complete the access control system production through two tasks: user face registration and access control face recognition.
Task 1: User Face Registration
First, we'll use the camera's face detection function. Then, by pressing Button A, we can register the user's face.
Task 2: Access Control Face Recognition
Then, use Button B to complete face recognition for the smart access control, and combine it with the servo to produce the access control system.
Task 1: User Face Registration
Code
User face registration is divided into two steps: judging whether a face is detected by the camera, and letting the system learn the face.These require the use of“switch mode (face detection)”and“learn face”blocks in”AI” of UNIHIKER K10, as shown below:

The specific programming ideas are as follows:

The corresponding program is as follows:

Note: The prompt sound (Prompt Tone 1.wav) must be stored in the TF card in advance.
After successful learning, UNIHIKER K10 assigns an ID to the face. After the voice prompt, the scanned face ID is output via the serial port to confirm registration completion. This requires“recognize face”and“recognized face id”blocks from UNIHIKER K10 AI , as shown below:

Since face learning and recognition take a long time, it's not recommended to put the "verify face registration" program in Button A callback function or the main program's loop execution part to avoid lag. Instead, use Button B to realize the verification function first.
The complete reading code is shown below:

Run the Code
Click the“Upload”buttonand wait for upload. Point the camera at the user's face. When the recognition box appears, press A to complete face learning; press B to output the scanned face ID number via the serial port.

Note: The“recognized face id”block retrieves the recognized ID number once after successful recognition and performs a reset (sets value to -1) immediately. When using Button B for entry verification, verify at least twice to prevent the reset value (-1) from the serial port from interfering with detection results.
Code Review
Category | Blocks | Function |
![]() | Used to forget all learned face IDs. | |
![]() | Used to forget a specified face ID. | |
![]() | Learning faces, used to collect and learn recognised faces, after successful learning it will automatically assign an ID number to the face, ID number starts from 1, up to 48 faces can be learned. | |
![]() | Recognise faces that have been learned. | |
![]() | Get the recognised face ID, used to read the face ID number after successful recognition. | |
![]() | Used to check whether face recognition has been completed. |
Task 2: Access Control Face Recognition
Code
STEP1: Face Recognition for Access Control
After the user’s face has been enrolled, press Button B to start recognition. When recognition is successful, the program can check whether the detected face ID matches a learned face.
As shown in Task 1, if face recognition is successful, the corresponding face ID will be output as 1, 2, 3, and so on. Otherwise, the output will be -1.
Based on the code from Task 1, modify the program as follows:

The corresponding program is as follows:

STEP 2: Add the Servo Module
Using a servo needs an expansion board. Click Extensions, find and click the “motor:bit”under“Module”to add it. Then turn back and find“motor:bit”in the Command Area to complete loading.

Use the code to rotate the servo to a specific angle before installing the servo arm for later setting of opening/closing angles. The specific way is as follows:

Based on the angle reference and servo installation method in the access control system, 90°means the door is closed, while 0°/180°means it is open, as shown below.

To protect the servo, we set the closing angle to 90° and the opening angle to 5°.
The complete reading code is shown in the figure below:

Run the Code
Click the“Upload”buttonand wait for completion. Since the program now forgets all learned faces at the beginning, re-enroll the faces first. Then, ask users with learned faces and users with unlearned faces to try the access control system. Press Button B and observe the results.

Have a try
How can we distinguish between different faces? You only need to use a variable to store the face ID, and then check the value of that ID. For example, see the code below.
Now, try enrolling multiple faces, such as the owner’s face as ID 1 and family members’ faces as other IDs.

Knowledge Base
Next, let's learn and summarize the hardware knowledge used in this lesson.
Concept and General Process of Face Recognition
1.Concept of Face Recognition
Face recognition is a biometric technology that identifies individuals by facial feature information. It uses a camcorder or camera to capture face-containing images or video streams, then automatically detects, tracks, and recognizes faces in frames. Also known as portrait or facial recognition, it analyzes a face image to answer“Whose face is this?”
2.General Process of Face Recognition
Face recognition consists of the following four steps:
(1) Face Detection: The process of locating the face region in an image.
(2) Face Preprocessing: Adjusting the face image to make it clearer.
(3) Face Collection and Learning: Collecting the detected and preprocessed face images, then learning and memorizing them.
(4) Face Recognition: Searching among the learned faces to find which one is most similar to the face to be identified.
The first three steps prepare for Step 4's face recognition. When designing a face - recognition program, first collect preprocessed faces for a dataset. Then, learn from the dataset and train it into a model. Finally, during recognition, collect real - time image data and input it into the model to get a result.

Face recognition technology is widely used in security surveillance, identity verification, attendance, mobile payment, financial services, transportation, public services and other fields.
Concept and General Process of Machine Learning
1.Concept of Machine Learning
Machine learning is a technology that allows computers to "learn" patterns autonomously from data analysis without fixed rules programmed manually. Specifically, by providing a computer with large amounts of relevant data, it automatically identifies relationships in data, uses the learned patterns to judge or predict new situations. This is computer's "autonomous learning ability".
2.General Process of Machine Learning
Machine learning consists of the following four core steps:
(1) Data Preparation: The process of collecting raw data relevant to the task.
(2) Data Preprocessing: Organize raw data, remove useless info, and fill in missing data to make the dataset clean and structured for computer analysis.
(3) Model Training: Letting the computer automatically discover patterns in the processed data.
(4) Model Application: Using the trained “pattern model” to process new, unknown data and make judgments or predictions.
The first three steps set the base for the final model application. When using the UNIHIKER K10 for machine learning, data preprocessing and model training are automatically done inside it. We only need to prepare data before model application to achieve the desired functions.

Challenge Task
To ensure equipment safety and maintain experimental order, a middle school's physics laboratory needs to build an intelligent access-control system with identity verification, automatic attendance, and anomaly-warning functions. Only registered teachers (IDs: 1-10) and students (IDs: 11-48) can enter, and attendance is automatically recorded.















