人脸检测5种方法

简介: 人脸检测5种方法

基本步骤

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

一、Haar

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

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

上述过程中:

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

二、Hog

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

import dlib
# 构造HOG人脸检测器 不需要参数
hog_face_detetor = dlib.get_frontal_face_detector()
# 检测人脸获取数据
# img 
# scale类似haar的scalFactor
detections = hog_face_detetor(img,1)
# 解析获取的数据
for face in detections:
    # 左上角
    x = face.left()
    y = face.top()
    # 右下角
    r = face.right()
    b = face.bottom()
    cv2.rectangle(img,(x,y),(r,b),(0,255,0))
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

三、CNN

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

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

四、SSD

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

此时

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

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

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

五、MTCNN

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

对比

image.png

相关文章
|
安全 数据处理 网络虚拟化
|
Java 流计算
利用java8 的 CompletableFuture 优化 Flink 程序
本文探讨了Flink使用avatorscript脚本语言时遇到的性能瓶颈,并通过CompletableFuture优化代码,显著提升了Flink的QPS。文中详细介绍了avatorscript的使用方法,包括自定义函数、从Map中取值、使用Java工具类及AviatorScript函数等,帮助读者更好地理解和应用avatorscript。
237 2
利用java8 的 CompletableFuture 优化 Flink 程序
|
12月前
|
网络协议 数据库 网络架构
OSPF的邻居状态机详解
OSPF的邻居状态机详解
649 6
|
分布式计算 Java 数据处理
Apache Spark优缺点大揭秘
【10月更文挑战第12天】
466 11
|
人工智能 自然语言处理 PyTorch
Prompt-“设计提示模板:用更少数据实现预训练模型的卓越表现,助力Few-Shot和Zero-Shot任务”
Prompt-“设计提示模板:用更少数据实现预训练模型的卓越表现,助力Few-Shot和Zero-Shot任务”
Prompt-“设计提示模板:用更少数据实现预训练模型的卓越表现,助力Few-Shot和Zero-Shot任务”
|
关系型数据库 MySQL 数据库
使用ZIP包安装MySQL及配置教程
使用ZIP包安装MySQL及配置教程
1453 4
【sgDragMove】自定义组件:自定义拖拽组件,仅支持拖拽、设置吸附屏幕边界距离。
【sgDragMove】自定义组件:自定义拖拽组件,仅支持拖拽、设置吸附屏幕边界距离。
|
网络协议 KVM 虚拟化
获取kvm的ip地址的三种方式
获取kvm的ip地址的三种方式
1210 0
|
弹性计算 CDN
阿里云服务器香港节点和北京深圳上海杭州地域的区别对比
阿里云中国香港地域服务器和中国大陆地域有什么区别?阿里云百科分别从备案、网络延迟速度及价格三方面来详细对比
1792 0
阿里云服务器香港节点和北京深圳上海杭州地域的区别对比