c++ yolov5 onnx

InferOnxx.h

#include <fstream>
#include <sstream>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <onnxruntime_cxx_api.h>

using namespace std;
using namespace cv;
using namespace Ort;

struct Net_config
{
    float confThreshold; // Confidence threshold
    float nmsThreshold;  // Non-maximum suppression threshold
    float objThreshold;  // Object Confidence threshold
    string modelpath;    // model path
    bool gpu = false;   // using gpu
};

typedef struct BoxInfo
{
    float x1;
    float y1;
    float x2;
    float y2;
    float score;
    int label;
} BoxInfo;

int endsWith(string s, string sub);

const float anchors_640[3][6] = { {10.0,  13.0, 16.0,  30.0,  33.0,  23.0},
                                 {30.0,  61.0, 62.0,  45.0,  59.0,  119.0},
                                 {116.0, 90.0, 156.0, 198.0, 373.0, 326.0} };

const float anchors_1280[4][6] = { {19, 27, 44, 40, 38, 94},{96, 68, 86, 152, 180, 137},{140, 301, 303, 264, 238, 542},
                       {436, 615, 739, 380, 925, 792} };

class YOLO
{
public:
    YOLO(Net_config config);
    void detect(Mat& frame);
private:
    float* anchors;
    int num_stride;
    int inpWidth;
    int inpHeight;
    int nout;
    int num_proposal;
    vector<string> class_names;
    int num_class;
    int seg_num_class;
    float confThreshold;
    float nmsThreshold;
    float objThreshold;
    const bool keep_ratio = true;
    vector<float> input_image_;
    void normalize_(Mat img);
    void nms(vector<BoxInfo>& input_boxes);
    Mat resize_image(Mat srcimg, int* newh, int* neww, int* top, int* left);
  
    Env env = Env(ORT_LOGGING_LEVEL_ERROR, "yolov5s");
    Ort::Session* ort_session = nullptr;
    SessionOptions sessionOptions = SessionOptions();
    vector<const char* > input_names;
    vector<const char* > output_names;
    vector<vector<int64_t>> input_node_dims; // >=1 outputs
    vector<vector<int64_t>> output_node_dims; // >=1 outputs
    std::vector<AllocatedStringPtr> In_AllocatedStringPtr;
    std::vector<AllocatedStringPtr> Out_AllocatedStringPtr;
};


InferOnxx.cpp

#include "inferOnxx.h"

int endsWith(string s, string sub) {
    return s.rfind(sub) == (s.length() - sub.length()) ? 1 : 0;
}

YOLO::YOLO(Net_config config)
{
    this->confThreshold = config.confThreshold;
    this->nmsThreshold = config.nmsThreshold;
    this->objThreshold = config.objThreshold;

    string classesFile = "class.names";
    string model_path = config.modelpath;
    std::wstring widestr = st
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值