import dlib
import cv2
 
# 步骤1: 加载预训练的人脸检测模型
face_detector = dlib.get_frontal_face_detector()
 
# 步骤2: 读取图片
image_path = r"E:\person1.jpg"
image = cv2.imread(image_path)
 
 
# 步骤3: 检测图片中的人脸
dets = face_detector(image, 1)
 
# 步骤5: 在检测到的人脸周围画矩形框
for i, d in enumerate(dets):
    x1, y1, w, h = d.left(), d.top(), d.width(), d.height()
    cv2.rectangle(image, (x1, y1), (x1 + w, y1 + h), (0, 255, 0), 2)
 
# 步骤6: 显示图片
cv2.imshow("Detected Faces", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

[python][dlib]使用dlib进行人脸检测_画矩形