mot_utils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. import time
  15. import os
  16. import sys
  17. import ast
  18. import argparse
  19. def argsparser():
  20. parser = argparse.ArgumentParser(description=__doc__)
  21. parser.add_argument(
  22. "--model_dir",
  23. type=str,
  24. default=None,
  25. help=("Directory include:'model.pdiparams', 'model.pdmodel', "
  26. "'infer_cfg.yml', created by tools/export_model.py."),
  27. required=True)
  28. parser.add_argument(
  29. "--image_file", type=str, default=None, help="Path of image file.")
  30. parser.add_argument(
  31. "--image_dir",
  32. type=str,
  33. default=None,
  34. help="Dir of image file, `image_file` has a higher priority.")
  35. parser.add_argument(
  36. "--batch_size", type=int, default=1, help="batch_size for inference.")
  37. parser.add_argument(
  38. "--video_file",
  39. type=str,
  40. default=None,
  41. help="Path of video file, `video_file` or `camera_id` has a highest priority."
  42. )
  43. parser.add_argument(
  44. "--camera_id",
  45. type=int,
  46. default=-1,
  47. help="device id of camera to predict.")
  48. parser.add_argument(
  49. "--threshold", type=float, default=0.5, help="Threshold of score.")
  50. parser.add_argument(
  51. "--output_dir",
  52. type=str,
  53. default="output",
  54. help="Directory of output visualization files.")
  55. parser.add_argument(
  56. "--run_mode",
  57. type=str,
  58. default='paddle',
  59. help="mode of running(paddle/trt_fp32/trt_fp16/trt_int8)")
  60. parser.add_argument(
  61. "--device",
  62. type=str,
  63. default='cpu',
  64. help="Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU."
  65. )
  66. parser.add_argument(
  67. "--use_gpu",
  68. type=ast.literal_eval,
  69. default=False,
  70. help="Deprecated, please use `--device`.")
  71. parser.add_argument(
  72. "--run_benchmark",
  73. type=ast.literal_eval,
  74. default=False,
  75. help="Whether to predict a image_file repeatedly for benchmark")
  76. parser.add_argument(
  77. "--enable_mkldnn",
  78. type=ast.literal_eval,
  79. default=False,
  80. help="Whether use mkldnn with CPU.")
  81. parser.add_argument(
  82. "--cpu_threads", type=int, default=1, help="Num of threads with CPU.")
  83. parser.add_argument(
  84. "--trt_min_shape", type=int, default=1, help="min_shape for TensorRT.")
  85. parser.add_argument(
  86. "--trt_max_shape",
  87. type=int,
  88. default=1280,
  89. help="max_shape for TensorRT.")
  90. parser.add_argument(
  91. "--trt_opt_shape",
  92. type=int,
  93. default=640,
  94. help="opt_shape for TensorRT.")
  95. parser.add_argument(
  96. "--trt_calib_mode",
  97. type=bool,
  98. default=False,
  99. help="If the model is produced by TRT offline quantitative "
  100. "calibration, trt_calib_mode need to set True.")
  101. parser.add_argument(
  102. '--save_images',
  103. action='store_true',
  104. help='Save visualization image results.')
  105. parser.add_argument(
  106. '--save_mot_txts',
  107. action='store_true',
  108. help='Save tracking results (txt).')
  109. parser.add_argument(
  110. '--save_mot_txt_per_img',
  111. action='store_true',
  112. help='Save tracking results (txt) for each image.')
  113. parser.add_argument(
  114. '--scaled',
  115. type=bool,
  116. default=False,
  117. help="Whether coords after detector outputs are scaled, False in JDE YOLOv3 "
  118. "True in general detector.")
  119. parser.add_argument(
  120. "--tracker_config", type=str, default=None, help=("tracker donfig"))
  121. parser.add_argument(
  122. "--reid_model_dir",
  123. type=str,
  124. default=None,
  125. help=("Directory include:'model.pdiparams', 'model.pdmodel', "
  126. "'infer_cfg.yml', created by tools/export_model.py."))
  127. parser.add_argument(
  128. "--reid_batch_size",
  129. type=int,
  130. default=50,
  131. help="max batch_size for reid model inference.")
  132. parser.add_argument(
  133. '--use_dark',
  134. type=ast.literal_eval,
  135. default=True,
  136. help='whether to use darkpose to get better keypoint position predict ')
  137. parser.add_argument(
  138. "--do_entrance_counting",
  139. action='store_true',
  140. help="Whether counting the numbers of identifiers entering "
  141. "or getting out from the entrance. Note that only support one-class"
  142. "counting, multi-class counting is coming soon.")
  143. parser.add_argument(
  144. "--secs_interval",
  145. type=int,
  146. default=2,
  147. help="The seconds interval to count after tracking")
  148. parser.add_argument(
  149. "--draw_center_traj",
  150. action='store_true',
  151. help="Whether drawing the trajectory of center")
  152. parser.add_argument(
  153. "--mtmct_dir",
  154. type=str,
  155. default=None,
  156. help="The MTMCT scene video folder.")
  157. parser.add_argument(
  158. "--mtmct_cfg", type=str, default=None, help="The MTMCT config.")
  159. return parser
  160. class Times(object):
  161. def __init__(self):
  162. self.time = 0.
  163. # start time
  164. self.st = 0.
  165. # end time
  166. self.et = 0.
  167. def start(self):
  168. self.st = time.time()
  169. def end(self, repeats=1, accumulative=True):
  170. self.et = time.time()
  171. if accumulative:
  172. self.time += (self.et - self.st) / repeats
  173. else:
  174. self.time = (self.et - self.st) / repeats
  175. def reset(self):
  176. self.time = 0.
  177. self.st = 0.
  178. self.et = 0.
  179. def value(self):
  180. return round(self.time, 4)
  181. class Timer(Times):
  182. def __init__(self, with_tracker=False):
  183. super(Timer, self).__init__()
  184. self.with_tracker = with_tracker
  185. self.preprocess_time_s = Times()
  186. self.inference_time_s = Times()
  187. self.postprocess_time_s = Times()
  188. self.tracking_time_s = Times()
  189. self.img_num = 0
  190. def info(self, average=False):
  191. pre_time = self.preprocess_time_s.value()
  192. infer_time = self.inference_time_s.value()
  193. post_time = self.postprocess_time_s.value()
  194. track_time = self.tracking_time_s.value()
  195. total_time = pre_time + infer_time + post_time
  196. if self.with_tracker:
  197. total_time = total_time + track_time
  198. total_time = round(total_time, 4)
  199. print("------------------ Inference Time Info ----------------------")
  200. print("total_time(ms): {}, img_num: {}".format(total_time * 1000,
  201. self.img_num))
  202. preprocess_time = round(pre_time / max(1, self.img_num),
  203. 4) if average else pre_time
  204. postprocess_time = round(post_time / max(1, self.img_num),
  205. 4) if average else post_time
  206. inference_time = round(infer_time / max(1, self.img_num),
  207. 4) if average else infer_time
  208. tracking_time = round(track_time / max(1, self.img_num),
  209. 4) if average else track_time
  210. average_latency = total_time / max(1, self.img_num)
  211. qps = 0
  212. if total_time > 0:
  213. qps = 1 / average_latency
  214. print("average latency time(ms): {:.2f}, QPS: {:2f}".format(
  215. average_latency * 1000, qps))
  216. if self.with_tracker:
  217. print(
  218. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}, tracking_time(ms): {:.2f}".
  219. format(preprocess_time * 1000, inference_time * 1000,
  220. postprocess_time * 1000, tracking_time * 1000))
  221. else:
  222. print(
  223. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}".
  224. format(preprocess_time * 1000, inference_time * 1000,
  225. postprocess_time * 1000))
  226. def report(self, average=False):
  227. dic = {}
  228. pre_time = self.preprocess_time_s.value()
  229. infer_time = self.inference_time_s.value()
  230. post_time = self.postprocess_time_s.value()
  231. track_time = self.tracking_time_s.value()
  232. dic['preprocess_time_s'] = round(pre_time / max(1, self.img_num),
  233. 4) if average else pre_time
  234. dic['inference_time_s'] = round(infer_time / max(1, self.img_num),
  235. 4) if average else infer_time
  236. dic['postprocess_time_s'] = round(post_time / max(1, self.img_num),
  237. 4) if average else post_time
  238. dic['img_num'] = self.img_num
  239. total_time = pre_time + infer_time + post_time
  240. if self.with_tracker:
  241. dic['tracking_time_s'] = round(track_time / max(1, self.img_num),
  242. 4) if average else track_time
  243. total_time = total_time + track_time
  244. dic['total_time_s'] = round(total_time, 4)
  245. return dic
  246. def get_current_memory_mb():
  247. """
  248. It is used to Obtain the memory usage of the CPU and GPU during the running of the program.
  249. And this function Current program is time-consuming.
  250. """
  251. import pynvml
  252. import psutil
  253. import GPUtil
  254. gpu_id = int(os.environ.get('CUDA_VISIBLE_DEVICES', 0))
  255. pid = os.getpid()
  256. p = psutil.Process(pid)
  257. info = p.memory_full_info()
  258. cpu_mem = info.uss / 1024. / 1024.
  259. gpu_mem = 0
  260. gpu_percent = 0
  261. gpus = GPUtil.getGPUs()
  262. if gpu_id is not None and len(gpus) > 0:
  263. gpu_percent = gpus[gpu_id].load
  264. pynvml.nvmlInit()
  265. handle = pynvml.nvmlDeviceGetHandleByIndex(0)
  266. meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
  267. gpu_mem = meminfo.used / 1024. / 1024.
  268. return round(cpu_mem, 4), round(gpu_mem, 4), round(gpu_percent, 4)
  269. def video2frames(video_path, outpath, frame_rate=25, **kargs):
  270. def _dict2str(kargs):
  271. cmd_str = ''
  272. for k, v in kargs.items():
  273. cmd_str += (' ' + str(k) + ' ' + str(v))
  274. return cmd_str
  275. ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
  276. vid_name = os.path.basename(video_path).split('.')[0]
  277. out_full_path = os.path.join(outpath, vid_name)
  278. if not os.path.exists(out_full_path):
  279. os.makedirs(out_full_path)
  280. # video file name
  281. outformat = os.path.join(out_full_path, '%05d.jpg')
  282. cmd = ffmpeg
  283. cmd = ffmpeg + [
  284. ' -i ', video_path, ' -r ', str(frame_rate), ' -f image2 ', outformat
  285. ]
  286. cmd = ''.join(cmd) + _dict2str(kargs)
  287. if os.system(cmd) != 0:
  288. raise RuntimeError('ffmpeg process video: {} error'.format(video_path))
  289. sys.exit(-1)
  290. sys.stdout.flush()
  291. return out_full_path
  292. def _is_valid_video(f, extensions=('.mp4', '.avi', '.mov', '.rmvb', '.flv')):
  293. return f.lower().endswith(extensions)