Update YOLOv8-ONNXRuntime-CPP example with GPU inference (#4328)

Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Onuralp SEZER 2023-08-13 18:34:39 +03:00 committed by GitHub
parent f6b58e9d75
commit b5d1af42d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 364 additions and 362 deletions

View file

@ -3,42 +3,41 @@
#include <filesystem>
#include <fstream>
void file_iterator(DCSP_CORE*& p)
{
std::filesystem::path current_path = std::filesystem::current_path();
std::filesystem::path imgs_path = current_path/"images";
for (auto& i : std::filesystem::directory_iterator(imgs_path))
{
if (i.path().extension() == ".jpg" || i.path().extension() == ".png")
{
std::string img_path = i.path().string();
cv::Mat img = cv::imread(img_path);
std::vector<DCSP_RESULT> res;
p->RunSession(img, res);
void file_iterator(DCSP_CORE *&p) {
std::filesystem::path current_path = std::filesystem::current_path();
std::filesystem::path imgs_path = current_path / "images";
for (auto &i: std::filesystem::directory_iterator(imgs_path)) {
if (i.path().extension() == ".jpg" || i.path().extension() == ".png" || i.path().extension() == ".jpeg") {
std::string img_path = i.path().string();
cv::Mat img = cv::imread(img_path);
std::vector<DCSP_RESULT> res;
p->RunSession(img, res);
for (auto & re : res)
{
cv::rectangle(img, re.box, cv::Scalar(0, 0 , 255), 3);
std::string label = p->classes[re.classId];
for (auto &re: res) {
cv::RNG rng(cv::getTickCount());
cv::Scalar color(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
cv::rectangle(img, re.box, color, 3);
std::string label = p->classes[re.classId] + " " + std::to_string(re.confidence);
cv::putText(
img,
label,
cv::Point(re.box.x, re.box.y - 5),
cv::FONT_HERSHEY_SIMPLEX,
0.75,
cv::Scalar(255, 255, 0),
color,
2
);
}
cv::imshow("Result", img);
cv::waitKey(0);
cv::destroyAllWindows();
}
}
}
std::cout << "Press any key to exit" << std::endl;
cv::imshow("Result of Detection", img);
cv::waitKey(0);
cv::destroyAllWindows();
}
}
}
int read_coco_yaml(DCSP_CORE*& p)
{
int read_coco_yaml(DCSP_CORE *&p) {
// Open the YAML file
std::ifstream file("coco.yaml");
if (!file.is_open()) {
@ -80,17 +79,19 @@ int read_coco_yaml(DCSP_CORE*& p)
}
int main()
{
DCSP_CORE* yoloDetector = new DCSP_CORE;
std::string model_path = "yolov8n.onnx";
int main() {
DCSP_CORE *yoloDetector = new DCSP_CORE;
std::string model_path = "yolov8n.onnx";
read_coco_yaml(yoloDetector);
// GPU FP32 inference
DCSP_INIT_PARAM params{ model_path, YOLO_ORIGIN_V8, {640, 640}, 0.1, 0.5, true };
#ifdef USE_CUDA
// GPU FP32 inference
DCSP_INIT_PARAM params{ model_path, YOLO_ORIGIN_V8, {640, 640}, 0.1, 0.5, true };
// GPU FP16 inference
// DCSP_INIT_PARAM params{ model_path, YOLO_ORIGIN_V8_HALF, {640, 640}, 0.1, 0.5, true };
// CPU inference
// DCSP_INIT_PARAM params{ model_path, YOLO_ORIGIN_V8, {640, 640}, 0.1, 0.5, false };
// DCSP_INIT_PARAM params{ model_path, YOLO_ORIGIN_V8_HALF, {640, 640}, 0.1, 0.5, true };
#else
// CPU inference
DCSP_INIT_PARAM params{model_path, YOLO_ORIGIN_V8, {640, 640}, 0.1, 0.5, false};
#endif
yoloDetector->CreateSession(params);
file_iterator(yoloDetector);
file_iterator(yoloDetector);
}