import dlib  
import cv2  
import os 
import time
# 加载dlib的人脸检测器  
detector = dlib.get_frontal_face_detector()  
imgdir = 'face_test/'

imglist = os.listdir(imgdir)
i = 0
for img in imglist:
    t1 = time.time()
    imgpath = imgdir+img
    img = cv2.imread(imgpath)  # 加载图像,替换'your_image_path.jpg'为你的图片路径  
    # 加载图像(替换为你的图像路径)  
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # dlib需要RGB图像  
    
    # 检测图像中的人脸  
    dets = detector(img_rgb, 1)  
    t2 = time.time()
    print('time ',t2-t1,' s')
    # 在检测到的人脸周围绘制矩形  
    for k, d in enumerate(dets):  
        # dlib的矩形是(left, top, right, bottom)  
        x1 = d.left()  
        y1 = d.top()  
        x2 = d.right()  
        y2 = d.bottom()  
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 0), 15)  
    cv2.imwrite('face_results/face'+str(i)+'.jpg', img)  
    i+=1