main.py 739 B

123456789101112131415161718192021222324252627282930313233
  1. import dlib
  2. import cv2
  3. import numpy as np
  4. import pandas as pd
  5. print(dlib.DLIB_USE_CUDA)
  6. # 加载 dlib 的人脸检测器
  7. detector = dlib.get_frontal_face_detector()
  8. # 加载输入图像
  9. image = cv2.imread('aa.jpg')
  10. # 将图像转换为灰度图像
  11. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  12. # 使用人脸检测器检测图像中的人脸
  13. faces = detector(gray)
  14. # 遍历检测到的人脸
  15. for face in faces:
  16. x, y, w, h = face.left(), face.top(), face.width(), face.height()
  17. # 在图像中标记人脸位置
  18. cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  19. # 显示标记后的图像
  20. # cv2.imshow('Detected Faces', image)
  21. # cv2.waitKey(0)
  22. # cv2.destroyAllWindows()
  23. cv2.imwrite('output.jpg', image)