实时人脸关键点源码推荐

 还有效果更好的,高精度轻量级关键点网络,参考我的另一篇博文:

轻量级高精度人脸关键点推荐_jacke121的专栏-CSDN博客

带人脸关键点,pc版70多ms,还可以,戴口罩也有效果,侧脸低头偶尔有偏差

GitHub - Jiahao-UTS/SLPT-master

又发现效果比较好的:

https://github.com/OAID/TengineKit

关键点数据集:

WFLW_images.tar.gz

标注:

WFLW_annotations.tar

SLIM

这个好像是1ms的,关键点和姿态都有,效果比较好,比下面pytorch_face_landmark的好,但是不能区分眨眼:

slim网络结构,有训练代码,wingloss:

https://github.com/hommmm/Peppa-Facial-Landmark-PyTorch

测试代码: 

import time

import cv2
import torch
import torch.onnx
from models.slim import Slim
import onnxruntime as rt
import numpy as np

def export():
    x = torch.randn(1, 3, 160, 160)
    model = Slim()
    model.load_state_dict(torch.load("../pretrained_weights/slim_160_latest.pth", map_location="cpu"))
    model.eval()
    torch.onnx.export(model, x, "../pretrained_weights/slim_160_latest.onnx", input_names=["input1"], output_names=['output1'])


if __name__ == '__main__':
    im_size=160

    sess = rt.InferenceSession("../pretrained_weights/slim_160_latest.onnx")
    input_name = sess.get_inputs()[0].name

    im = cv2.imread(r'd:\qinlan3.jpg')
    im = cv2.resize(im, (im_size, im_size))
    crop_image = (im - 127.0) / 127.0
    crop_image = np.array([np.transpose(crop_image, (2, 0, 1))]).astype(np.float32)
    start = time.time()
    raw = sess.run(None, {input_name: crop_image})[0][0]
    end = time.time()
    print("ONNX Inference Time: {:.6f}".format(end - start))
    font = cv2.FONT_HERSHEY_SIMPLEX
    img_black = np.zeros([800, 800, 3], np.uint8)
    img_black = cv2.resize(im, (800, 800))
    lmks = (np.array(raw[0:136])*160).astype(np.int)
    lmks=lmks[:136].reshape(-1,2)
    for point in lmks:
            im = cv2.circle(im, tuple(point), 2, (0, 255, 0), -1, 1)
            # frame = cv2.putText(frame, "Pitch: {:.4f}".format(PRY_3d[0]), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
            #                     (0, 255, 0), 1, 1)
            # frame = cv2.putText(frame, "Yaw: {:.4f}".format(PRY_3d[1]), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
            #                     (0, 255, 0), 1, 1)
            # frame = cv2.putText(frame, "Roll: {:.4f}".format(PRY_3d[2]), (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
            #                     (0, 255, 0), 1, 1)

    color = (200, 160, 75)
    pred_black = np.round(raw[0:136] * 800).astype(np.int).reshape(-1,2)

    for i in range(pred_black.shape[0]):
        p = tuple(pred_black[i])
        img_black = cv2.putText(img_black, str(i), tuple(p), font, 0.3, (0, 0, 255), 1)
        cv2.circle(img_black, p, 1, color, 1, cv2.LINE_AA)

    cv2.imshow('img_black', img_black[200:, 100:])

    cv2.imshow("Peppa ", im)
    cv2.waitKey(0)

PFLD

神力ai出的98个关键点

AI算法详情页

8ms左右,但是飘的很严重,基本不能用。

人脸,人头都能检测,还有人头姿态

https://github.com/jacke121/headpose_final

https://github.com/rafabs97/headpose_final

gpu,头部姿态需要8ms

效果不错,不能区分眨眼

https://github.com/cunjian/pytorch_face_landmark

test_camera_pfld_onnx.py 50ms

ort_session_landmark = onnxruntime.InferenceSession("pfld.onnx")

test_camera_light_onnx.py

onnx_path = "models/onnx/version-RFB-320.onnx"

效果不好:

https://github.com/polarisZhao/Facial-Landmark-Detection

https://github.com/MUST-AI-Lab/HRNet-Facial-Landmark-Detection

https://github.com/emilianavt/OpenSeeFace

https://github.com/KristianMSchmidt/Kaggle-Google-Landmark-Competetition

有数据集:

https://github.com/cunjian/pytorch_face_landmark

tf 1070 64*64 7ms,可能和torch不兼容

https://github.com/songhengyang/face_landmark_factory

无预训练:

https://github.com/srivastava41099/Facial-Keypoint-Detection

https://github.com/yanuartadityan/cnn-facefeature-detection

https://github.com/jacke121/cnn-facefeature-detection

我自己加了一个detect.py

15个关键点:

This is a quick 4-layers single pool CNN for detecting 15 face keypoints.

81个,有误差,不是特别准,特别快,1ms

https://github.com/codeniko/shape_predictor_81_face_landmarks

https://github.com/justusschock/shapenet

效果一般,不准

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time

import cv2
import math
from shapenet.networks.feature_extractors import Img224x224Kernel7x7SeparatedDims
from shapenet.networks.utils import CustomGroupNorm
import torch
import pytest
import numpy as np

@pytest.mark.parametrize("num_outputs,num_in_channels,norm_class,p_dropout",
                         [
                             (16, 1, torch.nn.InstanceNorm2d, 0.1),
                             (16, 1, torch.nn.BatchNorm2d, 0.5),
                             (16, 1, CustomGroupNorm, 0.5),
                             (75, 125, torch.nn.InstanceNorm2d, 0.),
                             (75, 125, torch.nn.BatchNorm2d, 0.),
                             (75, 125, CustomGroupNorm, 0.)
                         ])
def test_224_img_size_7_kernel_size_separated_dims(num_outputs, num_in_channels,
                                                   norm_class, p_dropout):
    net = Img224x224Kernel7x7SeparatedDims(num_in_channels, num_outputs,
                                           norm_class, p_dropout)

    state_dict = torch.load('pretrained_face.ptj')
    net.load_state_dict(state_dict)

    net.cuda()
    net.eval()
    input_tensor = torch.rand(1, num_in_channels, 224, 224).cuda()

    for i in range(1):
        start = time.time()
        out= net(input_tensor)#.shape == (16, num_outputs, 1, 1)
        print('time', time.time() - start)
        print(out)

if __name__ == '__main__':
    # num_outputs= (16, 1, torch.nn.InstanceNorm2d, 0.1)

    # from shapenet.jit import JitHomogeneousShapeLayer
    # from shapenet.jit import JitShapeNetwork
    #
    import torch
    import numpy as np

    net= torch.jit.load('pretrained_face.ptj')
    net.cuda()
    net.eval()
    vc = cv2.VideoCapture(0)  # 读入视频文件
    d = 0
    exit_flag = False
    c = 0


    index = 1
    while True:  # 循环读取视频帧
        image=cv2.imread('shu.jpg')
        c += 1
        # if c % 2 == 1:
        #     continue
            # image_origin = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        images = []
        images_origin = []
        start=time.time()



        image = cv2.resize(image, (224, 224))#,
                           # interpolation=cv2.INTER_LINEAR)
        image_origin = image
        image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)


        # image -= np.array((104.00699, 116.66877, 122.67892))
        image = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
        image = image.astype(np.float32)
        image = image/ 255.0

        image = image.transpose((1, 0))

        images_origin.append(image_origin)  # keep for save result

        # image /= 255.0
        # image = np.transpose(image, (2, 0, 1))
        # image = np.transpose(image, (1, 0))
        # image = image.astype(np.float32)
        images.append(image)

        print("time", (time.time() - start))
        images = np.asarray([images])
        images_cuda = torch.from_numpy(images).cuda()
        out=net(images_cuda)
        angle=-45
        angle=math.pi/(180/angle)
        center_x=112
        center_y=112
        for i,point in enumerate(out[0]):
            x=point[1]
            y=point[0]
            srx = (x - center_x) * np.cos(angle) + (y - center_y) * np.sin(angle) + center_x

            sry = (y - center_y) * np.cos(angle) - (x - center_x) * np.sin(angle) + center_y
            cv2.circle(image_origin, (int(srx),int(sry)), 2, (0, 0, 255), -1)
            if i>30:
                break

        cv2.imshow('asdf',image_origin)
        cv2.waitKey(1)

    # def test_jit_equality():
    #     layer_kwargs = {"shapes": np.random.rand(26, 68, 2),
    #                     "n_dims": 2,
    #                     "use_cpp": False}
    #
    #     input_tensor = torch.rand(10, 1, 224, 224)
    #
    #     jit_net = JitShapeNetwork(JitHomogeneousShapeLayer, layer_kwargs)
    #
    #     out= torch.jit.trace(jit_net, (torch.rand(1, 1, 224, 224)))
    #
    #     test_224_img_size_7_kernel_size_separated_dims(75, 125, torch.nn.BatchNorm2d, 0)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,其中包含了许多用于图像处理和机器视觉的算法和工具。人脸识别是OpenCV库中的一个重要功能,下面将简要介绍OpenCV人脸识别的核心源码OpenCV人脸识别的核心源码主要包含以下几个模块: 1. 人脸检测(Face Detection): OpenCV利用级联分类器(Cascade Classifier)实现了人脸检测功能,其中最常用的是Haar特征分类器和深度学习模型。Haar特征分类器是一种基于Haar特征的机器学习算法,通过训练分类器来识别人脸的特征,例如眼睛、嘴巴等。深度学习模型则是利用深度神经网络来实现人脸检测,例如一些基于卷积神经网络(CNN)的模型,如SSD、YOLO等。 2. 人脸对齐(Face Alignment): 人脸对齐是人脸识别中的重要步骤,其目的是将检测到的人脸在图像中进行精确的对齐,以利于后续的特征提取和匹配。OpenCV中常用的人脸对齐算法包括基于2D关键点的仿射变换和基于3D模型的三维形状重建。 3. 特征提取(Feature Extraction): 特征提取是人脸识别的核心步骤,通过提取人脸图像中的特征来表示该人脸的唯一性。OpenCV中常用的特征提取算法有局部二值模式(Local Binary Patterns, LBP)、主成分分析(Principal Component Analysis, PCA)以及深度学习模型中的卷积层特征等。 4. 特征匹配(Feature Matching): 特征匹配是人脸识别中的关键步骤,通过比较待识别人脸的特征与已知人脸特征进行匹配,从而确定是否为同一个人。OpenCV中一种常用的特征匹配算法是基于人脸特征向量的欧氏距离或余弦相似度进行比较,通过设定阈值来判断是否为同一个人。 综上所述,OpenCV人脸识别的核心源码包含了人脸检测、人脸对齐、特征提取和特征匹配等多个模块,通过不同的算法和技术实现了人脸识别的功能。这些源码可以帮助开发者理解和实现人脸识别系统,从而应用于人脸识别、人脸验证、人脸跟踪等相关领域。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值