retinaface.h 2.23 KB
#ifndef RETINAFACE_H
#define RETINAFACE_H
#include<opencv2/opencv.hpp>
#include<MNN/Interpreter.hpp>
#include<MNN/ImageProcess.hpp>
#include<iostream>
#include<memory>

using namespace MNN;
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{
    public:
        float confidence_threshold = 0.5;
        bool is_bbox_process=true;
        int num_thread = 2;
        MNNForwardType forward_type = MNN_FORWARD_CPU;
    public:
        bool model_init=false;
        vector<int> input_size={640,640};
        vector<float> variances={0.1,0.2};
        float mean[3] = {104.0f, 117.0f, 123.0f};
        float keep_top_k = 100;
        float nms_threshold = 0.4;
        float resize_scale = 1.0;

        std::shared_ptr<MNN::Interpreter> net;
        Session *session = nullptr;
        MNN::Tensor* input_tensor=nullptr;
        shared_ptr<MNN::CV::ImageProcess> pretreat;
        vector<vector<float>> anchors;

    private:
        // 生成anchors
        vector<vector<float>> priorBox(vector<int> image_size);
        // 解析bounding box  landmarks 包含置信度
        vector<Bbox> decode(float *loc,float *score,float *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);
        // 根据阈值筛选
        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);

    public:
    
        RetinaFace();
        // ~RetinaFace();
        RetinaFace(string model_path){
            init_model(model_path);
        }
        bool init_model(string model_path);

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