yolo_detector.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <opencv2/opencv.hpp>
  3. #include <string>
  4. #include <vector>
  5. #include "rknn_api.h"
  6. struct Detection {
  7. cv::Rect bbox;
  8. float confidence;
  9. int class_id;
  10. std::string class_name;
  11. };
  12. class YoloDetector {
  13. public:
  14. YoloDetector() = default;
  15. ~YoloDetector();
  16. // core_mask: 0=自动; 1=Core0; 2=Core1; 4=Core2; 7=0/1/2
  17. bool load(const std::string& model_path, int input_size,
  18. float conf_thresh, float nms_thresh, int core_mask = 0);
  19. std::vector<Detection> detect(const float* rgb_data, int img_w, int img_h,
  20. float scale, int pad_x, int pad_y);
  21. bool isLoaded() const { return loaded_; }
  22. int inputSize() const { return input_size_; }
  23. private:
  24. rknn_context ctx_ = 0;
  25. bool loaded_ = false;
  26. int input_size_ = 640;
  27. float conf_thresh_ = 0.35f;
  28. float nms_thresh_ = 0.45f;
  29. uint32_t n_input_ = 0;
  30. uint32_t n_output_ = 0;
  31. int frame_count_ = 0;
  32. std::vector<rknn_tensor_attr> input_attrs_;
  33. std::vector<rknn_tensor_attr> output_attrs_;
  34. static void nms(std::vector<Detection>& dets, float threshold);
  35. };