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

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;

    private:
        bool use_gpu=true;
        vector<float> input_size={640,640};
        vector<float> variances={0.1,0.2};
        Scalar mean = Scalar(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;
        ScheduleConfig config;
        vector<vector<float>> anchors;

    private:
        // 生成anchors
        vector<vector<float>> priorBox(vector<float> 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(string model_path){
            net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_path.c_str()));//创建解释器
            config.numThread = 8;
            config.type = MNN_FORWARD_CPU;
            session = net->createSession(config);//创建session 
            anchors=priorBox(input_size);
        }

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