nndeploy C++ API  0.2.0
nndeploy C++ API
drawbox.h
Go to the documentation of this file.
1 #ifndef _NNDEPLOY_DETECT_DRAWBOX_H_
2 #define _NNDEPLOY_DETECT_DRAWBOX_H_
3 
6 #include "nndeploy/dag/node.h"
10 
11 namespace nndeploy {
12 namespace detect {
13 
14 // class DrawBox : public dag::Node {
15 // public:
16 // DrawBox(const std::string &name,
17 // std::initializer_list<dag::Edge *> inputs,
18 // std::initializer_list<dag::Edge *> outputs);
19 // virtual ~DrawBox();
20 
21 // virtual base::Status run();
22 // };
23 
24 // class YoloMultiConvDrawBox : public dag::Node {
25 // public:
26 // YoloMultiConvDrawBox(const std::string &name,
27 // std::initializer_list<dag::Edge *> inputs,
28 // std::initializer_list<dag::Edge *> outputs);
29 // virtual ~YoloMultiConvDrawBox();
30 
31 // virtual base::Status run();
32 // };
33 
35  public:
36  DrawBox(const std::string &name) : Node(name) {
37  key_ = "nndeploy::detect::DrawBox";
38  desc_ =
39  "Draw detection boxes on input cv::Mat image based on detection "
40  "results[cv::Mat->cv::Mat]";
41  this->setInputTypeInfo<cv::Mat>();
42  this->setInputTypeInfo<DetectResult>();
43  this->setOutputTypeInfo<cv::Mat>();
44  }
45  DrawBox(const std::string &name, std::vector<dag::Edge *> inputs,
46  std::vector<dag::Edge *> outputs)
47  : Node(name, inputs, outputs) {
48  key_ = "nndeploy::detect::DrawBox";
49  desc_ =
50  "Draw detection boxes on input cv::Mat image based on detection "
51  "results[cv::Mat->cv::Mat]";
52  this->setInputTypeInfo<cv::Mat>();
53  this->setInputTypeInfo<DetectResult>();
54  this->setOutputTypeInfo<cv::Mat>();
55  }
56  virtual ~DrawBox() {}
57 
58  virtual base::Status run() {
59  cv::Mat *input_mat = inputs_[0]->get<cv::Mat>(this);
60  if (input_mat == nullptr) {
61  NNDEPLOY_LOGE("input_mat is nullptr\n");
63  }
64  // NNDEPLOY_LOGE("input_mat: %p\n", input_mat);
65  detect::DetectResult *result =
66  (detect::DetectResult *)inputs_[1]->get<DetectResult>(this);
67  if (result == nullptr) {
68  NNDEPLOY_LOGE("result is nullptr\n");
70  }
71  // NNDEPLOY_LOGE("result: %p\n", result);
72  float w_ratio = float(input_mat->cols);
73  float h_ratio = float(input_mat->rows);
74  const int CNUM = 80;
75  cv::RNG rng(0xFFFFFFFF);
76  cv::Scalar_<int> randColor[CNUM];
77  cv::Mat *output_mat = new cv::Mat();
78  input_mat->copyTo(*output_mat);
79  for (int i = 0; i < CNUM; i++)
80  rng.fill(randColor[i], cv::RNG::UNIFORM, 0, 256);
81  int i = -1;
82  for (auto bbox : result->bboxs_) {
83  std::array<float, 4> box;
84  box[0] = bbox.bbox_[0]; // 640.0;
85  box[2] = bbox.bbox_[2]; // 640.0;
86  box[1] = bbox.bbox_[1]; // 640.0;
87  box[3] = bbox.bbox_[3]; // 640.0;
88  box[0] *= w_ratio;
89  box[2] *= w_ratio;
90  box[1] *= h_ratio;
91  box[3] *= h_ratio;
92  int width = box[2] - box[0];
93  int height = box[3] - box[1];
94  int id = bbox.label_id_;
95  // NNDEPLOY_LOGE("box[0]:%f, box[1]:%f, width :%d, height :%d\n", box[0],
96  // box[1], width, height);
97  cv::Point p = cv::Point(box[0], box[1]);
98  cv::Rect rect = cv::Rect(box[0], box[1], width, height);
99  cv::rectangle(*output_mat, rect, randColor[id], 2);
100  std::string text =
101  " ID:" + std::to_string(id) + " score:" + std::to_string(bbox.score_);
102  cv::putText(*output_mat, text, p, cv::FONT_HERSHEY_PLAIN, 1,
103  randColor[id]);
104  }
105  outputs_[0]->set(output_mat, false);
106  return base::kStatusCodeOk;
107  }
108 };
109 
111  public:
112  YoloMultiConvDrawBox(const std::string &name) : Node(name) {
113  key_ = "nndeploy::detect::YoloMultiConvDrawBox";
114  desc_ =
115  "Draw detection boxes on input cv::Mat image based on detection "
116  "results[cv::Mat->cv::Mat]";
117  this->setInputTypeInfo<cv::Mat>();
118  this->setInputTypeInfo<DetectResult>();
119  this->setOutputTypeInfo<cv::Mat>();
120  }
121  YoloMultiConvDrawBox(const std::string &name, std::vector<dag::Edge *> inputs,
122  std::vector<dag::Edge *> outputs)
123  : Node(name, inputs, outputs) {
124  key_ = "nndeploy::detect::YoloMultiConvDrawBox";
125  desc_ =
126  "Draw detection boxes on input cv::Mat image based on detection "
127  "results[cv::Mat->cv::Mat]";
128  this->setInputTypeInfo<cv::Mat>();
129  this->setInputTypeInfo<DetectResult>();
130  this->setOutputTypeInfo<cv::Mat>();
131  }
133 
134  virtual base::Status run() {
135  cv::Mat *input_mat = inputs_[0]->getCvMat(this);
136  detect::DetectResult *result =
137  (detect::DetectResult *)inputs_[1]->getParam(this);
138  float w_ratio = float(input_mat->cols);
139  float h_ratio = float(input_mat->rows);
140  const int CNUM = 80;
141  cv::RNG rng(0xFFFFFFFF);
142  cv::Scalar_<int> randColor[CNUM];
143  for (int i = 0; i < CNUM; i++)
144  rng.fill(randColor[i], cv::RNG::UNIFORM, 0, 256);
145  int i = -1;
146  for (auto bbox : result->bboxs_) {
147  std::array<float, 4> box;
148  box[0] = bbox.bbox_[0]; // 640.0;
149  box[2] = bbox.bbox_[2]; // 640.0;
150  box[1] = bbox.bbox_[1]; // 640.0;
151  box[3] = bbox.bbox_[3]; // 640.0;
152  int width = box[2] - box[0];
153  int height = box[3] - box[1];
154  int id = bbox.label_id_;
155  NNDEPLOY_LOGE("box[0]:%f, box[1]:%f, width :%d, height :%d\n", box[0],
156  box[1], width, height);
157  cv::Point p = cv::Point(box[0], box[1]);
158  cv::Rect rect = cv::Rect(box[0], box[1], width, height);
159  cv::rectangle(*input_mat, rect, randColor[id], 10);
160  std::string text = " ID:" + std::to_string(id);
161  cv::putText(*input_mat, text, p, cv::FONT_HERSHEY_SIMPLEX, 1.0,
162  randColor[id], 4);
163  }
164  // cv::Mat *output_mat = new cv::Mat(*input_mat);
165  cv::Mat *output_mat = new cv::Mat();
166  input_mat->copyTo(*output_mat);
167  outputs_[0]->set(output_mat, false);
168  return base::kStatusCodeOk;
169  }
170 };
171 
172 } // namespace detect
173 } // namespace nndeploy
174 
175 #endif
Node base class.
Definition: node.h:171
std::vector< DetectBBoxResult > bboxs_
Definition: result.h:46
DrawBox(const std::string &name)
Definition: drawbox.h:36
virtual base::Status run()
Run node (pure virtual function)
Definition: drawbox.h:58
DrawBox(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
Definition: drawbox.h:45
virtual base::Status run()
Run node (pure virtual function)
Definition: drawbox.h:134
YoloMultiConvDrawBox(const std::string &name)
Definition: drawbox.h:112
YoloMultiConvDrawBox(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
Definition: drawbox.h:121
#define NNDEPLOY_LOGE(fmt,...)
Definition: log.h:59
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
@ kStatusCodeOk
Definition: status.h:13
@ kStatusCodeErrorInvalidParam
Definition: status.h:21