123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #pragma once
- #ifdef ORTTOOLKIT
- #define ORTTOOLKIT __declspec(dllexport)
- #else
- #define ORTTOOLKIT __declspec(dllimport)
- #endif
- #include <vector>
- #include <array>
- #include <opencv/cv.h>
- #include <onnxruntime/onnxruntime_cxx_api.h>
- #include "Type.h"
- namespace OrtToolkit {
- namespace Vision {
- namespace Detect {
- /// <summary>
- /// 飞桨PP_YOLOE转ONNX模型BatchSize只能为1
- /// </summary>
- class ORTTOOLKIT PP_YOLOE
- {
- public:
- /// <summary>
- /// PP_YOLOE模型初始化
- /// </summary>
- /// <param name="model_path">模型路径</param>
- /// <param name="confThreshold">置信度阈值</param>
- /// <param name="ExecutionProvider">ONNX概念下的 执行提供程序 (EP),参数为字符串,默认为CPU,其他:CUDA</param>
- PP_YOLOE(std::string model_path, float confThreshold, std::string ExecutionProvider = "CUDA");
- /// <summary>
- /// 目标检测
- /// </summary>
- /// <param name="cv_image">输入OpenCV的BGR图像</param>
- void Predict(cv::Mat& cv_image, Type::Res_PP_YOLOE& res);
- private:
- float confThreshold;//置信度阈值
- cv::Mat preprocess(cv::Mat srcimg);
- const int inpWidth = 640;
- const int inpHeight = 640;
- std::vector<float> mean_ = { 0.485, 0.456, 0.406 };
- std::vector<float> std_ = { 0.229, 0.224, 0.225 };
- std::vector<float> input_image_;
- std::vector<float> scale_factor = { 1,1 };
- Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_ERROR, "PP_YOLOE");
- Ort::Session* ort_session = nullptr;
- Ort::SessionOptions sessionOptions = Ort::SessionOptions();
- std::vector<char*> input_names;
- std::vector<char*> output_names;
- std::vector<std::vector<int64_t>> input_node_dims; // 输入节点维度
- std::vector<std::vector<int64_t>> output_node_dims; // 输出节点维度
- };
- }
- namespace FaceDetect {
- class ORTTOOLKIT YOLOV7_face
- {
- public:
- YOLOV7_face(float confThreshold, float nmsThreshold, std::string modelpath, std::string ExecutionProvider = "CUDA");
- void Predict(cv::Mat& frame, Type::Res_YOLOV7_FACE& generate_boxes);
- private:
- int inpWidth;
- int inpHeight;
- int nout;
- int num_proposal;
- float confThreshold;//置信度阈值
- float nmsThreshold;//nms阈值
- std::vector<float> mean_ = { 0.5,0.5,0.5 };
- std::vector<float> std_ = { 0.5,0.5,0.5 };
- std::vector<float> input_image_;//模型输入图片
- void nms(std::vector<Type::Type_Iou_YOLOV7FACE>Items, Type::Res_YOLOV7_FACE& Res);//NMS
- bool has_postprocess;
- std::vector<Ort::AllocatedStringPtr>In_AllocatedStringPtr;
- std::vector<Ort::AllocatedStringPtr>Out_AllocatedStringPtr;
- Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_ERROR, "YOLOV7_face");
- Ort::Session* ort_session = nullptr;//OnnxRunTime会话
- Ort::SessionOptions sessionOptions = Ort::SessionOptions();
- std::vector<char*> input_names;
- std::vector<char*> output_names;
- std::vector<std::vector<int64_t>> input_node_dims; // >=1 outputs
- std::vector<std::vector<int64_t>> output_node_dims; // >=1 outputs
- };
- }
- namespace FaceAlign {
- class ORTTOOLKIT PFLD98
- {
- public:
- PFLD98(std::string ModPath);
- void Predict(cv::Mat& Img, std::vector<cv::Point2f>& Density);
- ~PFLD98();
- private:
- Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, "MCNN");
- Ort::SessionOptions sessionOptions = Ort::SessionOptions();
- Ort::AllocatorWithDefaultOptions allocator;
- const char* input_name;
- const char* output_nameA;
- const char* output_nameB;
- std::vector<int64_t> input_dims;
- std::vector<int64_t> output_dims;
- Ort::Session* ort_session;
- std::vector<const char*> input_names;
- std::vector<const char*> output_names;
- std::vector<const char*> input_node_names;
- std::vector<const char*> output_node_names;
- private:
- int DensityRow;
- int DensityCol;
- std::vector<float> input_image_;
- };
- }
- namespace FaceId {
- class ORTTOOLKIT Arcface {
- public:
- Arcface(std::string modelpath, std::string ExecutionProvider = "CUDA");
- void Predict(cv::Mat& frame, std::vector<float>& FaceVector);
- private:
- int inpWidth;
- int inpHeight;
- int nout;
- std::vector<float> input_image_;//模型输入图片
- std::vector<float> mean_ = { 0.5, 0.5, 0.5 };
- std::vector<float> std_ = { 0.5, 0.5, 0.5 };
- std::vector<Ort::AllocatedStringPtr>In_AllocatedStringPtr;
- std::vector<Ort::AllocatedStringPtr>Out_AllocatedStringPtr;
- Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_ERROR, "Arcface");//日志记录
- Ort::Session* ort_session = nullptr;//OnnxRunTime会话
- Ort::SessionOptions sessionOptions = Ort::SessionOptions();
- std::vector<char*> input_names;
- std::vector<char*> output_names;
- std::vector<std::vector<int64_t>> input_node_dims; // >=1 outputs
- std::vector<std::vector<int64_t>> output_node_dims; // >=1 outputs
- };
- }
- namespace CrowdCounting {
- class ORTTOOLKIT MCNN
- {
- public:
- MCNN(std::string ModPath);
- void Predict(cv::Mat& Img, std::vector<float>& Density);
- ~MCNN();
- private:
- Ort::Env env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, "MCNN");
- Ort::SessionOptions sessionOptions = Ort::SessionOptions();
- Ort::AllocatorWithDefaultOptions allocator;
- const char* input_name;
- const char* output_name;
- std::vector<int64_t> input_dims;
- std::vector<int64_t> output_dims;
- Ort::Session* ort_session;
- std::vector<const char*> input_names;
- std::vector<const char*> output_names;
- std::vector<const char*> input_node_names;
- std::vector<const char*> output_node_names;
- std::vector<float> input_image_;
- private:
- int DensityRow;
- int DensityCol;
- };
- }
- }
- namespace Util{
- void ORTTOOLKIT overflow(float& info, int lower, int upper);
- void ORTTOOLKIT BoxOverflow(Type::Base_Type_Box& Box, int lower, int upper);
- float ORTTOOLKIT CosineSimilarity(const std::vector<float>& a, const std::vector<float>& b, bool normalized = false);
- std::string ORTTOOLKIT create_uuid();
- uint16_t floatToHalf(float f);
- float halfToFloat(uint16_t h);
- template <typename T, typename U>
- void ReadBin(U& x, std::string path) {
- std::ifstream file(path, std::ios::binary);
- if (file) {
- file.seekg(0, std::ios::end);
- std::streampos size = file.tellg();
- file.seekg(0, std::ios::beg);
- x.resize(size / sizeof(T));
- file.read(reinterpret_cast<char*>(x.data()), size);
- file.close();
- }
- };
- }
- }
|