text: # Copyright (c) OpenMMLab. All rights reserved.
  import os
  import os.path as osp
  import tempfile
  from argparse import ArgumentParser
  
  import mmcv
  
  from mmtrack.apis import inference_vid, init_model
  
  
  def main():
      parser = ArgumentParser()
      parser.add_argument('config', help='Config file')
      parser.add_argument('--input', help='input video file')
      parser.add_argument('--output', help='output video file (mp4 format)')
      parser.add_argument('--checkpoint', help='Checkpoint file')
      parser.add_argument(
          '--device', default='cuda:0', help='Device used for inference')
      parser.add_argument(
          '--show',
          action='store_true',
          default=False,
          help='whether to show visualizations.')
      parser.add_argument(
          '--score-thr', type=float, default=0.8, help='bbox score threshold')
      parser.add_argument(
          '--thickness', default=3, type=int, help='Thickness of bbox lines.')
      parser.add_argument('--fps', help='FPS of the output video')
      args = parser.parse_args()
  
      # load images
      if osp.isdir(args.input):
          imgs = sorted(os.listdir(args.input))
          IN_VIDEO = False
      else:
          imgs = mmcv.VideoReader(args.input)
          IN_VIDEO = True
  
      # define output
      if args.output is not None:
          if args.output.endswith('.mp4'):
              OUT_VIDEO = True
              out_dir = tempfile.TemporaryDirectory()
              out_path = out_dir.name
              _out = args.output.rsplit('/', 1)
              if len(_out) > 1:
                  os.makedirs(_out[0], exist_ok=True)
          else:
              OUT_VIDEO = False
              out_path = args.output
              os.makedirs(out_path, exist_ok=True)
      fps = args.fps
      if args.show or OUT_VIDEO:
          if fps is None and IN_VIDEO:
              fps = imgs.fps
          if not fps:
              raise ValueError('Please set the FPS for the output video.')
          fps = int(fps)
  
      # build the model from a config file and a checkpoint file
      model = init_model(args.config, args.checkpoint, device=args.device)
  
      prog_bar = mmcv.ProgressBar(len(imgs))
      # test and show/save the images
      for i, img in enumerate(imgs):
          if isinstance(img, str):
              img = osp.join(args.input, img)
              img = mmcv.imread(img)
  
          result = inference_vid(model, img, frame_id=i)
          if args.output is not None:
              if IN_VIDEO or OUT_VIDEO:
                  out_file = osp.join(out_path, f'{i:06d}.jpg')
              else:
                  out_file = osp.join(out_path, img.rsplit('/', 1)[-1])
          else:
              out_file = None
          model.show_result(
              img,
              result,
              score_thr=args.score_thr,
              show=args.show,
              wait_time=int(1000. / fps) if fps else 0,
              out_file=out_file,
              thickness=args.thickness)
          prog_bar.update()
  
      if OUT_VIDEO:
          print(
              f'\nmaking the output video at {args.output} with a FPS of {fps}')
          mmcv.frames2video(out_path, args.output, fps=fps, fourcc='mp4v')
          out_dir.cleanup()
  
  
  if __name__ == '__main__':
      main()
  