pipeline.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef DEPLOY_PPTRACKING_CPP_INCLUDE_PIPELINE_H_
  15. #define DEPLOY_PPTRACKING_CPP_INCLUDE_PIPELINE_H_
  16. #include <glog/logging.h>
  17. #include <math.h>
  18. #include <sys/types.h>
  19. #include <algorithm>
  20. #include <iostream>
  21. #include <numeric>
  22. #include <string>
  23. #include <vector>
  24. #ifdef _WIN32
  25. #include <direct.h>
  26. #include <io.h>
  27. #elif LINUX
  28. #include <stdarg.h>
  29. #include <sys/stat.h>
  30. #endif
  31. #include "include/jde_predictor.h"
  32. #include "include/sde_predictor.h"
  33. namespace PaddleDetection {
  34. class Pipeline {
  35. public:
  36. explicit Pipeline(const std::string& device,
  37. const double threshold,
  38. const std::string& output_dir,
  39. const std::string& run_mode = "paddle",
  40. const int gpu_id = 0,
  41. const bool use_mkldnn = false,
  42. const int cpu_threads = 1,
  43. const bool trt_calib_mode = false,
  44. const bool do_entrance_counting = false,
  45. const bool save_result = false,
  46. const std::string& scene = "pedestrian",
  47. const bool tiny_obj = false,
  48. const bool is_mtmct = false,
  49. const int secs_interval = 10,
  50. const std::string track_model_dir = "",
  51. const std::string det_model_dir = "",
  52. const std::string reid_model_dir = "") {
  53. std::vector<std::string> input;
  54. this->input_ = input;
  55. this->device_ = device;
  56. this->threshold_ = threshold;
  57. this->output_dir_ = output_dir;
  58. this->run_mode_ = run_mode;
  59. this->gpu_id_ = gpu_id;
  60. this->use_mkldnn_ = use_mkldnn;
  61. this->cpu_threads_ = cpu_threads;
  62. this->trt_calib_mode_ = trt_calib_mode;
  63. this->do_entrance_counting_ = do_entrance_counting;
  64. this->secs_interval_ = secs_interval_;
  65. this->save_result_ = save_result;
  66. SelectModel(scene,
  67. tiny_obj,
  68. is_mtmct,
  69. track_model_dir,
  70. det_model_dir,
  71. reid_model_dir);
  72. InitPredictor();
  73. }
  74. // Set input, it must execute before Run()
  75. void SetInput(const std::string& input_video);
  76. void ClearInput();
  77. // Run pipeline in video
  78. void Run();
  79. void PredictMOT(const std::string& video_path);
  80. void PredictMTMCT(const std::vector<std::string> video_inputs);
  81. // Run pipeline in stream
  82. void RunMOTStream(const cv::Mat img,
  83. const int frame_id,
  84. const int video_fps,
  85. const Rect entrance,
  86. cv::Mat out_img,
  87. std::vector<std::string>* records,
  88. std::set<int>* count_set,
  89. std::set<int>* interval_count_set,
  90. std::vector<int>* in_count_list,
  91. std::vector<int>* out_count_list,
  92. std::map<int, std::vector<float>>* prev_center,
  93. std::vector<std::string>* flow_records);
  94. void RunMTMCTStream(const std::vector<cv::Mat> imgs,
  95. std::vector<std::string>* records);
  96. void PrintBenchmarkLog(const std::vector<double> det_time, const int img_num);
  97. private:
  98. // Select model according to scenes, it must execute before Run()
  99. void SelectModel(const std::string& scene = "pedestrian",
  100. const bool tiny_obj = false,
  101. const bool is_mtmct = false,
  102. const std::string track_model_dir = "",
  103. const std::string det_model_dir = "",
  104. const std::string reid_model_dir = "");
  105. void InitPredictor();
  106. std::shared_ptr<PaddleDetection::JDEPredictor> jde_sct_;
  107. std::shared_ptr<PaddleDetection::SDEPredictor> sde_sct_;
  108. std::vector<std::string> input_;
  109. std::vector<cv::Mat> stream_;
  110. std::string device_;
  111. double threshold_;
  112. std::string output_dir_;
  113. std::string track_model_dir_;
  114. std::string det_model_dir_;
  115. std::string reid_model_dir_;
  116. std::string run_mode_ = "paddle";
  117. int gpu_id_ = 0;
  118. bool use_mkldnn_ = false;
  119. int cpu_threads_ = 1;
  120. bool trt_calib_mode_ = false;
  121. bool do_entrance_counting_ = false;
  122. bool save_result_ = false;
  123. int secs_interval_ = 10;
  124. };
  125. } // namespace PaddleDetection
  126. #endif // DEPLOY_PPTRACKING_CPP_INCLUDE_PIPELINE_H_