model.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #pragma once
  15. #include "ultra_infer/ultra_infer_model.h"
  16. #include "ultra_infer/vision/common/processors/transform.h"
  17. #include "ultra_infer/vision/common/result.h"
  18. #include "ultra_infer/vision/tracking/pptracking/tracker.h"
  19. #include <map>
  20. namespace ultra_infer {
  21. namespace vision {
  22. namespace tracking {
  23. struct TrailRecorder {
  24. std::map<int, std::vector<std::array<int, 2>>> records;
  25. void Add(int id, const std::array<int, 2> &record);
  26. };
  27. inline void TrailRecorder::Add(int id, const std::array<int, 2> &record) {
  28. auto iter = records.find(id);
  29. if (iter != records.end()) {
  30. auto trail = records[id];
  31. trail.push_back(record);
  32. records[id] = trail;
  33. } else {
  34. records[id] = {record};
  35. }
  36. }
  37. class ULTRAINFER_DECL PPTracking : public UltraInferModel {
  38. public:
  39. /** \brief Set path of model file and configuration file, and the
  40. * configuration of runtime
  41. *
  42. * \param[in] model_file Path of model file, e.g pptracking/model.pdmodel
  43. * \param[in] params_file Path of parameter file, e.g
  44. * pptracking/model.pdiparams, if the model format is ONNX, this parameter
  45. * will be ignored \param[in] config_file Path of configuration file for
  46. * deployment, e.g pptracking/infer_cfg.yml \param[in] custom_option
  47. * RuntimeOption for inference, the default will use cpu, and choose the
  48. * backend defined in `valid_cpu_backends` \param[in] model_format Model
  49. * format of the loaded model, default is Paddle format
  50. */
  51. PPTracking(const std::string &model_file, const std::string &params_file,
  52. const std::string &config_file,
  53. const RuntimeOption &custom_option = RuntimeOption(),
  54. const ModelFormat &model_format = ModelFormat::PADDLE);
  55. /// Get model's name
  56. std::string ModelName() const override { return "pptracking"; }
  57. /** \brief Predict the detection result for an input image(consecutive)
  58. *
  59. * \param[in] im The input image data which is consecutive frame, comes from
  60. * imread() or videoCapture.read() \param[in] result The output tracking
  61. * result will be written to this structure \return true if the prediction
  62. * succeeded, otherwise false
  63. */
  64. virtual bool Predict(cv::Mat *img, MOTResult *result);
  65. /** \brief bind tracking trail struct
  66. *
  67. * \param[in] recorder The MOT trail will record the trail of object
  68. */
  69. void BindRecorder(TrailRecorder *recorder);
  70. /** \brief cancel binding and clear trail information
  71. */
  72. void UnbindRecorder();
  73. private:
  74. bool BuildPreprocessPipelineFromConfig();
  75. bool Initialize();
  76. bool Preprocess(Mat *img, std::vector<FDTensor> *outputs);
  77. bool Postprocess(std::vector<FDTensor> &infer_result, MOTResult *result);
  78. std::vector<std::shared_ptr<Processor>> processors_;
  79. std::string config_file_;
  80. float draw_threshold_;
  81. float conf_thresh_;
  82. float tracked_thresh_;
  83. float min_box_area_;
  84. bool is_record_trail_ = false;
  85. std::unique_ptr<JDETracker> jdeTracker_;
  86. TrailRecorder *recorder_ = nullptr;
  87. };
  88. } // namespace tracking
  89. } // namespace vision
  90. } // namespace ultra_infer