Git Product home page Git Product logo

liruilongs / mtcnn_demo Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 86.38 MB

人脸检测服务, 用于输出适合人脸识别的 人脸数据集,通过 mtcnn cnn检测人脸,通过 hopenet 开源项目确定人脸是姿态,拿到头部姿态欧拉角,通过 拉普拉斯算子 拿到人脸模糊度,通过对mtcnn 三级网络和置信度,欧拉角阈值,模糊度设置阈值筛选合适人脸

License: Apache License 2.0

Python 100.00%

mtcnn_demo's Introduction

mtcnn_demo

mtcnn Demo

这是一个 人脸检测的 Demo, 用于输出适合人脸识别的 人脸数据集,通过 mtcnn 检测人脸,拿到置信度,通过 Hopenet 确定人脸是姿态,拿到姿态欧拉角,通过 拉普拉斯算子 确定人脸模糊度。

这里默认对人脸做了对齐处理, 提供了 Web 服务 版本和队列版本,需要可以切换分支

  • http 版本可以通过上传文件或者 url 提取人脸信息,返回 JSON 数据,
  • 队列版本会重 redis 读取照片 url`。处理完数据在存储到队列里面

生成结果

python mtcnn_demo.py
原图
在这里插入图片描述
--
生成标记后图片,粉色数据为标记 不合格数据,全部标记为蓝色数据为合规数据,也就是需要处理的数据
在这里插入图片描述
--
标记含义:
在这里插入图片描述

符合条件筛选的人脸

人脸原始图片 对齐后的人脸 头部原始图片 对齐后头部姿态
在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述
在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述
在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述
在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

部署

创建 虚拟环境,导入依赖

(base) C:\Users\liruilong>conda create -n mtcnn python==3.8.8
pip instasll -r  requirements.txt  -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

也可以直接使用 conda 的方式

conda env create -f /environment.yml
source activate mtcnn
pip install -r /requirements.txt  -i  https://pypi.tuna.tsinghua.edu.cn/simple

检测使用 mtcnn

使用的下面的库,关于 mtcnn是什么,这里不多介绍,这里主要看下和识别精度相关的参数

对应的pip 库位置: https://pypi.org/project/mtcnn/

def __init__(self, weights_file: str = None, min_face_size: int = 20, steps_threshold: list = None,
                 scale_factor: float = 0.709):
        """
        Initializes the MTCNN.
        :param weights_file: file uri with the weights of the P, R and O networks from MTCNN. By default it will load
        the ones bundled with the package.
        :param min_face_size: minimum size of the face to detect
        :param steps_threshold: step's thresholds values
        :param scale_factor: scale factor
        """
        if steps_threshold is None:
            steps_threshold = [0.6, 0.7, 0.7]

        if weights_file is None:
            weights_file = pkg_resources.resource_stream('mtcnn', 'data/mtcnn_weights.npy')

        self._min_face_size = min_face_size
        self._steps_threshold = steps_threshold
        self._scale_factor = scale_factor

        self._pnet, self._rnet, self._onet = NetworkFactory().build_P_R_O_nets_from_file(weights_file)

影响 MTCNN 单张测试结果的准确度和测试用时的主要因素为:

网络阈值(steps_threshold)

MTCNN 使用了一系列的阈值来进行人脸检测和关键点定位。这些阈值包括人脸 置信度阈值(Face Confidence Threshold)、人脸框关键点之间的IoU(Intersection over Union)阈值等。上面的构造函数 MTCNN的三个阶段(P-Net、R-Net和O-Net)中,相应的阈值设置为0.6、0.7和0.7。

  1. P-Net阶段,它是一个浅层的卷积神经网络,生成 候选人脸框时,只有置信度大于等于0.6的候选框将被接受,其他低于该阈值的候选框将被拒绝。
  2. R-Net阶段,一个较深的卷积神经网络,用于对P-Net生成的候选框进行筛选和精细调整。R-Net会对每个候选框进行特征提取,并输出判断该框是否包含人脸的概率以及对应的边界框调整值,对于从P-Net阶段获得的候选框,只有置信度大于等于0.7的框将被接受,其他低于该阈值的框将被拒绝。
  3. O-Net阶段,最深的卷积神经网络,用于进一步筛选和精细调整R-Net输出的候选框。O-Net与R-Net类似,对于从R-Net阶段获得的候选框,同样只有置信度大于等于0.7的框将被接受,其他低于该阈值的框将被拒绝。O-Net还可以输出 人脸关键点的位置坐标。最终,O-Net提供了最终的人脸检测结果和人脸关键点的位置信息。

在这里插入图片描述

影响因子(原始图像的比例跨度)(scale_factor):

MTCNN 使用了图像金字塔来检测不同尺度的人脸。通过对图像进行 缩放,可以检测到不同大小的人脸。影响因子是指图像金字塔中的 缩放因子,控制了不同尺度之间的跨度。较小的影响因子会导致 更多的金字塔层级,可以检测到 更小的人脸,但会增加计算时间。较大的影响因子可以 加快检测速度,但可能会错过 较小的人脸。因此,选择合适的影响因子是在准确度和速度之间进行权衡的关键。

要检测的 最小面容参数(min_face_size):

这是 MTCNN 中用于 过滤掉较小人脸的参数。最小面容参数定义了一个 人脸框最小边长,小于此值的人脸将被 忽略。较小的最小面容参数可以检测到更小的人脸,但可能会增加 虚警(错误接受)的机会。较大的最小面容参数可以 减少虚警,但可能会漏检一些较小的人脸。因此,根据应用需求和场景,需要调整最小面容参数以平衡 准确度和召回率

from mtcnn import MTCNN
import cv2

img = cv2.cvtColor(cv2.imread("ivan.jpg"), cv2.COLOR_BGR2RGB)
detector = MTCNN()
detector.detect_faces(img)

box 为人脸矩形框,keypoints 为人脸特征点,confidence 为置信度

[
    {
        'box': [277, 90, 48, 63],
        'keypoints':
        {
            'nose': (303, 131),
            'mouth_right': (313, 141),
            'right_eye': (314, 114),
            'left_eye': (291, 117),
            'mouth_left': (296, 143)
        },
        'confidence': 0.99851983785629272
    }
]

姿态判断 Hopenet

姿态判断使用 Hopenet

在这里插入图片描述

论文地址: https://arxiv.org/abs/1710.00925

使用的模型来自项目

https://github.com/natanielruiz/deep-head-pose

一个 大佬写好的 Demo

https://colab.research.google.com/drive/1vvntbLyVxxBHoVN0e6-pfs7gB3pp-VUS?usp=sharing

模糊度检测 拉普拉斯算子

opencv 拉普拉斯方差方法 方法

在这里插入图片描述

def calculate_blur(image):
    # 计算图像的拉普拉斯梯度
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    laplacian = cv2.Laplacian(gray, cv2.CV_64F).var()
    return laplacian

来源

https://pyimagesearch.com/2015/09/07/blur-detection-with-opencv/

配置文件简单说明:

### 人脸检测配置文件
## mtcnn 检测相关:
mtcnn:
  zero:
    # 最小人脸尺寸
    min_face_size: 20
    # 影响因子
    scale_factor: 0.709
    # 三层网络阈值
    steps_threshold: 
      - 0.6
      - 0.7
      - 0.7
    # 结果置信度阈值
    face_threshold: 0.995
    # 模糊度阈值
    blur_threshold: 100


## hopenet 姿态检测相关
hopenet:
  zero:
    # 模型位置
    snapshot_path: "./content/dhp/hopenet_robust_alpha1.pkl"
    # 欧拉角阈值
    yaw_threshold: 45
    pitch_threshold: 20
    roll_threshold: 25 

# 是否输出结果图片
is_objectification: true
# 输出图片结果
objectification_dir: './output/'
# 需要处理的图片位置
parse_dir: "./mtcnn_test/"

mtcnn_demo's People

Contributors

liruilongs avatar

Stargazers

 avatar  avatar  avatar wang avatar leon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.