c7e48e8e by 乔峰昇

the first submit

0 parents
opencv4.5.5
模型初始化
FaceRecognize face_rec=FaceRecognize(det_model_path,landm_model_path,rec_model_path)
det_model_path:人脸检测模型retinaface的onnx模型路径
landm_model_path:106人脸关键点模型的onnx模型路径
rec_model_path:人脸识别模型的onnx模型路径
重要参数(.h文件)
bool use_gpu=true; //是否使用gpu
float confidence_threshold = 0.5; //人脸检测阈值
float nms_threshold = 0.4; //nms阈值
double face_recongnize_thr = 0.2; //人脸相似度阈值
接口(返回结果 bool:true/false)
bool face_recognize(string image1_path,string image2_path);参数为两张图像地址,其中iamge1_path为face_id图像输入
bool face_recognize_image(Mat image1,Mat image2);参数为两张opencv读取的图像矩阵,其中iamge1为face_id图像输入
编译
g++ main.cpp -L . -lfacerecognize -o main
如果报错无法找到改用:g++ main.cpp -L . -lfacerecognize -o main pkg-config --libs --cflags opencv4
./main
No preview for this file type
No preview for this file type
#ifndef FACERECOGNIZE_H
#define FACERECOGNIZE_H
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
struct Bbox{
float xmin;
float ymin;
float xmax;
float ymax;
float score;
float x1;
float y1;
float x2;
float y2;
float x3;
float y3;
float x4;
float y4;
float x5;
float y5;
};
class FaceRecognize{
private:
//是否使用gpu?
bool use_gpu=true;
//人脸检测
vector<float> input_size={640,640};
vector<float> variances={0.1,0.2};
vector<float> mean_data={104,117,128};
float confidence_threshold = 0.5; //人脸检测阈值
float keep_top_k = 100;
float vis_threshold = 0.5;
float nms_threshold = 0.4;
float resize_scale = 1.0;
bool is_bbox_process=true; //人脸外扩
//人脸识别
double face_recongnize_thr = 0.2; //人脸相似度阈值
cv::dnn::Net det_net,landm_net,rec_net;
public:
FaceRecognize();
FaceRecognize(string face_det_model_path,string face_landm_model_path,string face_rec_model_path){
det_net = cv::dnn::readNetFromONNX(face_det_model_path);
landm_net = cv::dnn::readNetFromONNX(face_landm_model_path);
rec_net = cv::dnn::readNetFromONNX(face_rec_model_path);
if(use_gpu){
det_net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
det_net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
landm_net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
landm_net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
rec_net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
rec_net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
}
}
//人脸检测部分
vector<vector<float>> priorBox(vector<float> image_size);
vector<Bbox> decode(vector<vector<float>> loc,vector<vector<float>> score,vector<vector<float>>pre,vector<vector<float>> priors,vector<float> variances);
void nms_cpu(std::vector<Bbox> &bboxes, float threshold);
vector<vector<float>> mat2vector(Mat mat);
vector<Bbox> select_score(vector<Bbox> bboxes,float threshold,float w_r,float h_r);
vector<Bbox> bbox_process(vector<Bbox> bboxes,float frame_w,float frame_h);
vector<Bbox> detect(string image_path);
vector<Bbox> detect_image(Mat image);
//人脸关键点部分
vector<vector<float>> detect_landmarks(string image_path);
vector<vector<float>> detect_image_landmarks(cv::Mat image);
//人脸识部分
cv::Mat meanAxis0(const cv::Mat &src);
cv::Mat elementwiseMinus(const cv::Mat &A,const cv::Mat &B);
cv::Mat varAxis0(const cv::Mat &src);
int MatrixRank(cv::Mat M);
cv::Mat similarTransform(cv::Mat src,cv::Mat dst);
Mat preprocess_face(Mat image,vector<vector<float>> land);
double getMold(const vector<double>& vec);
double cos_distance(const vector<double>& base, const vector<double>& target);
double get_samilar(Mat image1,Mat image2);
//整体
bool face_recognize(string image1_path,string image2_path);
bool face_recognize_image(Mat image1,Mat image2);
};
#endif
\ No newline at end of file
No preview for this file type
No preview for this file type
#include"facerecognize.h"
#include "ctime"
int main(){
FaceRecognize face_rec=FaceRecognize("/home/situ/qfs/sdk_project/mobile_face_recognize/det_face_retina_torch_1.4_v0.0.2.onnx","/home/situ/qfs/sdk_project/mobile_face_recognize/det_landmarks_106_v0.0.1.onnx","/home/situ/qfs/sdk_project/mobile_face_recognize/ms1mv3_r18.onnx");
clock_t start, end;
cv::Mat image1=cv::imread("/data/face_recognize/pipeline_test/59297ec0094211ecaf3d00163e514671/310faceImageContent163029410817774.jpg");
cv::Mat image2=cv::imread("/data/face_recognize/pipeline_test/59297ec0094211ecaf3d00163e514671/310cardImageContent163029410836583.jpg");
cout<<"start"<<endl;
start = clock();
bool result=face_rec.face_recognize_image(image1,image2);
end = clock();
double elapsedTime = static_cast<double>(end-start) / CLOCKS_PER_SEC ;
printf("PROCESSING TIME: %f",elapsedTime);
}
This file is too large to display.
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!