picodet_openvino.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2022 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_openvino
  15. #ifndef _PICODET_OPENVINO_H_
  16. #define _PICODET_OPENVINO_H_
  17. #include <inference_engine.hpp>
  18. #include <opencv2/core.hpp>
  19. #include <string>
  20. #define image_size 416
  21. typedef struct HeadInfo {
  22. std::string cls_layer;
  23. std::string dis_layer;
  24. int stride;
  25. } HeadInfo;
  26. typedef struct BoxInfo {
  27. float x1;
  28. float y1;
  29. float x2;
  30. float y2;
  31. float score;
  32. int label;
  33. } BoxInfo;
  34. class PicoDet {
  35. public:
  36. PicoDet(const char *param);
  37. ~PicoDet();
  38. InferenceEngine::ExecutableNetwork network_;
  39. InferenceEngine::InferRequest infer_request_;
  40. std::vector<HeadInfo> heads_info_{
  41. // cls_pred|dis_pred|stride
  42. {"transpose_0.tmp_0", "transpose_1.tmp_0", 8},
  43. {"transpose_2.tmp_0", "transpose_3.tmp_0", 16},
  44. {"transpose_4.tmp_0", "transpose_5.tmp_0", 32},
  45. {"transpose_6.tmp_0", "transpose_7.tmp_0", 64},
  46. };
  47. std::vector<BoxInfo> detect(cv::Mat image, float score_threshold,
  48. float nms_threshold);
  49. private:
  50. void preprocess(cv::Mat &image, InferenceEngine::Blob::Ptr &blob);
  51. void decode_infer(const float *&cls_pred, const float *&dis_pred, int stride,
  52. float threshold,
  53. std::vector<std::vector<BoxInfo>> &results);
  54. BoxInfo disPred2Bbox(const float *&dfl_det, int label, float score, int x,
  55. int y, int stride);
  56. static void nms(std::vector<BoxInfo> &result, float nms_threshold);
  57. std::string input_name_;
  58. int input_size_ = image_size;
  59. int num_class_ = 80;
  60. int reg_max_ = 7;
  61. };
  62. #endif