Welcome to DOFBOT-Ultra repository
5. Face Detection
Face Detection
Please ensure that the web control program backend process is not running in the background. To close it, refer to the [Get Started Quickly - 2. Access the Terminal] section in [Disable the Auto-Start Web Control Backend Process]. After closing, please restart DofbotUltra!!!
Use MediaPipe Face Detection to detect faces in the camera feed in real-time, drawing bounding boxes and corner markers around detected faces, and displaying confidence percentages.
1、Startup
1.1、Startup
Enter the following command to start the program
xxxxxxxxxxros2 run dofbot_mediapipe 05_FaceDetection.py

Press the q key in the image window or Ctrl+C in the terminal to exit the program.
1.2、Source Code
Raspberry Pi source path:
xxxxxxxxxx~/dofbot_ultra_ws/src/dofbot_mediapipe/scripts/05_FaceDetection.pyRDK X5 source path:
xxxxxxxxxx~/dofbot_ultra_ws/src/2d/dofbot_mediapipe/scripts/05_FaceDetection.pyx#!/usr/bin/env python3# encoding: utf-8import mediapipe as mpimport cv2 as cvfrom dofbot_utils.camera import create_camerafrom dofbot_utils.fps import FPS
class FaceDetector: def __init__(self, minDetectionCon=0.5): self.mpFaceDetection = mp.solutions.face_detection self.mpDraw = mp.solutions.drawing_utils self.facedetection = self.mpFaceDetection.FaceDetection( min_detection_confidence=minDetectionCon)
def findFaces(self, frame): img_RGB = cv.cvtColor(frame, cv.COLOR_BGR2RGB) results = self.facedetection.process(img_RGB) bboxs = [] if results.detections: for id, detection in enumerate(results.detections): bboxC = detection.location_data.relative_bounding_box ih, iw, ic = frame.shape bbox = (int(bboxC.xmin * iw), int(bboxC.ymin * ih), int(bboxC.width * iw), int(bboxC.height * ih)) bboxs.append([id, bbox, detection.score]) frame = self.fancyDraw(frame, bbox) cv.putText(frame, f'{int(detection.score[0] * 100)}%', (bbox[0], bbox[1] - 20), cv.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 2) return frame, bboxs
def fancyDraw(self, frame, bbox, l=30, t=10): x, y, w, h = bbox x1, y1 = x + w, y + h cv.rectangle(frame, (x, y), (x1, y1), (255, 0, 255), 2) cv.line(frame, (x, y), (x + l, y), (255, 0, 255), t) cv.line(frame, (x, y), (x, y + l), (255, 0, 255), t) cv.line(frame, (x1, y), (x1 - l, y), (255, 0, 255), t) cv.line(frame, (x1, y), (x1, y + l), (255, 0, 255), t) cv.line(frame, (x, y1), (x + l, y1), (255, 0, 255), t) cv.line(frame, (x, y1), (x, y1 - l), (255, 0, 255), t) cv.line(frame, (x1, y1), (x1 - l, y1), (255, 0, 255), t) cv.line(frame, (x1, y1), (x1, y1 - l), (255, 0, 255), t) return frame
def main(): detector = FaceDetector() fps = FPS()
cap = create_camera() if not cap.isOpened(): raise RuntimeError("Failed to open camera")
try: while cap.isOpened(): ret, frame = cap.read() if not ret: continue
frame, _ = detector.findFaces(frame) fps.update_fps() fps.show_fps(frame) cv.imshow('FaceDetection', frame)
if cv.waitKey(1) & 0xFF == ord('q'): break finally: cap.release() cv.destroyAllWindows()
if __name__ == '__main__': main()