1、环境:win10+cuda11.4+onnxruntime-gpu1.10+opencv3.4.4+vs2019 c++
GPU:本人使用的NVIDIA GeForce GTX 1650 4GB显存。
2、yolov4模型的训练参考:
https://edu.51cto.com/course/22877.html
https://github.com/AlexeyAB/darknet
darknet模型转onnx模型参考:
https://github.com/Tianxiaomo/pytorch-YOLOv4
3、C++调用测试:
#include"deeplearning_yolo_interface.h"
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <codecvt>
#include <numeric>
#include "encoding.h"
static std::string wstringToUtf8(const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> strCnv;
return strCnv.to_bytes(str);
}
int main(int argc,char** argv)
{
std::string modelpath = argv[1];
std::string image_file = argv[2];
std::string gpu_id = argv[3];
std::string yolo_type = argv[4];
int yoloType = atoi(yolo_type.c_str());
int gpuId= atoi(gpu_id.c_str());
//初始化OCR模型
void* modelHandles = DL_YOLO_LIB_CPP::DL_YOLO_Init(modelpath, gpuId, yoloType);
if (modelHandles == NULL)
{
cout << "DL_YOLO_Init failed !!!" << endl;
return -1;
}
std::string image_dir = image_file;
cv::String pattern = image_dir + "/*.jpg";
vector<cv::String> image_list;
cv::glob(pattern, image_list, false);
if (image_list.empty())
{
cout << "images list is null,please check your image dir !!!" << endl;
return -1;
}
for (int i = 0; i < image_list.size(); i++)
{
std::cout << "[" << i + 1 << "/" << image_list.size() << "]" << image_list[i] << endl;
cv::Mat inputImage = cv::imread(image_list[i]);
std::vector<detect_rlt> detResult;
int flg = DL_YOLO_LIB_CPP::DL_YOLO_Detect(modelHandles, inputImage, detResult);
for (auto& rlt: detResult)
{
cv::Rect tmp_rect;
tmp_rect.x = rlt.box.x;
tmp_rect.y = rlt.box.y;
tmp_rect.width = rlt.box.w;
tmp_rect.height = rlt.box.h;
cv::rectangle(inputImage, tmp_rect, cv::Scalar(0, 0, 255), 1);
float score = rlt.box.score;
int class_id = rlt.class_id;
}
/*std::wstring yoloResult = DL_YOLO_LIB_CPP::DL_YOLO_Detect(modelHandles, inputImage);
cout << "DL_OCR_Rec_bankcard_id=" << utf8_to_gbk(wstr_to_utf8(yoloResult)) << endl;*/
}
//模型释放
DL_YOLO_LIB_CPP::DL_YOLO_Free(modelHandles);
return 0;
}
yolo目标检测 C++ onnx使用示例工程分享:
链接: https://pan.baidu.com/s/1Ga_x8auJ9220Nubx0Mw6gA
提取码:kpe9
自己要学习Onnx的C++推导,可学习以下课程:
https://edu.51cto.com/course/30388.html