nndeploy C++ API  0.2.0
nndeploy C++ API
drawbox.h
Go to the documentation of this file.
1 #ifndef _NNDEPLOY_OCR_DETECTOR_DRAWBOX_H_
2 #define _NNDEPLOY_OCR_DETECTOR_DRAWBOX_H_
3 
6 #include "nndeploy/dag/node.h"
9 #include "nndeploy/ocr/result.h"
11 
12 namespace nndeploy {
13 namespace ocr {
14 
16  public:
17  DrawDetectorBox(const std::string &name) : Node(name) {
18  key_ = "nndeploy::ocr::DrawDetectorBox";
19  desc_ =
20  "Draw ocr boxes on input cv::Mat image based on detection "
21  "results[cv::Mat->cv::Mat]";
22  this->setInputTypeInfo<cv::Mat>();
23  this->setInputTypeInfo<OCRResult>();
24  this->setOutputTypeInfo<cv::Mat>();
25  }
26  DrawDetectorBox(const std::string &name, std::vector<dag::Edge *> inputs,
27  std::vector<dag::Edge *> outputs)
28  : Node(name, inputs, outputs) {
29  key_ = "nndeploy::ocr::DrawDetectorBox";
30  desc_ =
31  "Draw ocr boxes on input cv::Mat image based on detection "
32  "results[cv::Mat->cv::Mat]";
33  this->setInputTypeInfo<cv::Mat>();
34  this->setInputTypeInfo<OCRResult>();
35  this->setOutputTypeInfo<cv::Mat>();
36  }
37  virtual ~DrawDetectorBox() {}
39  virtual base::Status run() {
40  cv::Mat *input_mat = inputs_[0]->get<cv::Mat>(this);
41  if (input_mat == nullptr) {
42  NNDEPLOY_LOGE("input_mat is nullptr\n");
44  }
45  // NNDEPLOY_LOGE("input_mat: %p\n", input_mat);
46  ocr::OCRResult *result = (ocr::OCRResult *)inputs_[1]->get<OCRResult>(this);
47  if (result == nullptr) {
48  NNDEPLOY_LOGE("result is nullptr\n");
50  }
51 
52  // NNDEPLOY_LOGE("result: %p\n", result);
53  int origin_w = int(input_mat->cols);
54  int origin_h = int(input_mat->rows);
55  const int CNUM = 80;
56  cv::RNG rng(0xFFFFFFFF);
57  cv::Scalar_<int> randColor[CNUM];
58  cv::Mat *output_mat = new cv::Mat();
59  input_mat->copyTo(*output_mat);
60  std::vector<std::vector<std::vector<int>>> boxes_recovered;
61 
62  for (auto &arr : result->boxes_) {
63  std::vector<std::vector<int>> one_box;
64  for (int i = 0; i < 8; i += 2) {
65  one_box.push_back({arr[i], arr[i + 1]}); // 每两个数就是一个点 (x,y)
66  }
67  boxes_recovered.push_back(one_box);
68  }
69 
70  boxes_recovered = util_post_processor_.FilterTagDetRes(
71  boxes_recovered,
72  {origin_w, origin_h, result->detector_resized_w,
73  result->detector_resized_h}); // 如果需要原图缩放信息可以传入
74 
75  // 5. 转成 DetectBBoxResult
76  for (int i = 0; i < boxes_recovered.size(); i++) {
77  std::array<int, 8> new_box;
78  int k = 0;
79  for (auto &vec : boxes_recovered[i]) {
80  for (auto &e : vec) {
81  new_box[k++] = e;
82  }
83  }
84  cv::Point rook_points[4];
85  for (int m = 0; m < 4; m++) {
86  rook_points[m] = cv::Point(int(new_box[m * 2]), // x 坐标
87  int(new_box[m * 2 + 1]) // y 坐标
88  );
89  }
90 
91  const cv::Point *ppt[1] = {rook_points};
92  int npt[] = {4};
93  cv::polylines(*output_mat, ppt, npt, 1, true, cv::Scalar(0, 255, 0), 2);
94  }
95  outputs_[0]->set(output_mat, false);
96  return base::kStatusCodeOk;
97  }
98 };
99 
100 } // namespace ocr
101 } // namespace nndeploy
102 
103 #endif
Node base class.
Definition: node.h:171
DrawDetectorBox(const std::string &name)
Definition: drawbox.h:17
virtual base::Status run()
Run node (pure virtual function)
Definition: drawbox.h:39
PostProcessor util_post_processor_
Definition: drawbox.h:38
DrawDetectorBox(const std::string &name, std::vector< dag::Edge * > inputs, std::vector< dag::Edge * > outputs)
Definition: drawbox.h:26
OCR识别结果类
Definition: result.h:29
std::vector< std::array< int, 8 > > boxes_
Definition: result.h:32
std::vector< std::vector< std::vector< int > > > FilterTagDetRes(std::vector< std::vector< std::vector< int >>> boxes, const std::array< int, 4 > &det_img_info)
#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