Welcome to ROSMASTER M3 repository
06.Face Detection
Face Detection
1. Course Content
- Run example programs, use the mediapipe framework to detect faces.
2. Program Startup
For Raspberry Pi 5 users, if the ROS container is not started, you need to start the container first,
xxxxxxxxxxbringup_m3Open a terminal inside the container,
xxxxxxxxxxexec_m3- Run face detection node in terminal
xxxxxxxxxxros2 launch m3_bringup mediapipe_demo.launch.py demo:=FaceDetectorAfter the program runs, as shown in the figure below, detected faces will be boxed and the detection score will be displayed. The higher the score, the more likely it is recognized as a face.

3. Core Code Analysis
Program code path:
xxxxxxxxxx~/yahboomM3_ws/code_ws/src/m3_common_demo/m3_common_demo/mediapipe/05_FaceDetection.pyImport the required library files
xxxxxxxxxximport timeimport cv2 as cvimport numpy as npimport rclpyfrom rclpy.node import Node#import mediapipe libraryimport mediapipe as mpfrom cv_bridge import CvBridgefrom sensor_msgs.msg import Imagefrom arm_msgs.msg import ArmJointsimport cv2Initialize data and define publishers and subscribers,
xxxxxxxxxxdef __init__(self, name): super().__init__(name) self.minDetectionCon=0.5 #Use the class in the mediapipe library to define a face detection object self.mpFaceDetection = mp.solutions.face_detection self.mpDraw = mp.solutions.drawing_utils self.facedetection = self.mpFaceDetection.FaceDetection(min_detection_confidence=self.minDetectionCon) self.rgb_bridge = CvBridge() #Define the subscriber for color image topic self.sub_rgb = self.create_subscription(Image,"/camera/color/image_raw",self.get_RGBImageCallBack,100)Color image callback function,
xxxxxxxxxxdef get_RGBImageCallBack(self,msg): #Use CvBridge to convert color image message data to image data rgb_image = self.rgb_bridge.imgmsg_to_cv2(msg, "bgr8") #Pass the obtained image to the defined findFaces function for face detection program frame,_ = self.findFaces(rgb_image) key = cv2.waitKey(1) cv.imshow('frame', frame)findFaces function,