当前位置:   article > 正文

人脸检测5种方法

人脸检测

众所周知,人脸识别是计算机视觉应用的一个重大领域,在学习人脸识别之前,我们先来简单学习下人脸检测的几种用法。

常见的人脸检测方法大致有5种,Haar、Hog、CNN、SSD、MTCNN:

注:本文章图片来源于网络

相关构造检测器的文件:opencv/data at master · opencv/opencv · GitHub

基本步骤

  1. 读入图片
  2. 构造检测器
  3. 获取检测结果
  4. 解析检测结果

一、Haar

  1. # 调整参数
  2. img = cv2.imread('./images/001.jpg')
  3. cv_show('img',img)
  4. # 构造harr检测器
  5. face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')
  6. # 转为灰度图
  7. img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  8. plt.imshow(img_gray,'gray')
  9. # 检测结果 上图4个人脸所以4个方框坐标
  10. # image
  11. # scaleFactor控制人脸尺寸 默认1.1
  12. detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.3)
  13. # 解析
  14. for x,y,w,h in detections:
  15. cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0))
  16. plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

  1. # 调整参数
  2. img = cv2.imread('./images/004.jpeg')
  3. cv_show('img',img)
  4. # 构造harr检测器
  5. face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')
  6. # 转为灰度图
  7. img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  8. plt.imshow(img_gray,'gray')
  9. # 检测结果 上图4个人脸所以4个方框坐标
  10. # image
  11. # scaleFactor控制人脸尺寸 默认1.1
  12. # minNeighbors 确定一个人脸框至少要有n个候选值 越高 质量越好
  13. # [, flags[,
  14. # minSize maxSize 人脸框的最大最小尺寸 如minSize=(40,40)
  15. detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.2, minNeighbors=10)# 在质量和数量上平衡
  16. # 解析
  17. for x,y,w,h in detections:
  18. cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0))
  19. plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

 

上述过程中:

  • scaleFactor参数:用来控制人脸框的大小,可以用它来排除一些错误检测; 
  • minNeighbors参数:我们给人脸框起来的时候,一般一张脸会框许多的框,假如这张脸框得越多,说明质量越好,越是一张正确的“脸”。

二、Hog

对于第一次使用这个功能的同学,要提前下载一下dlib。

  1. import dlib
  2. # 构造HOG人脸检测器 不需要参数
  3. hog_face_detetor = dlib.get_frontal_face_detector()
  4. # 检测人脸获取数据
  5. # img
  6. # scale类似haar的scalFactor
  7. detections = hog_face_detetor(img,1)
  8. # 解析获取的数据
  9. for face in detections:
  10. # 左上角
  11. x = face.left()
  12. y = face.top()
  13. # 右下角
  14. r = face.right()
  15. b = face.bottom()
  16. cv2.rectangle(img,(x,y),(r,b),(0,255,0))
  17. plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

 

三、CNN

  1. import dlib
  2. # 构造CNN人脸检测器
  3. cnn_face_detector = dlib.cnn_face_detection_model_v1("./weights/mmod_human_face_detector.dat")
  4. # 检测人脸 参数与上一种相似
  5. detections = cnn_face_detector(img,1)
  6. for face in detections:
  7. # 左上角
  8. x = face.rect.left()
  9. y = face.rect.top()
  10. # 右下角
  11. r = face.rect.right()
  12. b = face.rect.bottom()
  13. # 置信度
  14. c = face.confidence
  15. print(c)
  16. cv2.rectangle(img,(x,y),(r,b),(0,255,0))
  17. plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

通过神经网络完成,这个过程中我们还可以查看每张脸检测时的置信度。

 

四、SSD

  1. # 加载模型
  2. face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt','./weights/res10_300x300_ssd_iter_140000.caffemodel')
  3. # 原图尺寸
  4. img_height = img.shape[0]
  5. img_width = img.shape[1]
  6. # 放缩至输入尺寸
  7. img_resized = cv2.resize(img,(500,300))
  8. # 转为2进制
  9. img_blob = cv2.dnn.blobFromImage(img_resized,1.0,(500,300),(104.0,177.0,123.0))
  10. # 输入
  11. face_detector.setInput(img_blob)
  12. # 推理
  13. detections = face_detector.forward()

此时

detections.shape # (1, 1, 200, 7)

说明有200个结果,后面的7则是我们做需要的一些数据,继续如下:

  1. # 查看人脸数量
  2. num_of_detections = detections.shape[2]
  3. img_copy = img.copy()
  4. for index in range(num_of_detections):
  5. # 置信度
  6. detections_confidence = detections[0,0,index,2]
  7. # 通过置信度筛选
  8. if detections_confidence > 0.15:
  9. # 位置 乘以宽高恢复大小
  10. locations = detections[0,0,index,3:7] * np.array([img_width,img_height,img_width,img_height])
  11. # 打印
  12. print(detections_confidence)
  13. lx,ly,rx,ry = locations.astype('int')
  14. # 绘制
  15. cv2.rectangle(img_copy,(lx,ly),(rx,ry),(0,255,0),2)
  16. plt.imshow(cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB))

 

五、MTCNN

  1. # 导入MTCNN
  2. from mtcnn.mtcnn import MTCNN
  3. # 记载模型
  4. face_detetor = MTCNN()
  5. # 检测人脸
  6. detections = face_detetor.detect_faces(img_cvt)
  7. for face in detections:
  8. x,y,w,h = face['box']
  9. cv2.rectangle(img_cvt,(x,y),(x+w,y+h),(0,255,0),2)
  10. plt.imshow(img_cvt)

 

对比

优势劣势
Haar速度最快、清凉、适合算力较小的设备准确度低、偶尔误报、无旋转不变性
HOG+Dlib比Haar准确率高速度比Haar低,计算量大、无旋转不变性、Dlib兼容性问题
SSD比Haar和hog准确率高、深度学习、大小一般低光照片准确率低,受肤色影响。
CNN最准确、误报率低、轻量相对于其他方法慢、计算量大、Dlib兼容性问题

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/31806
推荐阅读
相关标签
  

闽ICP备14008679号