生活随笔
收集整理的這篇文章主要介紹了
基于深度学习的人脸检测与静默活体检测——C++实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
1.系統環境是win10,顯卡RTX3080;cuda10.2,cudnn7.1;OpenCV4.5,ncnn版本是20210525;C++ IDE vs2019。
2.使用NCNN作模型推理加速庫,能更容易的使用GPU進行加速,代碼不需要改動很大就可以移植到邊緣設備或者移動端上。
一、人臉檢測
1.人臉檢測用的yolov5-face,yolov5-face是一種實時、高精度的人臉檢測,搭配NCNN在安卓上(Mate 30 pro)cpu 能跑出18 FPS左右,GPU能跑出25 FPS。算法源碼地址:https://github.com/deepcam-cn/yolov5-face 。論文地址:https://arxiv.org/abs/2105.12931 。
2.人臉檢測效果:
二、活體檢測
1、人臉活體檢測是用來檢測當前攝像頭所檢測到的人臉是否是偽造的,是人臉驗證和人臉識別的前提條件,如果不能檢測出來是否是活體,那么就會出現比如常見用照片,人臉面具,3D人像等其他媒介來騙過人臉識別系統。
2、目前主流的活體解決方案分為配合式活體檢測和非配合式活體檢測(靜默活體檢測)。配合式活體檢測需要用戶根據提示完成指定的動作(比如眨眼,頭往哪邊轉一下),然后再進行活體校驗,靜默活體則在用戶無感的情況下直接進行活體校驗。
3、這里演示的是靜默活體檢測,算法地址:https://github.com/minivision-ai/Silent-Face-Anti-Spoofing 。
三、代碼
1.代碼流程,輸入攝像頭或者視頻,先檢測當前畫面是否存在人臉,如果在用到項目上,也可以做當前存在是否是唯一人臉的檢測,存在人臉則對人臉做活體檢測,檢測當前輸入的人臉是否是活體人臉。
2.代碼:
#include <opencv2/opencv.hpp>
#include "TLive.h"
#include "yoloface.h"
#include <opencv2/video/video.hpp>cv
::Rect
targetResize(const cv
::Mat
& cv_src
, cv
::Mat
& cv_dst
, int target_w
, int target__h
);int main(void)
{int target_w
= 640, target_h
= 480;YoloFace yolo_face
;TLive live
;live
.LoadModel("models/live/");yolo_face
.loadModel("models/face/face_lite");cv
::VideoCapture cap
;cap
.open("face.mp4");if (!cap
.isOpened()){return 0;}cv
::Mat cv_src
;while (1){cap
>> cv_src
;if (cv_src
.empty()){break;}std
::vector
<Object
> objects
;cv
::Mat cv_target
;cv
::Rect rect
= targetResize(cv_src
, cv_target
, target_w
, target_h
);yolo_face
.detection(cv_target
, objects
);for (int i
= 0; i
< objects
.size(); ++i
){float x1
= objects
[i
].rect
.x
;float y1
= objects
[i
].rect
.y
;float x2
= objects
[i
].rect
.width
+ x1
;float y2
= objects
[i
].rect
.height
+ y1
;struct LiveFaceBox LiveBox
= { x1
,y1
,x2
,y2
};float prod
= live
.Detect(cv_target
, LiveBox
);objects
[i
].live
= prod
;}yolo_face
.drawFace(cv_target
, objects
);cv
::Mat
cv_dst(cv_target(rect
));cv
::namedWindow("face", 0);cv
::imshow("face", cv_dst
);cv
::waitKey(20);}cap
.release();return 0;
}
cv
::Rect
targetResize(const cv
::Mat
& cv_src
, cv
::Mat
& cv_dst
, int target_w
, int target_h
)
{float s
;if (cv_src
.cols
> cv_src
.rows
){s
= float(target_w
) / cv_src
.cols
;}else{s
= float(target_h
) / cv_src
.rows
;}float w
= cv_src
.cols
* s
;float h
= cv_src
.rows
* s
;int w_p
= (target_w
- w
) / 2;int h_p
= (target_h
- h
) / 2;cv
::Mat cv_size
;cv
::resize(cv_src
, cv_size
, cv
::Size(w
, h
));cv
::copyMakeBorder(cv_size
, cv_dst
, h_p
, h_p
, w_p
, w_p
, cv
::BORDER_CONSTANT
, 114.f);return cv
::Rect(w_p
, h_p
, cv_size
.cols
, cv_size
.rows
);
}
3.測試結果
先輸入一個全是照片的視頻:
再測試一個正常的活動的人臉:
4.源碼地址:https://download.csdn.net/download/matt45m/84996790
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的基于深度学习的人脸检测与静默活体检测——C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。