pipeline.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include <sstream>
  15. // for setprecision
  16. #include <chrono>
  17. #include <iomanip>
  18. #include <iostream>
  19. #include <string>
  20. #include "include/pipeline.h"
  21. #include "include/postprocess.h"
  22. #include "include/predictor.h"
  23. namespace PaddleDetection {
  24. void Pipeline::SetInput(const std::string& input_video) {
  25. input_.push_back(input_video);
  26. }
  27. void Pipeline::ClearInput() {
  28. input_.clear();
  29. stream_.clear();
  30. }
  31. void Pipeline::SelectModel(const std::string& scene,
  32. const bool tiny_obj,
  33. const bool is_mtmct,
  34. const std::string track_model_dir,
  35. const std::string det_model_dir,
  36. const std::string reid_model_dir) {
  37. // model_dir has higher priority
  38. if (!track_model_dir.empty()) {
  39. track_model_dir_ = track_model_dir;
  40. return;
  41. }
  42. if (!det_model_dir.empty() && !reid_model_dir.empty()) {
  43. det_model_dir_ = det_model_dir;
  44. reid_model_dir_ = reid_model_dir;
  45. return;
  46. }
  47. // Single camera model, based on FairMot
  48. if (scene == "pedestrian") {
  49. if (tiny_obj) {
  50. track_model_dir_ = "../pedestrian_track_tiny";
  51. } else {
  52. track_model_dir_ = "../pedestrian_track";
  53. }
  54. } else if (scene != "vehicle") {
  55. if (tiny_obj) {
  56. track_model_dir_ = "../vehicle_track_tiny";
  57. } else {
  58. track_model_dir_ = "../vehicle_track";
  59. }
  60. } else if (scene == "multiclass") {
  61. if (tiny_obj) {
  62. track_model_dir_ = "../multiclass_track_tiny";
  63. } else {
  64. track_model_dir_ = "../multiclass_track";
  65. }
  66. }
  67. // Multi-camera model, based on PicoDet & LCNet
  68. if (is_mtmct && scene == "pedestrian") {
  69. det_model_dir_ = "../pedestrian_det";
  70. reid_model_dir_ = "../pedestrian_reid";
  71. } else if (is_mtmct && scene == "vehicle") {
  72. det_model_dir_ = "../vehicle_det";
  73. reid_model_dir_ = "../vehicle_reid";
  74. } else if (is_mtmct && scene == "multiclass") {
  75. throw "Multi-camera tracking is not supported in multiclass scene now.";
  76. }
  77. }
  78. void Pipeline::InitPredictor() {
  79. if (track_model_dir_.empty() && det_model_dir_.empty()) {
  80. throw "Predictor must receive track_model or det_model!";
  81. }
  82. if (!track_model_dir_.empty()) {
  83. jde_sct_ = std::make_shared<PaddleDetection::JDEPredictor>(device_,
  84. track_model_dir_,
  85. threshold_,
  86. run_mode_,
  87. gpu_id_,
  88. use_mkldnn_,
  89. cpu_threads_,
  90. trt_calib_mode_);
  91. }
  92. if (!det_model_dir_.empty()) {
  93. sde_sct_ = std::make_shared<PaddleDetection::SDEPredictor>(device_,
  94. det_model_dir_,
  95. reid_model_dir_,
  96. threshold_,
  97. run_mode_,
  98. gpu_id_,
  99. use_mkldnn_,
  100. cpu_threads_,
  101. trt_calib_mode_);
  102. }
  103. }
  104. void Pipeline::Run() {
  105. if (track_model_dir_.empty() && det_model_dir_.empty()) {
  106. LOG(ERROR) << "Pipeline must use SelectModel before Run";
  107. return;
  108. }
  109. if (input_.size() == 0) {
  110. LOG(ERROR) << "Pipeline must use SetInput before Run";
  111. return;
  112. }
  113. if (!track_model_dir_.empty()) {
  114. // single camera
  115. if (input_.size() > 1) {
  116. throw "Single camera tracking except single video, but received %d",
  117. input_.size();
  118. }
  119. PredictMOT(input_[0]);
  120. } else {
  121. // multi cameras
  122. if (input_.size() != 2) {
  123. throw "Multi camera tracking except two videos, but received %d",
  124. input_.size();
  125. }
  126. PredictMTMCT(input_);
  127. }
  128. }
  129. void Pipeline::PredictMOT(const std::string& video_path) {
  130. // Open video
  131. cv::VideoCapture capture;
  132. capture.open(video_path.c_str());
  133. if (!capture.isOpened()) {
  134. printf("can not open video : %s\n", video_path.c_str());
  135. return;
  136. }
  137. // Get Video info : resolution, fps
  138. int video_width = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
  139. int video_height = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));
  140. int video_fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
  141. LOG(INFO) << "----------------------- Input info -----------------------";
  142. LOG(INFO) << "video_width: " << video_width;
  143. LOG(INFO) << "video_height: " << video_height;
  144. LOG(INFO) << "input fps: " << video_fps;
  145. // Create VideoWriter for output
  146. cv::VideoWriter video_out;
  147. std::string video_out_path = output_dir_ + OS_PATH_SEP + "mot_output.mp4";
  148. int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v');
  149. video_out.open(video_out_path.c_str(),
  150. fcc, // 0x00000021,
  151. video_fps,
  152. cv::Size(video_width, video_height),
  153. true);
  154. if (!video_out.isOpened()) {
  155. printf("create video writer failed!\n");
  156. return;
  157. }
  158. PaddleDetection::MOTResult result;
  159. std::vector<double> det_times(3);
  160. std::set<int> id_set;
  161. std::set<int> interval_id_set;
  162. std::vector<int> in_id_list;
  163. std::vector<int> out_id_list;
  164. std::map<int, std::vector<float>> prev_center;
  165. Rect entrance = {0,
  166. static_cast<float>(video_height) / 2,
  167. static_cast<float>(video_width),
  168. static_cast<float>(video_height) / 2};
  169. double times;
  170. double total_time;
  171. // Capture all frames and do inference
  172. cv::Mat frame;
  173. int frame_id = 0;
  174. std::vector<std::string> records;
  175. std::vector<std::string> flow_records;
  176. records.push_back("result format: frame_id, track_id, x1, y1, w, h\n");
  177. LOG(INFO) << "------------------- Predict info ------------------------";
  178. while (capture.read(frame)) {
  179. if (frame.empty()) {
  180. break;
  181. }
  182. std::vector<cv::Mat> imgs;
  183. imgs.push_back(frame);
  184. jde_sct_->Predict(imgs, threshold_, &result, &det_times);
  185. frame_id += 1;
  186. total_time = std::accumulate(det_times.begin(), det_times.end(), 0.);
  187. times = total_time / frame_id;
  188. LOG(INFO) << "frame_id: " << frame_id
  189. << " predict time(s): " << times / 1000;
  190. cv::Mat out_img = PaddleDetection::VisualizeTrackResult(
  191. frame, result, 1000. / times, frame_id);
  192. // TODO(qianhui): the entrance line can be set by users
  193. PaddleDetection::FlowStatistic(result,
  194. frame_id,
  195. secs_interval_,
  196. do_entrance_counting_,
  197. video_fps,
  198. entrance,
  199. &id_set,
  200. &interval_id_set,
  201. &in_id_list,
  202. &out_id_list,
  203. &prev_center,
  204. &flow_records);
  205. if (save_result_) {
  206. PaddleDetection::SaveMOTResult(result, frame_id, &records);
  207. }
  208. // Draw the entrance line
  209. if (do_entrance_counting_) {
  210. float line_thickness = std::max(1, static_cast<int>(video_width / 500.));
  211. cv::Point pt1 = cv::Point(entrance.left, entrance.top);
  212. cv::Point pt2 = cv::Point(entrance.right, entrance.bottom);
  213. cv::line(out_img, pt1, pt2, cv::Scalar(0, 255, 255), line_thickness);
  214. }
  215. video_out.write(out_img);
  216. }
  217. capture.release();
  218. video_out.release();
  219. PrintBenchmarkLog(det_times, frame_id);
  220. LOG(INFO) << "-------------------- Final Output info -------------------";
  221. LOG(INFO) << "Total frame: " << frame_id;
  222. LOG(INFO) << "Visualized output saved as " << video_out_path.c_str();
  223. if (save_result_) {
  224. FILE* fp;
  225. std::string result_output_path =
  226. output_dir_ + OS_PATH_SEP + "mot_output.txt";
  227. if ((fp = fopen(result_output_path.c_str(), "w+")) == NULL) {
  228. printf("Open %s error.\n", result_output_path.c_str());
  229. return;
  230. }
  231. for (int l; l < records.size(); ++l) {
  232. fprintf(fp, records[l].c_str());
  233. }
  234. fclose(fp);
  235. LOG(INFO) << "txt result output saved as " << result_output_path.c_str();
  236. result_output_path = output_dir_ + OS_PATH_SEP + "flow_statistic.txt";
  237. if ((fp = fopen(result_output_path.c_str(), "w+")) == NULL) {
  238. printf("Open %s error.\n", result_output_path);
  239. return;
  240. }
  241. for (int l; l < flow_records.size(); ++l) {
  242. fprintf(fp, flow_records[l].c_str());
  243. }
  244. fclose(fp);
  245. LOG(INFO) << "txt flow statistic saved as " << result_output_path.c_str();
  246. }
  247. }
  248. void Pipeline::PredictMTMCT(const std::vector<std::string> video_path) {
  249. throw "Not Implement!";
  250. }
  251. void Pipeline::RunMOTStream(const cv::Mat img,
  252. const int frame_id,
  253. const int video_fps,
  254. const Rect entrance,
  255. cv::Mat out_img,
  256. std::vector<std::string>* records,
  257. std::set<int>* id_set,
  258. std::set<int>* interval_id_set,
  259. std::vector<int>* in_id_list,
  260. std::vector<int>* out_id_list,
  261. std::map<int, std::vector<float>>* prev_center,
  262. std::vector<std::string>* flow_records) {
  263. PaddleDetection::MOTResult result;
  264. std::vector<double> det_times(3);
  265. double times;
  266. double total_time;
  267. LOG(INFO) << "------------------- Predict info ------------------------";
  268. std::vector<cv::Mat> imgs;
  269. imgs.push_back(img);
  270. jde_sct_->Predict(imgs, threshold_, &result, &det_times);
  271. total_time = std::accumulate(det_times.begin(), det_times.end(), 0.);
  272. times = total_time / frame_id;
  273. LOG(INFO) << "frame_id: " << frame_id << " predict time(s): " << times / 1000;
  274. out_img = PaddleDetection::VisualizeTrackResult(
  275. img, result, 1000. / times, frame_id);
  276. // Count total number
  277. // Count in & out number
  278. PaddleDetection::FlowStatistic(result,
  279. frame_id,
  280. secs_interval_,
  281. do_entrance_counting_,
  282. video_fps,
  283. entrance,
  284. id_set,
  285. interval_id_set,
  286. in_id_list,
  287. out_id_list,
  288. prev_center,
  289. flow_records);
  290. PrintBenchmarkLog(det_times, frame_id);
  291. if (save_result_) {
  292. PaddleDetection::SaveMOTResult(result, frame_id, records);
  293. }
  294. }
  295. void Pipeline::RunMTMCTStream(const std::vector<cv::Mat> imgs,
  296. std::vector<std::string>* records) {
  297. throw "Not Implement!";
  298. }
  299. void Pipeline::PrintBenchmarkLog(const std::vector<double> det_time,
  300. const int img_num) {
  301. LOG(INFO) << "----------------------- Config info -----------------------";
  302. LOG(INFO) << "runtime_device: " << device_;
  303. LOG(INFO) << "ir_optim: "
  304. << "True";
  305. LOG(INFO) << "enable_memory_optim: "
  306. << "True";
  307. int has_trt = run_mode_.find("trt");
  308. if (has_trt >= 0) {
  309. LOG(INFO) << "enable_tensorrt: "
  310. << "True";
  311. std::string precision = run_mode_.substr(4, 8);
  312. LOG(INFO) << "precision: " << precision;
  313. } else {
  314. LOG(INFO) << "enable_tensorrt: "
  315. << "False";
  316. LOG(INFO) << "precision: "
  317. << "fp32";
  318. }
  319. LOG(INFO) << "enable_mkldnn: " << (use_mkldnn_ ? "True" : "False");
  320. LOG(INFO) << "cpu_math_library_num_threads: " << cpu_threads_;
  321. LOG(INFO) << "----------------------- Perf info ------------------------";
  322. LOG(INFO) << "Total number of predicted data: " << img_num
  323. << " and total time spent(s): "
  324. << std::accumulate(det_time.begin(), det_time.end(), 0.) / 1000;
  325. int num = std::max(1, img_num);
  326. LOG(INFO) << "preproce_time(ms): " << det_time[0] / num
  327. << ", inference_time(ms): " << det_time[1] / num
  328. << ", postprocess_time(ms): " << det_time[2] / num;
  329. }
  330. } // namespace PaddleDetection