Welcome to MicroROS-Pi5 repository
当製品の日本語版マニュアル資料は、こちらからご取得ください。
4. Face recognition tracking
Face recognition tracking
Note: The ROS_DOMAIN_ID of the Raspberry Pi and the microROS control board need to be consistent. You can check [MicroROS Control Board Parameter Configuration] to set the microROS control board ROS_DOMAIN_ID. Check the tutorial [Connect MicroROS Agent] to determine whether the IDs are consistent.
1、Program Description
After running the program, when a person's face is displayed on the screen and a box appears surrounding the person's face, the PTZ camera will move along with the movement of the person's face.
2、Steps
Program code reference path:
/root/yahboomcar_ws/src/yahboomcar_astra/yahboomcar_astra/face_fllow.py
2.1、Start command
After entering the docker container, according to the actual car model, enter the terminal.
xxxxxxxxxxros2 run yahboomcar_astra face_fllow
After the program is started, the following camera screen will appear.

When the face is recognized, the frame will be selected, and the 2D pan/tilt will follow the movement of the face, and the terminal will print the movement angle.


2.2、Dynamic parameter adjustment
Enter the following commands in the Docker terminal.
xxxxxxxxxxros2 run rqt_reconfigure rqt_reconfigure

After modifying the parameters, click on a blank space in the GUI to write the parameter values. As can be seen from the above figure,
faceTracker mainly adjusts the three parameters of PID to make the gimbal more sensitive
3、core code
3.1、face_fllow .py
The principle of function implementation is similar to that of object tracking. It calculates the rotation angle of the servo based on the center coordinates of the target, and then publishes it to the chassis. Part of the code is as follows.
x
# Calculate center point cv.putText(frame, text, (20, 30), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 1) face_patterns = cv2.CascadeClassifier('/root/yahboomcar_ws/src/yahboomcar_astra/yahboomcar_astra/haarcascade_frontalface_default.xml') faces = face_patterns.detectMultiScale(frame , scaleFactor=1.1, minNeighbors=5, minSize=(100, 100)) if len(faces)>0: for (x, y, w, h) in faces: m=x n=y cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) self.execute(m,n)#Calculate the steering gear rotation based on the center point [x_Pid, y_Pid] = self.PID_controller.update([point_x - 320, point_y - 240]) if self.img_flip == True: self.PWMServo_X += x_Pid self.PWMServo_Y += y_Pid else: self.PWMServo_X -= x_Pid self.PWMServo_Y += y_Pid
if self.PWMServo_X >= 90: self.PWMServo_X = 90 elif self.PWMServo_X <= -90: self.PWMServo_X = -90 if self.PWMServo_Y >= 20: self.PWMServo_Y = 20 elif self.PWMServo_Y <= -90: self.PWMServo_Y = -90