如何使用 opencv 调用 yolov5 onnx 模型 ?
rtx3090 24G release 90 fps by use yolov5n
rtx3090 24G release 20 fps by use yolov5s
#include <fstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
/*!
* use ONNX model
* */
class YoloV5
{
public:
struct Data{
int classIndex;
float confidence;
cv::Rect box;
};
bool init(const std::string & model,bool isUseGPU = true) {
try{
net = cv::dnn::readNetFromONNX(model);
if(isUseGPU) {
net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
std::cout << "SET DNN_BACKEND_CUDA & DNN_TARGET_CUDA" << std::endl;
}else {
net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
std::cout << "SET DNN_BACKEND_OPENCV & DNN_TARGET_CPU" << std::endl;
}
}catch (const cv::Exception &e){
std::cerr << e.what() << std::endl;
return false;
}
return true;
}
void detect(cv::Mat & frame,const std::vector<std::string> &labels,std::vector<YoloV5::Data> &output,float confidenceThreshold