8.8、face recognition

8.8.1、Experimental goal

This lesson mainly learns the face recognition function, which has more recognition effects than the face detection function.

The reference code path for this experiment is:K210_Broad\05-AI\face_recog.py

 

8.8.2、Preparation before experiment

Please import the model file to the memory card first, and then insert the memory card into the memory card slot of the K210 module. Please refer to the specific operation steps:

Appendix: Import model files to memory card

 

8.8.3、The process of the experiment

The factory firmware of the module has integrated the AI vision algorithm module. If you have downloaded other firmware, please burn it back to the factory firmware and then conduct experiments.

  1. Import related libraries, and initialize camera and LCD display.

  1. The size of the new face feature image is 64*64.

  1. Initialize the parameters related to the face detection model, the model file path is: /sd/KPU/yolo_face_detect/face_detect_320x240.kmodel, and use yolo2 to calculate whether it meets the model requirements.

  1. Initialize the relevant parameters of the ld5 model. The address of the model is: /sd/KPU/face_recognition/ld5.kmodel.

  1. Initialize the relevant parameters of the feature model. The address of the model is: /sd/KPU/face_recognition/feature_extraction.kmodel.

  1. New key function, rising edge trigger, the main function is to record the face to be recognized.

  1. Create a new face recognition variable, among which, the variable record_ftrs: represents the array of recognized face features; the variable THRESHOLD: represents the threshold of face recognition, and if it exceeds this value, it is considered a recognized face; the variable recog_flag: Indicates whether the detected face has been recognized, which is True if it is recognized, and False if it is not.

  1. Extract information about detected faces.

  1. Create a new while loop. The main function is to detect the face first, and record the face information when the BOOT button is recognized. When the detected face information is greater than the recognition threshold, it means that it is a recognized face, with a green box, and the recognition score is displayed, otherwise it means an unrecognized face, with a white box.

 

8.8.4、Experimental effect

Since the BOOT button is needed, do not run it directly in CanMV IDE. CanMV IDE cannot detect the BOOT button at present. Please download the code as main.py to the K210 development board and run it.

Point the camera at the face, if it is an unrecognized face, a white box will be displayed.

image-20230220145105079

At this time, press the BOOT button in the upper right corner to record the current face information, the white frame will change to a green frame, and the recognized score will be displayed.

image-20230220145126870

The result of recognizing that the boot button has not been pressed to register into the face database is as follows:

image-20230220145126871

 

8.8.5、Experiment summary

Face recognition needs to use the memory card to load the model file, so you need to import the model file into the memory card in advance, and then insert the memory card into the memory card slot of the K210 development board. If the model file in the memory card cannot be read, then Will report an error.

Since the face recognition needs to use the BOOT button, do not run the face recognition code in CanMV IDE, please download the code as main.py to the K210 chip, and then press the reset button to start running.

Recognized faces are sorted in order, the first recognized person is person:0, the second recognized person is person:1, and so on.