From 112a5d8f41442fc727417635f6ee01a42828f2e3 Mon Sep 17 00:00:00 2001 From: triple-Mu Date: Mon, 9 Jan 2023 12:41:24 +0800 Subject: [PATCH] fix opencv bug and engine destory --- csrc/end2end/main.cpp | 29 +++++++++++++++++++++-------- csrc/end2end/yolov8.hpp | 20 +++++++++----------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/csrc/end2end/main.cpp b/csrc/end2end/main.cpp index 186f457..c51167c 100644 --- a/csrc/end2end/main.cpp +++ b/csrc/end2end/main.cpp @@ -29,27 +29,38 @@ int main(int argc, char** argv) auto* yolov8 = new YOLOv8(engine_file_path); yolov8->make_pipe(true); - + cv::Mat res; + cv::namedWindow("result", cv::WINDOW_AUTOSIZE); if (isVideo) { cv::VideoCapture cap(path); cv::Mat image; - - while (cap.isOpened()) + if (!cap.isOpened()) + { + printf("can not open ...\n"); + return -1; + } + double fp_ = cap.get(cv::CAP_PROP_FPS); + int fps = round(1000.0 / fp_); + while (cap.read(image)) { - cap >> image; + auto start = std::chrono::system_clock::now(); yolov8->copy_from_Mat(image); yolov8->infer(); std::vector objs; yolov8->postprocess(objs); - draw_objects(image, objs); - if (cv::waitKey(1) == 'q') + draw_objects(image, res, objs); + auto end = std::chrono::system_clock::now(); + auto tc = std::chrono::duration_cast(end - start).count() / 1000.f; + cv::imshow("result", res); + printf("cost %2.4f ms\n", tc); + if (cv::waitKey(fps) == 'q') { break; } } - cv::destroyAllWindows(); + } else { @@ -66,10 +77,12 @@ int main(int argc, char** argv) std::vector objs; yolov8->postprocess(objs); - draw_objects(image, objs); + draw_objects(image, res, objs); + cv::imshow("result", res); cv::waitKey(0); } } + cv::destroyAllWindows(); delete yolov8; return 0; } diff --git a/csrc/end2end/yolov8.hpp b/csrc/end2end/yolov8.hpp index b8e39c9..1caafd7 100644 --- a/csrc/end2end/yolov8.hpp +++ b/csrc/end2end/yolov8.hpp @@ -182,8 +182,8 @@ YOLOv8::YOLOv8(const std::string& engine_file_path) YOLOv8::~YOLOv8() { - this->engine->destroy(); this->context->destroy(); + this->engine->destroy(); this->runtime->destroy(); cudaStreamDestroy(this->stream); for (auto& ptr : this->buffs) @@ -193,7 +193,7 @@ YOLOv8::~YOLOv8() for (auto& ptr : this->outputs) { - CHECK(cudaFree(ptr)); + CHECK(cudaFreeHost(ptr)); } } @@ -353,13 +353,13 @@ void YOLOv8::postprocess(std::vector& objs) } } -static void draw_objects(const cv::Mat& image, const std::vector& objs) +static void draw_objects(const cv::Mat& image, cv::Mat& res, const std::vector& objs) { - cv::Mat img = image.clone(); + res = image.clone(); for (auto& obj : objs) { cv::Scalar color = cv::Scalar(COLORS[obj.label][0], COLORS[obj.label][1], COLORS[obj.label][2]); - cv::rectangle(img, obj.rect, color, 2); + cv::rectangle(res, obj.rect, color, 2); char text[256]; sprintf(text, "%s %.1f%%", CLASS_NAMES[obj.label], obj.prob * 100); @@ -370,14 +370,12 @@ static void draw_objects(const cv::Mat& image, const std::vector& objs) int x = (int)obj.rect.x; int y = (int)obj.rect.y + 1; - if (y > img.rows) - y = img.rows; + if (y > res.rows) + y = res.rows; - cv::rectangle(img, cv::Rect(x, y, label_size.width, label_size.height + baseLine), RECT_COLOR, -1); + cv::rectangle(res, cv::Rect(x, y, label_size.width, label_size.height + baseLine), RECT_COLOR, -1); - cv::putText(img, text, cv::Point(x, y + label_size.height), + cv::putText(res, text, cv::Point(x, y + label_size.height), cv::FONT_HERSHEY_SIMPLEX, 0.4, TXT_COLOR, 1); } - - cv::imshow("results", img); }