mot_utils.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. '--skip_frame_num',
  139. type=int,
  140. default=-1,
  141. help='Skip frames to speed up the process of getting mot results.')
  142. parser.add_argument(
  143. '--warmup_frame',
  144. type=int,
  145. default=50,
  146. help='Warmup frames to test speed of the process of getting mot results.'
  147. )
  148. parser.add_argument(
  149. "--do_entrance_counting",
  150. action='store_true',
  151. help="Whether counting the numbers of identifiers entering "
  152. "or getting out from the entrance. Note that only support single-class MOT."
  153. )
  154. parser.add_argument(
  155. "--do_break_in_counting",
  156. action='store_true',
  157. help="Whether counting the numbers of identifiers break in "
  158. "the area. Note that only support single-class MOT and "
  159. "the video should be taken by a static camera.")
  160. parser.add_argument(
  161. "--region_type",
  162. type=str,
  163. default='horizontal',
  164. help="Area type for entrance counting or break in counting, 'horizontal' and "
  165. "'vertical' used when do entrance counting. 'custom' used when do break in counting. "
  166. "Note that only support single-class MOT, and the video should be taken by a static camera."
  167. )
  168. parser.add_argument(
  169. '--region_polygon',
  170. nargs='+',
  171. type=int,
  172. default=[],
  173. help="Clockwise point coords (x0,y0,x1,y1...) of polygon of area when "
  174. "do_break_in_counting. Note that only support single-class MOT and "
  175. "the video should be taken by a static camera.")
  176. parser.add_argument(
  177. "--secs_interval",
  178. type=int,
  179. default=2,
  180. help="The seconds interval to count after tracking")
  181. parser.add_argument(
  182. "--draw_center_traj",
  183. action='store_true',
  184. help="Whether drawing the trajectory of center")
  185. parser.add_argument(
  186. "--mtmct_dir",
  187. type=str,
  188. default=None,
  189. help="The MTMCT scene video folder.")
  190. parser.add_argument(
  191. "--mtmct_cfg", type=str, default=None, help="The MTMCT config.")
  192. return parser
  193. class Times(object):
  194. def __init__(self):
  195. self.time = 0.
  196. # start time
  197. self.st = 0.
  198. # end time
  199. self.et = 0.
  200. def start(self):
  201. self.st = time.time()
  202. def end(self, repeats=1, accumulative=True):
  203. self.et = time.time()
  204. if accumulative:
  205. self.time += (self.et - self.st) / repeats
  206. else:
  207. self.time = (self.et - self.st) / repeats
  208. def reset(self):
  209. self.time = 0.
  210. self.st = 0.
  211. self.et = 0.
  212. def value(self):
  213. return round(self.time, 4)
  214. class Timer(Times):
  215. def __init__(self, with_tracker=False):
  216. super(Timer, self).__init__()
  217. self.with_tracker = with_tracker
  218. self.preprocess_time_s = Times()
  219. self.inference_time_s = Times()
  220. self.postprocess_time_s = Times()
  221. self.tracking_time_s = Times()
  222. self.img_num = 0
  223. def info(self, average=False):
  224. pre_time = self.preprocess_time_s.value()
  225. infer_time = self.inference_time_s.value()
  226. post_time = self.postprocess_time_s.value()
  227. track_time = self.tracking_time_s.value()
  228. total_time = pre_time + infer_time + post_time
  229. if self.with_tracker:
  230. total_time = total_time + track_time
  231. total_time = round(total_time, 4)
  232. print("------------------ Inference Time Info ----------------------")
  233. print("total_time(ms): {}, img_num: {}".format(total_time * 1000,
  234. self.img_num))
  235. preprocess_time = round(pre_time / max(1, self.img_num),
  236. 4) if average else pre_time
  237. postprocess_time = round(post_time / max(1, self.img_num),
  238. 4) if average else post_time
  239. inference_time = round(infer_time / max(1, self.img_num),
  240. 4) if average else infer_time
  241. tracking_time = round(track_time / max(1, self.img_num),
  242. 4) if average else track_time
  243. average_latency = total_time / max(1, self.img_num)
  244. qps = 0
  245. if total_time > 0:
  246. qps = 1 / average_latency
  247. print("average latency time(ms): {:.2f}, QPS: {:2f}".format(
  248. average_latency * 1000, qps))
  249. if self.with_tracker:
  250. print(
  251. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}, tracking_time(ms): {:.2f}".
  252. format(preprocess_time * 1000, inference_time * 1000,
  253. postprocess_time * 1000, tracking_time * 1000))
  254. else:
  255. print(
  256. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}".
  257. format(preprocess_time * 1000, inference_time * 1000,
  258. postprocess_time * 1000))
  259. def tracking_info(self, average=True):
  260. pre_time = self.preprocess_time_s.value()
  261. infer_time = self.inference_time_s.value()
  262. post_time = self.postprocess_time_s.value()
  263. track_time = self.tracking_time_s.value()
  264. total_time = pre_time + infer_time + post_time
  265. if self.with_tracker:
  266. total_time = total_time + track_time
  267. total_time = round(total_time, 4)
  268. print(
  269. "------------------ Tracking Module Time Info ----------------------"
  270. )
  271. preprocess_time = round(pre_time / max(1, self.img_num),
  272. 4) if average else pre_time
  273. postprocess_time = round(post_time / max(1, self.img_num),
  274. 4) if average else post_time
  275. inference_time = round(infer_time / max(1, self.img_num),
  276. 4) if average else infer_time
  277. tracking_time = round(track_time / max(1, self.img_num),
  278. 4) if average else track_time
  279. if self.with_tracker:
  280. print(
  281. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}, tracking_time(ms): {:.2f}".
  282. format(preprocess_time * 1000, inference_time * 1000,
  283. postprocess_time * 1000, tracking_time * 1000))
  284. else:
  285. print(
  286. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}".
  287. format(preprocess_time * 1000, inference_time * 1000,
  288. postprocess_time * 1000))
  289. def report(self, average=False):
  290. dic = {}
  291. pre_time = self.preprocess_time_s.value()
  292. infer_time = self.inference_time_s.value()
  293. post_time = self.postprocess_time_s.value()
  294. track_time = self.tracking_time_s.value()
  295. dic['preprocess_time_s'] = round(pre_time / max(1, self.img_num),
  296. 4) if average else pre_time
  297. dic['inference_time_s'] = round(infer_time / max(1, self.img_num),
  298. 4) if average else infer_time
  299. dic['postprocess_time_s'] = round(post_time / max(1, self.img_num),
  300. 4) if average else post_time
  301. dic['img_num'] = self.img_num
  302. total_time = pre_time + infer_time + post_time
  303. if self.with_tracker:
  304. dic['tracking_time_s'] = round(track_time / max(1, self.img_num),
  305. 4) if average else track_time
  306. total_time = total_time + track_time
  307. dic['total_time_s'] = round(total_time, 4)
  308. return dic
  309. def get_current_memory_mb():
  310. """
  311. It is used to Obtain the memory usage of the CPU and GPU during the running of the program.
  312. And this function Current program is time-consuming.
  313. """
  314. import pynvml
  315. import psutil
  316. import GPUtil
  317. gpu_id = int(os.environ.get('CUDA_VISIBLE_DEVICES', 0))
  318. pid = os.getpid()
  319. p = psutil.Process(pid)
  320. info = p.memory_full_info()
  321. cpu_mem = info.uss / 1024. / 1024.
  322. gpu_mem = 0
  323. gpu_percent = 0
  324. gpus = GPUtil.getGPUs()
  325. if gpu_id is not None and len(gpus) > 0:
  326. gpu_percent = gpus[gpu_id].load
  327. pynvml.nvmlInit()
  328. handle = pynvml.nvmlDeviceGetHandleByIndex(0)
  329. meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
  330. gpu_mem = meminfo.used / 1024. / 1024.
  331. return round(cpu_mem, 4), round(gpu_mem, 4), round(gpu_percent, 4)
  332. def video2frames(video_path, outpath, frame_rate=25, **kargs):
  333. def _dict2str(kargs):
  334. cmd_str = ''
  335. for k, v in kargs.items():
  336. cmd_str += (' ' + str(k) + ' ' + str(v))
  337. return cmd_str
  338. ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error ']
  339. vid_name = os.path.basename(video_path).split('.')[0]
  340. out_full_path = os.path.join(outpath, vid_name)
  341. if not os.path.exists(out_full_path):
  342. os.makedirs(out_full_path)
  343. # video file name
  344. outformat = os.path.join(out_full_path, '%05d.jpg')
  345. cmd = ffmpeg
  346. cmd = ffmpeg + [
  347. ' -i ', video_path, ' -r ', str(frame_rate), ' -f image2 ', outformat
  348. ]
  349. cmd = ''.join(cmd) + _dict2str(kargs)
  350. if os.system(cmd) != 0:
  351. raise RuntimeError('ffmpeg process video: {} error'.format(video_path))
  352. sys.exit(-1)
  353. sys.stdout.flush()
  354. return out_full_path
  355. def _is_valid_video(f, extensions=('.mp4', '.avi', '.mov', '.rmvb', '.flv')):
  356. return f.lower().endswith(extensions)