Welcome to MicroROS V2 Robot Car repository
5. Facial Recognition
Face Recognition
1. Experiment Objective
Use MediaPipe Face Detection to detect the positions of faces in the camera image, marking each face region with a rectangular bounding box and displaying the confidence.
2. Experiment Principle
1. Key Terms
| Keyword | Description |
|---|---|
| MediaPipe Face Detection | Google's face detection model, which outputs a face bounding box + 6 keypoints (eyes, nose, mouth, ears) but not the 468-point mesh |
| Bounding box | The rectangular box enclosing the face region, in the format (x, y, width, height) |
| Confidence | The model's certainty about a detection result, overlaid on the frame as a percentage |
2. Technical Architecture
Terminal 1: start agent + camera + joint modelsTerminal 2: face recognition
3. Experiment Steps
1. Hardware Preparation
| Hardware | Requirement |
|---|---|
| PTZ version | ✅ Supported |
| No-PTZ version | ✅ Supported |
| Required peripherals | ESP32 camera |
2. Start the agent + camera + joint models (Terminal 1)
xxxxxxxxxxros2 launch microros_v2_bringup car_base.launch.py
3. Start face recognition (Terminal 2)
xxxxxxxxxxros2 launch microros_v2_mediapipe_pkg face_detection.launch.py4. Observed Results
- Faces are enclosed in pink rectangular bounding boxes, with the confidence percentage shown above the box
- 6 keypoints (eyes, nose, mouth, ears) are annotated at their corresponding positions

5. Code Walkthrough
Source code paths:
- Launch:
~/microros_ws/src/microros_v2_mediapipe_pkg/launch/face_detection.launch.py- Node:
~/microros_ws/src/microros_v2_mediapipe_pkg/microros_v2_mediapipe_pkg/FaceDetection.py
1. Core Node Analysis
x# Source file: ~/microros_ws/src/microros_v2_mediapipe_pkg/microros_v2_mediapipe_pkg/FaceDetection.pyclass FaceDetection(Node): def __init__(self): super().__init__("face_detection_node") self.mp_face_detection = mp.solutions.face_detection self.face_detection = self.mp_face_detection.FaceDetection( model_selection=0, min_detection_confidence=0.5) def image_callback(self, msg): frame = self.bridge.imgmsg_to_cv2(msg, 'bgr8') results = self.face_detection.process(cv.cvtColor(frame, cv.COLOR_BGR2RGB)) if results.detections: for detection in results.detections: bbox = detection.location_data.relative_bounding_box h, w, _ = frame.shape x, y = int(bbox.xmin * w), int(bbox.ymin * h) bw, bh = int(bbox.width * w), int(bbox.height * h) cv.rectangle(frame, (x, y), (x+bw, y+bh), (255, 0, 255), 2) cv.putText(frame, f'{detection.score[0]:.0%}', (x, y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 2) cv.imshow(self.window_name, frame)Key logic:
model_selection=0: short-range model (within 2 meters), suitable for desktop/indoor scenes;=1is the long-range modelrelative_bounding_box: normalized coordinates (0~1) that must be multiplied by the actual image width/height to convert to pixel coordinates- The bounding box + confidence percentage are drawn directly on the original frame; the 6 keypoints are obtained via
detection.location_data.relative_keypoints
2. Key Parameters
Parameter definition files:
- Launch:
~/microros_ws/src/microros_v2_mediapipe_pkg/launch/face_detection.launch.py- Node:
~/microros_ws/src/microros_v2_mediapipe_pkg/microros_v2_mediapipe_pkg/FaceDetection.py
| Parameter | Type | Default | Description |
|---|---|---|---|
model_selection | int | 0 | 0=short-range model, 1=long-range model |
min_detection_confidence | float | 0.5 | Detection confidence threshold |
3. Node Description
| Node | Function |
|---|---|
face_detection_node | MediaPipe Face Detection face bounding box + confidence annotation |
6. Frequently Asked Questions
| Issue | Cause | Solution |
|---|---|---|
| Face not detected at long range | model_selection=0 only works within 2 meters | Set model_selection:=1 to switch to the long-range model |