当前位置:网站首页>Three methods for extracting facial features
Three methods for extracting facial features
2022-08-05 10:50:00 【HUAWEI CLOUD】
@[toc]
第一种方法 直接使用dlib.
安装dlib方法:
https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/121470556
思路:
1、使用dlib.get_frontal_face_detector()方法检测人脸的位置.
2、使用 dlib.shape_predictor()方法得到人脸的关键点.
3、使用dlib.face_recognition_model_v1()方法提取特征.
新建face_embedding1.py,插入代码:
import dlib,numpyimport cv2# 人脸关键点检测器predictor_path = "shape_predictor_68_face_landmarks.dat"# 人脸识别模型、提取特征值face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"predictor_path是恋人关键点检测器模型的路径.
face_rec_model_path是提取人脸特征的路径.
# 加载模型detector = dlib.get_frontal_face_detector() #人脸检测sp = dlib.shape_predictor(predictor_path) #关键点检测facerec = dlib.face_recognition_model_v1(face_rec_model_path)# 编码分别初始化人脸检测、关键点检测、特征编码方法.
image_path='train_images/11.jpg'image = cv2.imread(image_path)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)# 人脸检测dets = detector(image, 1)if len(dets)==1: print('检测到人脸')shape = sp(image, dets[0])# 关键点# 提取特征face_descriptor = facerec.compute_face_descriptor(image, shape)#获取到128位的编码v = numpy.array(face_descriptor)print(v)读取图片.然后将图片转为RGB格式.
检测人脸.
获取人脸的68个关键点.
获取128位人脸编码.
使用感受: 使用dlib.get_frontal_face_detector()检测人脸效果一般,模糊的人脸检测不出来.速度上也是比较慢.
第二种方法 使用深度学习方法查找人脸,dlib提取特征.
思路:
这种方法使用 cv2自带的dnn.readNetFromCaffe方法,加载深度学习模型实现人脸的检测.然后继续使用dlib提取人脸特征.
新建face_embedding2.py,插入代码:
import dlib,numpyimport cv2# 人脸关键点检测器predictor_path = "shape_predictor_68_face_landmarks.dat"# 人脸识别模型、提取特征值face_rec_model_path = "dlib_face_recognition_resnet_model_v1.dat"prototxt_path = 'deploy.proto.txt'model_path = 'res10_300x300_ssd_iter_140000_fp16.caffemodel'导入需要的包.
定义模型的路径.
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)sp = dlib.shape_predictor(predictor_path) #关键点检测facerec = dlib.face_recognition_model_v1(face_rec_model_path)# 编码初始化人脸检测模型、关键点检测模型、人脸特征提取模型.
image_path='train_images/11.jpg'image = cv2.imread(image_path)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)(h, w) = image.shape[:2]blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))net.setInput(blob)detections = net.forward()startX, startY, endX, endY = 0, 0, 0, 0for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the # prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > 0.5: # compute the (x, y)-coordinates of the bounding box for the # object box = detections[0, 0, i, 3:7] * numpy.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") breakrect = dlib.rectangle(startX, startY, endX, endY)这部分的代码主要是人脸检测逻辑.
读取图片,并将其改为RGB格式.
获取图片的大小.
初始化blob.
net.forward()计算人脸的位置.
遍历检测结果
如果置信度大于0.5,则认为是合格的人脸.
计算出人脸的坐标.
将坐标转为dlib.rectangle对象.
shape = sp(image, rect)print(shape)# 提取特征face_descriptor = facerec.compute_face_descriptor(image, shape)#获取到128位的编码v = numpy.array(face_descriptor)print(v)计算人脸的关键点.
提取人脸的特征.
使用感受:使用深度学习模型提取人脸特征,无论速度还是准确率都有很大的提高,即使很模糊的图像依然能检测到.
第三种使用insightface提取人脸特征
InsightFace 是一个开源的 2D&3D 深度人脸分析工具箱,其中高效地实现了丰富多样的人脸识别、人脸检测和人脸对齐算法,并且针对训练和部署进行了优化,在多项算法测评、比赛获得优胜.
安装InsightFace
pip install insightfacepip install onnxruntime-gpu==1.9.0 注意:onnxruntime安装1.9以下的版本.
提取特征
新建face_embedding3.py 插入代码:
import insightfaceimport cv2model = insightface.app.FaceAnalysis()model.prepare(ctx_id=0, det_thresh=0.45)face_img = cv2.imread('train_images/11.jpg')res = model.get(face_img)print('embedding: ', res[0].embedding) 初始化FaceAnalysis()模型.
设置置信度位0.45.
读取图片
使用模型预测.
打印人脸特征res[0].embedding.
除了能人脸特征外,还有一些其他的属性,比如:bbox、kps、landmark_3d_68、landmark_2d_106、age、gender .可以通过res[0].keys()查看.
使用感受:速度比较慢,精度还行.
完整的代码和模型:
https://download.csdn.net/download/hhhhhhhhhhwwwwwwwwww/85345364
边栏推荐
- 上位机开发C#语言:模拟STC串口助手接收单片机发送数据
- Import Excel/CSV from Sub Grid within Dynamics 365
- Our Web3 Entrepreneurship Project, Yellow
- sqlserver编写通用脚本实现获取一年前日期的方法
- Go compilation principle series 6 (type checking)
- Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
- 第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
- Opencv图像缩放和平移
- R语言ggplot2可视化:可视化密度图(Density plot)、可视化多个分组的密度图、数据点分布在箱图中间、添加主标题、副标题、题注信息
- STM32入门开发:编写XPT2046电阻触摸屏驱动(模拟SPI)
猜你喜欢

教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!

【MindSpore Easy-Diantong Robot-01】You may have seen many knowledge quiz robots, but this one is a bit different
![[Strong Net Cup 2022] WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[Strong Net Cup 2022] WP-UM

Getting started with Polkadot parachain development, this article is enough

【加密解密】明文加密解密-已实现【已应用】

工程设备在线监测管理系统自动预警功能

HDD杭州站•ArkUI让开发更灵活

E-sports, convenience, efficiency, security, key words for OriginOS functions

Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database

Create a Dapp, why choose Polkadot?
随机推荐
Score interview (1)----related to business
2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
电气工程的标准是什么
How OpenHarmony Query Device Type
机器学习-基础知识 - Precision, Recall, Sensitivity, Specificity, Accuracy, FNR, FPR, TPR, TNR, F1 Score, Bal
一张图看懂 SQL 的各种 join 用法!
static linking and dynamic linking
2022技能大赛训练题:交换机snmp配置
The fuse: OAuth 2.0 four authorized login methods must read
In-depth understanding of timeout settings for Istio traffic management
Opencv图像缩放和平移
【MySQL基础】-【数据处理之增删改】
七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
API 网关简述
导火索:OAuth 2.0四种授权登录方式必读
STM32入门开发:编写XPT2046电阻触摸屏驱动(模拟SPI)
Latex如何控制表格的宽度和高度
Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)