facerecognize.h 3.35 KB
#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);
            }
            
        }

    private:
        //人脸检测部分
        vector<vector<float>> priorBox(vector<float> image_size);
        vector<Bbox> decode(Mat loc,Mat score,Mat 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);

    public:
        //人脸识别整体推理
        bool face_recognize(string image1_path,string image2_path);
        bool face_recognize_image(Mat image1,Mat image2);
};


#endif