Lesson 12: AI Smart Door Access Monitor

0 28 Medium

With advances in technology, more and more places use face recognition to manage entry and exit, effectively preventing unauthorized access. Today, let us also try building an AI access control system with UNIHIKER M10 and experience the power of smart access control.

Goal

 

When the access control system recognizes a family member, the door opens automatically

and closes automatically after 10 seconds. When it recognizes a stranger, the LED ring flashes to warn the person to leave, and the system takes and saves a face photo of the stranger.

Get to Know

 

1. Understand the principles of face recognition.

2. Master how to use the face recognition library on UNIHIKER M10.

 

Materials List

 

Hardware List:

HARDWARE LIST
1 UNIHIKER M10
2 USB Cable
1 USB camera
1 Servo
1 WS2812-12 RGB LED Ring
1 PH2.0-3Pin cable

Software: 

 

Mind+ programming software x1

Download: https://mindplus.cc/en

 

Hands-on Practice

 

AI access control security monitoring mainly uses face recognition to identify family members and strangers. We will use OpenCV face recognition and complete the project through three tasks: data collection, model training, and recognition.

Task 1: Collect Face Images

In this task, we will capture face images through the camera and save them to a specified

folder, creating a face image dataset for later training and recognition.

Task 2: Train a Face Model

Based on the face image dataset built in Task 1, we will use training blocks to let UNIHIKER

M10 process the images and obtain a dedicated face recognition model for access control.

Task 3: Real-Time Face Recognition

In this task, we will use the trained face recognition model to identify faces appearing in the

camera. If it is a family member, unlock the door; otherwise trigger the light alarm and take a

photo for record.

 

Task 1: Collect Face Images

 

1. Hardware Connection

 

This project uses a USB camera to capture images, a servo (P23) to simulate door open/close, and an LED ring (P21) as an alarm light. Connect components to UNIHIKER M10 as shown below. After successful connection, connect UNIHIKER M10 to the computer with a USB cable.

2. Software Preparation

 

Open Mind+ and complete software preparation as shown below.

3. Write a Program

 

The face recognition feature in AI secure access control is similar to unlocking a phone with

your face in daily life: you must first collect face data. Next, let us learn to use face recognition library blocks to control the USB camera and collect face images.

(1) Load the Face Recognition Library

In this lesson we will use the OpenCV face recognition extension library, so first install the

face recognition library by following these steps.

First, ensure the computer and UNIHIKER M10 are connected to the network, then install the

OpenCV face recognition extension library. Click "Code" to switch to code mode and open

"Library Management".

In "PIP mode", enter `pip install opencv_contrib_python`, click Run, and wait for installation to

complete.

Note: If installation fails due to network issues, find the `face_recognition` folder in the

`assets` folder, drag it into `Files in UNIHIKER M10`, then switch to code mode and run

`1-Install_dependency.py` in that folder to complete installation. If the OpenCV face

recognition extension was installed before, reinstalling may show warnings and errors, but it

can still work normally.

Then import the graphical library. After installing the extension library, you can search for the

"Face Recognition" graphical library in "Extensions" -> "User Library" (search link: https://gitee.com/chenqi1233/ext-face_recognition).

(2) Enroll Faces

After adding the face recognition library, we can use blocks and the USB camera to enroll

faces. First, use the block `initialize camera until success` with camera ID 0.

Note: The ID above is the camera device ID, default 0. It is not the face ID and does not

need modification.

During face collection, photos are captured only when a person appears in the camera view.

So we need to `import face detection model`, then use the block `wait until button A is pressed, save 50 face images to folder path, face ID 0` to complete face detection and enrollment.

Note that in the block `wait until button A is pressed, save 0 face images to folder path, face

ID 0`, the more face images you enroll, the higher the recognition accuracy, but the longer model generation takes. The face ID is used to label each enrolled person; different people should use different IDs.

Now we can use code to control the camera and complete learning for the first face. The

complete reference code is shown below:

4. Run the Program

 

Click Run, point the camera at the face, and press button A to start enrollment. After the

camera detects a face, the UNIHIKER M10 screen shows "shooting" and the ID number; the

terminal records the number of saved images. The program stops when enrollment ends.

Note: If an error occurs during execution, check camera connection. To display newly

saved files, click Refresh on the right side of "File Directory". To view these images, refer to

the official guide (https://wiki.unihiker.com/samba). During enrollment, keep the camera as

still as possible and maintain a proper distance between face and camera to ensure faster

face detection.

5. Have a try

Please modify the face ID and enroll a few more faces. (Each run can enroll only one face, and remember to set continuous IDs.)

 

Task 2: Train a Face Model

 

1. Write a Program

 

Through Task 1, we have enrolled enough images of different faces. Now we will use these

images to train a dedicated face model, so UNIHIKER M10 can "remember" the enrolled faces.

In the face recognition graphical library, training a model from face images requires only two

steps: import face images to train the model, and save the model.

Note: When using the block `save trained model file`, to save `model.yml` to the correct path,

modify the path to `/root/face_recognition/model.yml` (remove `/model`). Put these blocks under `when Python main program starts`. You can also add a "training completed" text prompt at the end. Reference code is shown below:

2. Run the Program

 

Click Run to start training the face model. When the terminal shows training complete, the

face model has been successfully trained and saved.

Note: If `open Can't open file...` appears during execution, check whether the model

storage folder exists.

 

Task 3: Real-Time Face Recognition

 

1. Write a Program

 

Now that we have trained the face recognition model, we can start recognizing faces,

displaying recognition results, and controlling corresponding actuators.

(1)Recognize Faces

Let us analyze the process: first do preparation, namely `initialize camera` and import the face model. Then `read camera frame`; when `a face is detected`, start face recognition, distinguish family and strangers, and finally `display recognition result`.

Next, in the face recognition library, follow the flow and find the corresponding blocks for the

blue parts in the diagram to complete the code. Also remember to modify the model path to the one saved in Task 2.

Now your program can recognize faces. To distinguish and display family members and strangers, you need to judge the obtained "confidence". "Confidence" represents the likelihood that the face in the frame matches a learned face. The larger the value, the more likely it is a family member. We can use `predict loaded image, return confidence > value` (for example, 60) to determine whether it is a family member.

The result can be shown by text prompt using the block `display text on camera frame with RGB color at XY coordinates`. You can also use it to set text color and position.

(2)Display Family Member Names

In the Try It of Task 1, we enrolled multiple faces. To display corresponding names, we can use a list and use face IDs as list indexes. At program start, create a variable `name_list` and initialize it as a list of names. The list content should match the enrollment order of faces.

Note: Names in the list must be English strings.

The family member name is the value at the list index, where the index corresponds to the

face ID. The block to get it is `predict loaded image, return recognition result index`.

When a family member is recognized, use the servo to simulate door opening and turn off

the alarm light. To implement this, first load the servo and LED ring libraries, initialize servo and LED ring, then use functions to implement door opening and light-off operations separately.

When a stranger is recognized, turn the LED ring red and take a photo of the stranger. Note

that photo capture should use the basic OpenCV library learned in the previous lesson. Since the default camera object in the face recognition library is `cap`, remember to replace `vd` with `cap`.

Now place the corresponding functions in the branches for family member and stranger

judgments, and the lesson program is complete. To avoid misrecognition of a single frame, we can set a variable `recognition_count` to count current recognized frames. Perform continuous recognition on 3 frames; only when all 3 frames have high confidence do we treat it as successful recognition, achieving stable family-member recognition. Complete reference code is shown below:

2. Run the Program

 

Run the program and use the camera to recognize faces. When a family member is recognized, the screen shows the person's name and "welcome back", and the door opens. When a stranger is recognized, the alarm light turns on, the screen shows "please leave", and a photo is taken.

 

Knowledge Base

 

1. Face Recognition Technology

 

Face recognition is a biometric technology based on facial feature information for identity

verification. Simply put, it is the process of analyzing a face image and identifying the

corresponding person. Its workflow is very similar to how we "recognize a friend walking toward us" in daily life.

"Recognizing a friend walking toward us" can be divided into two parts: I already knew this

friend, and I recognized her now. The former happened in the past, and the latter happens now. In face recognition technology, these correspond to training and recognition respectively.

In summary, training helps the machine learn who a person is first. A machine cannot

memorize face images as easily as the human brain. It needs to learn from a certain number of images and process them specially to form a model for recognition. In this project, for example, we use a camera to collect face images and then train a face model. This is the preparation for face recognition and the foundation of recognition tasks.

Recognition means letting the computer apply the face model to match detected faces against learned faces, find the most similar one, and then output the final recognition result.

2. UNIHIKER M10 Pin Summary

 

So far, we have used many devices with UNIHIKER M10, including analog and digital ones.

Today let us summarize them.

 

Digital signals

Analog signals

Common input

Signal pins

P0~P16, P21~P24

P0~P4, P10, P21, P22

Common output

Signal pins

P0~P16, P21~P25

P0, P2, P3, P8~P10, P16, P21~P23

If you are interested in these pin layouts, visit the UNIHIKER M10 official site

(https://wiki.unihiker.com/jianjie#target_5) for details.

3. Block Overview

 

 

Challenge Yourself

 

After today's lesson, you should already have a good understanding of face recognition. Try

designing a face-based attendance check-in system that stores names and check-in times of

people who have checked in into a list. When no one is in the frame, press button A and print

check-in records in the terminal.

Hint: For time acquisition, you can use `get system time (hour/minute/second)` in the Python

category.

icon lesson12.zip 5.42MB Download(0)

Click to download “face_recognition”.

License
All Rights
Reserved
licensBg
0