retinaface.h 2.26 KB
#ifndef RETINAFACE_H
#define RETINAFACE_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 RetinaFace{

    private:
        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.8;
        float keep_top_k = 100;
        float vis_threshold = 0.2;
        float nms_threshold = 0.4;
        float resize_scale = 1.0;
        bool is_bbox_process=true;

        cv::dnn::Net net;
        vector<vector<float>> anchors;

    private:
        // 生成anchors
        vector<vector<float>> priorBox(vector<float> image_size);
        // 解析bounding box  landmarks 包含置信度
        vector<Bbox> decode(Mat loc,Mat score,Mat pre,vector<vector<float>> priors,vector<float> variances);
        // 解析landmarks
        // vector<vector<float>> decode_landm(vector<vector<float>> pre,vector<vector<float>> priors,vector<float> variances);
        //NMS
        void nms_cpu(std::vector<Bbox> &bboxes, float threshold);
        // Mat转vector
        vector<vector<float>> mat2vector(Mat mat);
        // 根据阈值筛选
        vector<Bbox> select_score(vector<Bbox> bboxes,float threshold,float w_r,float h_r);
        // vector转Bbox
        // vector<Bbox> vec2Bbox(vector<vector<float>> bbox,float w_r,float h_r);
        // 数据后处理
        vector<Bbox> bbox_process(bool is_bbox_process,vector<Bbox> bboxes,float frame_w,float frame_h);

    public:
        RetinaFace(){};
        RetinaFace(string model_path){
            net = cv::dnn::readNetFromONNX(model_path);
            if(use_gpu){
                net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
                net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
            }
            anchors=priorBox(input_size);
        }

        // 推理
        vector<Bbox> detect(string image_path);
        vector<Bbox> detect_image(Mat image);
};
#endif