【Mind+Python】人脸检测2

2021-08-071.9万
AI 快速预览详细 收起
本文介绍了如何使用dlib库进行人脸检测,通过shape_predictor_5_face_landmarks.dat和shape_predictor_68_face_landmarks.dat模型库,可以方便地检测人脸5点和68个关键点。文章提供了详细的代码示例和步骤说明,适合编程入门和AI启蒙玩具项目。
dlib :一个很经典的用于图像处理的开源库,shape_predictor_68_face_landmarks.dat是一个用于人脸68个关键点检测的dat模型库,使用这个模型库可以很方便地进行人脸检测。


检测人脸5点
shape_predictor_5_face_landmarks.dat,首先检测人脸5点。

【Mind+Python】人脸检测2图1【Mind+Python】人脸检测2图5
  1. import dlib
  2. import os
  3. import cv2
  4. predictor_path  = "shape_predictor_5_face_landmarks.dat"
  5. cap = cv2.VideoCapture(0) # 打开摄像头
  6. detector = dlib.get_frontal_face_detector()
  7. predicator = dlib.shape_predictor(predictor_path)
  8. #win = dlib.image_window()
  9. while cap.isOpened():
  10.   ok, img1 = cap.read()
  11.   if not ok:
  12.             break
  13.   dets = detector(img1,1)
  14.   print("Number of faces detected : {}".format(len(dets)))
  15.   for k,d in enumerate(dets):
  16.     print("Detection {}  left:{}  Top: {} Right {}  Bottom {}".format(
  17.         k,d.left(),d.top(),d.right(),d.bottom()
  18.     ))
  19.     lanmarks = [[p.x,p.y] for p in predicator(img1,d).parts()]
  20.     for idx,point in enumerate(lanmarks):
  21.         point = (point[0],point[1])
  22.         cv2.circle(img1,point,5,(0,0,255),-1)
  23.         font = cv2.FONT_HERSHEY_COMPLEX_SMALL
  24.         cv2.putText(img1,str(idx),point,font,0.6,(0,255,0),1,cv2.LINE_AA)
  25.         #对标记点进行递归;
  26.   cv2.namedWindow("img",cv2.WINDOW_NORMAL)
  27.   cv2.imshow("img",img1)
  28.   c = cv2.waitKey(1)
  29.   if c & 0xFF == ord('q'):
  30.             break
  31. cap.release()#释放摄像头并销毁所有窗口
  32. cv2.destroyAllWindows()
复制代码
人脸68个关键点【Mind+Python】人脸检测2图2

修改:
  1. predictor_path  = "shape_predictor_68_face_landmarks.dat"
复制代码
【文件位置】
shape_predictor_5_face_landmarks.dat,shape_predictor_68_face_landmarks.dat,两文件需放到与py文件同目录下:
【Mind+Python】人脸检测2图3

【附件】
两文件下载:
http://dlib.net/files/
【Mind+Python】人脸检测2图4

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -