fix opencv bug and engine destory

pull/1/head
triple-Mu 2 years ago
parent 398cff92d6
commit 112a5d8f41
  1. 29
      csrc/end2end/main.cpp
  2. 20
      csrc/end2end/yolov8.hpp

@ -29,27 +29,38 @@ int main(int argc, char** argv)
auto* yolov8 = new YOLOv8(engine_file_path); auto* yolov8 = new YOLOv8(engine_file_path);
yolov8->make_pipe(true); yolov8->make_pipe(true);
cv::Mat res;
cv::namedWindow("result", cv::WINDOW_AUTOSIZE);
if (isVideo) if (isVideo)
{ {
cv::VideoCapture cap(path); cv::VideoCapture cap(path);
cv::Mat image; cv::Mat image;
if (!cap.isOpened())
while (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->copy_from_Mat(image);
yolov8->infer(); yolov8->infer();
std::vector<Object> objs; std::vector<Object> objs;
yolov8->postprocess(objs); yolov8->postprocess(objs);
draw_objects(image, objs); draw_objects(image, res, objs);
if (cv::waitKey(1) == 'q') auto end = std::chrono::system_clock::now();
auto tc = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.f;
cv::imshow("result", res);
printf("cost %2.4f ms\n", tc);
if (cv::waitKey(fps) == 'q')
{ {
break; break;
} }
} }
cv::destroyAllWindows();
} }
else else
{ {
@ -66,10 +77,12 @@ int main(int argc, char** argv)
std::vector<Object> objs; std::vector<Object> objs;
yolov8->postprocess(objs); yolov8->postprocess(objs);
draw_objects(image, objs); draw_objects(image, res, objs);
cv::imshow("result", res);
cv::waitKey(0); cv::waitKey(0);
} }
} }
cv::destroyAllWindows();
delete yolov8; delete yolov8;
return 0; return 0;
} }

@ -182,8 +182,8 @@ YOLOv8::YOLOv8(const std::string& engine_file_path)
YOLOv8::~YOLOv8() YOLOv8::~YOLOv8()
{ {
this->engine->destroy();
this->context->destroy(); this->context->destroy();
this->engine->destroy();
this->runtime->destroy(); this->runtime->destroy();
cudaStreamDestroy(this->stream); cudaStreamDestroy(this->stream);
for (auto& ptr : this->buffs) for (auto& ptr : this->buffs)
@ -193,7 +193,7 @@ YOLOv8::~YOLOv8()
for (auto& ptr : this->outputs) for (auto& ptr : this->outputs)
{ {
CHECK(cudaFree(ptr)); CHECK(cudaFreeHost(ptr));
} }
} }
@ -353,13 +353,13 @@ void YOLOv8::postprocess(std::vector<Object>& objs)
} }
} }
static void draw_objects(const cv::Mat& image, const std::vector<Object>& objs) static void draw_objects(const cv::Mat& image, cv::Mat& res, const std::vector<Object>& objs)
{ {
cv::Mat img = image.clone(); res = image.clone();
for (auto& obj : objs) for (auto& obj : objs)
{ {
cv::Scalar color = cv::Scalar(COLORS[obj.label][0], COLORS[obj.label][1], COLORS[obj.label][2]); 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]; char text[256];
sprintf(text, "%s %.1f%%", CLASS_NAMES[obj.label], obj.prob * 100); 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<Object>& objs)
int x = (int)obj.rect.x; int x = (int)obj.rect.x;
int y = (int)obj.rect.y + 1; int y = (int)obj.rect.y + 1;
if (y > img.rows) if (y > res.rows)
y = img.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::FONT_HERSHEY_SIMPLEX, 0.4, TXT_COLOR, 1);
} }
cv::imshow("results", img);
} }

Loading…
Cancel
Save