main.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <glog/logging.h>
  15. #include <math.h>
  16. #include <sys/types.h>
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <numeric>
  20. #include <string>
  21. #include <vector>
  22. #ifdef _WIN32
  23. #include <direct.h>
  24. #include <io.h>
  25. #else
  26. #include <stdarg.h>
  27. #include <sys/stat.h>
  28. #endif
  29. #include <gflags/gflags.h>
  30. #include "include/pipeline.h"
  31. DEFINE_string(video_file, "", "Path of input video.");
  32. DEFINE_string(video_other_file,
  33. "",
  34. "Path of other input video used for MTMCT.");
  35. DEFINE_string(device,
  36. "CPU",
  37. "Choose the device you want to run, it can be: CPU/GPU/XPU, "
  38. "default is CPU.");
  39. DEFINE_double(threshold, 0.5, "Threshold of score.");
  40. DEFINE_string(output_dir, "output", "Directory of output visualization files.");
  41. DEFINE_string(run_mode,
  42. "paddle",
  43. "Mode of running(paddle/trt_fp32/trt_fp16/trt_int8)");
  44. DEFINE_int32(gpu_id, 0, "Device id of GPU to execute");
  45. DEFINE_bool(use_mkldnn, false, "Whether use mkldnn with CPU");
  46. DEFINE_int32(cpu_threads, 1, "Num of threads with CPU");
  47. DEFINE_bool(trt_calib_mode,
  48. false,
  49. "If the model is produced by TRT offline quantitative calibration, "
  50. "trt_calib_mode need to set True");
  51. DEFINE_bool(tiny_obj, false, "Whether tracking tiny object");
  52. DEFINE_bool(do_entrance_counting,
  53. false,
  54. "Whether counting the numbers of identifiers entering "
  55. "or getting out from the entrance.");
  56. DEFINE_int32(secs_interval, 10, "The seconds interval to count after tracking");
  57. DEFINE_bool(save_result, false, "Whether saving result after tracking");
  58. DEFINE_string(
  59. scene,
  60. "",
  61. "scene of tracking system, it can be : pedestrian/vehicle/multiclass");
  62. DEFINE_bool(is_mtmct, false, "Whether use multi-target multi-camera tracking");
  63. DEFINE_string(track_model_dir, "", "Path of tracking model");
  64. DEFINE_string(det_model_dir, "", "Path of detection model");
  65. DEFINE_string(reid_model_dir, "", "Path of reid model");
  66. static std::string DirName(const std::string& filepath) {
  67. auto pos = filepath.rfind(OS_PATH_SEP);
  68. if (pos == std::string::npos) {
  69. return "";
  70. }
  71. return filepath.substr(0, pos);
  72. }
  73. static bool PathExists(const std::string& path) {
  74. #ifdef _WIN32
  75. struct _stat buffer;
  76. return (_stat(path.c_str(), &buffer) == 0);
  77. #else
  78. struct stat buffer;
  79. return (stat(path.c_str(), &buffer) == 0);
  80. #endif // !_WIN32
  81. }
  82. static void MkDir(const std::string& path) {
  83. if (PathExists(path)) return;
  84. int ret = 0;
  85. #ifdef _WIN32
  86. ret = _mkdir(path.c_str());
  87. #else
  88. ret = mkdir(path.c_str(), 0755);
  89. #endif // !_WIN32
  90. if (ret != 0) {
  91. std::string path_error(path);
  92. path_error += " mkdir failed!";
  93. throw std::runtime_error(path_error);
  94. }
  95. }
  96. static void MkDirs(const std::string& path) {
  97. if (path.empty()) return;
  98. if (PathExists(path)) return;
  99. MkDirs(DirName(path));
  100. MkDir(path);
  101. }
  102. int main(int argc, char** argv) {
  103. // Parsing command-line
  104. google::ParseCommandLineFlags(&argc, &argv, true);
  105. bool has_model_dir =
  106. !(FLAGS_track_model_dir.empty() && FLAGS_det_model_dir.empty() &&
  107. FLAGS_reid_model_dir.empty());
  108. if (FLAGS_video_file.empty() || (FLAGS_scene.empty() && !has_model_dir)) {
  109. LOG(ERROR) << "Usage: \n"
  110. << "1. ./main -video_file=/PATH/TO/INPUT/IMAGE/ "
  111. << "-scene=pedestrian/vehicle/multiclass\n"
  112. << "2. ./main -video_file=/PATH/TO/INPUT/IMAGE/ "
  113. << "-track_model_dir=/PATH/TO/MODEL_DIR" << std::endl;
  114. return -1;
  115. }
  116. if (!(FLAGS_run_mode == "paddle" || FLAGS_run_mode == "trt_fp32" ||
  117. FLAGS_run_mode == "trt_fp16" || FLAGS_run_mode == "trt_int8")) {
  118. LOG(ERROR)
  119. << "run_mode should be 'paddle', 'trt_fp32', 'trt_fp16' or 'trt_int8'.";
  120. return -1;
  121. }
  122. transform(FLAGS_device.begin(),
  123. FLAGS_device.end(),
  124. FLAGS_device.begin(),
  125. ::toupper);
  126. if (!(FLAGS_device == "CPU" || FLAGS_device == "GPU" ||
  127. FLAGS_device == "XPU")) {
  128. LOG(ERROR) << "device should be 'CPU', 'GPU' or 'XPU'.";
  129. return -1;
  130. }
  131. if (!PathExists(FLAGS_output_dir)) {
  132. MkDirs(FLAGS_output_dir);
  133. }
  134. PaddleDetection::Pipeline pipeline(FLAGS_device,
  135. FLAGS_threshold,
  136. FLAGS_output_dir,
  137. FLAGS_run_mode,
  138. FLAGS_gpu_id,
  139. FLAGS_use_mkldnn,
  140. FLAGS_cpu_threads,
  141. FLAGS_trt_calib_mode,
  142. FLAGS_do_entrance_counting,
  143. FLAGS_save_result,
  144. FLAGS_scene,
  145. FLAGS_tiny_obj,
  146. FLAGS_is_mtmct,
  147. FLAGS_secs_interval,
  148. FLAGS_track_model_dir,
  149. FLAGS_det_model_dir,
  150. FLAGS_reid_model_dir);
  151. pipeline.SetInput(FLAGS_video_file);
  152. if (!FLAGS_video_other_file.empty()) {
  153. pipeline.SetInput(FLAGS_video_other_file);
  154. }
  155. pipeline.Run();
  156. return 0;
  157. }