picodet.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // reference from https://github.com/RangiLyu/nanodet/tree/main/demo_ncnn
  15. #ifndef PICODET_H
  16. #define PICODET_H
  17. #include <net.h>
  18. #include <opencv2/core/core.hpp>
  19. typedef struct NonPostProcessHeadInfo {
  20. std::string cls_layer;
  21. std::string dis_layer;
  22. int stride;
  23. } NonPostProcessHeadInfo;
  24. typedef struct BoxInfo {
  25. float x1;
  26. float y1;
  27. float x2;
  28. float y2;
  29. float score;
  30. int label;
  31. } BoxInfo;
  32. class PicoDet {
  33. public:
  34. PicoDet(const char *param, const char *bin, int input_width, int input_hight,
  35. bool useGPU, float score_threshold_, float nms_threshold_);
  36. ~PicoDet();
  37. static PicoDet *detector;
  38. ncnn::Net *Net;
  39. static bool hasGPU;
  40. int detect(cv::Mat image, std::vector<BoxInfo> &result_list,
  41. bool has_postprocess);
  42. private:
  43. void preprocess(cv::Mat &image, ncnn::Mat &in);
  44. void decode_infer(ncnn::Mat &cls_pred, ncnn::Mat &dis_pred, int stride,
  45. float threshold,
  46. std::vector<std::vector<BoxInfo>> &results);
  47. BoxInfo disPred2Bbox(const float *&dfl_det, int label, float score, int x,
  48. int y, int stride);
  49. static void nms(std::vector<BoxInfo> &result, float nms_threshold);
  50. void nms_boxes(ncnn::Mat &cls_pred, ncnn::Mat &dis_pred,
  51. float score_threshold,
  52. std::vector<std::vector<BoxInfo>> &result_list);
  53. int image_w;
  54. int image_h;
  55. int in_w = 320;
  56. int in_h = 320;
  57. int num_class = 80;
  58. int reg_max = 7;
  59. float score_threshold;
  60. float nms_threshold;
  61. std::vector<float> bbox_output_data_;
  62. std::vector<float> class_output_data_;
  63. std::vector<std::string> nms_heads_info{"tmp_16", "concat_4.tmp_0"};
  64. // If not export post-process, will use non_postprocess_heads_info
  65. std::vector<NonPostProcessHeadInfo> non_postprocess_heads_info{
  66. // cls_pred|dis_pred|stride
  67. {"transpose_0.tmp_0", "transpose_1.tmp_0", 8},
  68. {"transpose_2.tmp_0", "transpose_3.tmp_0", 16},
  69. {"transpose_4.tmp_0", "transpose_5.tmp_0", 32},
  70. {"transpose_6.tmp_0", "transpose_7.tmp_0", 64},
  71. };
  72. };
  73. #endif