Welcome to Rider Pi Robot repository
17.Facial Recognition
Face Recognition
Face Recognition1. Experiment Objective2. Experiment Steps3. Experimental Phenomena4. Experimental Source Code Analysis
This tutorial is specific to CM5 version; it cannot be used directly in CM4 version.
1. Experiment Objective
To learn how RiderPI can recognize faces after collecting facial image data.
2. Experiment Steps
- Enter the RiderPI system, terminate the RiderPI main program, and enter "ip (where ip is RiderPI's IP):8888" in your browser. After logging in, enter the password "yahboom".

- Then log in and navigate to the path cd /home/pi/Rider-pi_class/5.AI Visual Recognition Course/17. Face Recognition/ and run face_recognition_Class.ipynb.
- Running the Program
3. Experimental Phenomena
- The screen will display a real-time image. At this time, press the button in the upper right corner of the screen to capture and record the face.
- Each time a face is captured, the screen will display "ok," but this is not sufficient evidence for subsequent recognition.

- Press the button in the upper left corner of the screen to perform face recognition. The program will print two results: 1. Successful capture, indicating face recognition can be performed. 2. Capture failed, indicating face recognition cannot be performed and needs to be re-captured.
- After recognizing a face, the screen will display the sequence number of the captured image.
Acquired:

Unacquired:

- This program will not save previously registered faces upon restarting; you need to re-register for recognition.
4. Experimental Source Code Analysis
def take_photo(): global i show_ok = False # 控制是否显示OK字样 Control whether the word 'OK' is displayed ok_display_time = 0 # 记录显示OK的时间 Record the time when OK is displayed while True: frame = picam2.capture_array() #cv2.imshow('frame', frame) image_widget.value = bgr8_to_jpeg(frame) b, g, r = cv2.split(frame) img = cv2.merge((r, g, b)) imgok = Image.fromarray(img) mydisplay.ShowImage(imgok) if button.press_d(): cv2.imwrite(f"./Face_P/{i}.jpg", frame) while (1): frame = picam2.capture_array() face_locations = face_recognition.face_locations(frame) face_encodings = face_recognition.face_encodings(frame, face_locations) # 在这个视频帧中循环遍历每个人脸 Loop through each face in this video frame for (top, right, bottom, left), face_encoding in zip( face_locations, face_encodings): # 看看面部是否与已知人脸相匹配。 Check if the face matches a known face. for i, v in enumerate(total_face_encoding): match = face_recognition.compare_faces( [v], face_encoding, tolerance=0.5) name = "Unknown" if match[0]: total_image_name[i] = total_image_name[i].replace(folder_path,"") name = total_image_name[i] #str(i+1) name = 'NO.' + name break # 画出一个框,框住脸 Draw a frame to enclose the face cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 画出一个带名字的标签,放在框下 Draw a label with a name and place it under the box cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX try: cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.7, (255, 255, 255), 1) except : name = "Unknown" cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.7, (255, 255, 255), 1) # 显示结果图像 Display result image image_widget.value = bgr8_to_jpeg(frame)take_photo(): The main business of this function is to collect faces
while (1): This loop performs recognition based on the collected faces. The recognized faces will display their serial numbers on the screen, while those not recognized will display "Unknown".