def bg_replace_video(model,
                     model_path,
                     transforms,
                     video_path,
                     bg_path='g',
                     save_dir='output',
                     fg_estimate=True):
    """
    predict and visualize the video.

    Args:
        model (nn.Layer): Used to predict for input video.
        model_path (str): The path of pretrained model.
        transforms (transforms.Compose): Preprocess for frames of video.
        video_path (str): The video path to be predicted.
        bg_path (str): The background. It can be image path or video path or a string of (r,g,b,w). Default: 'g'.
        save_dir (str, optional): The directory to save the visualized results. Default: 'output'.
        fg_estimate (bool, optional): Whether to estimate foreground when predicting. It is invalid if the foreground is predicted by model. Default: True
    """
    utils.utils.load_entire_model(model, model_path)
    model.eval()

    # Build loader and writer for video
    loader, writer = build_loader_writter(
        video_path, transforms, save_dir=save_dir)
    # Get bg
    bg_reader = get_bg(
        bg_path, shape=(loader.dataset.height, loader.dataset.width))

    logger.info("Start to predict...")
    progbar_pred = progbar.Progbar(target=len(loader), verbose=1)
    preprocess_cost_averager = TimeAverager()
    infer_cost_averager = TimeAverager()
    postprocess_cost_averager = TimeAverager()
    batch_start = time.time()
    with paddle.no_grad():
        for i, data in enumerate(loader):
            preprocess_cost_averager.record(time.time() - batch_start)

            infer_start = time.time()
            result = model(data)  # result maybe a Tensor or a dict
            if isinstance(result, paddle.Tensor):
                alpha = result
                fg = None
            else:
                alpha = result['alpha']
                fg = result.get('fg', None)
            infer_cost_averager.record(time.time() - infer_start)

            # postprocess
            postprocess_start = time.time()
            if isinstance(bg_reader, Iterable):
                try:
                    bg = next(bg_reader)
                except StopIteration:
                    bg_reader = get_bg(
                        bg_path,
                        shape=(loader.dataset.height, loader.dataset.width))
                    bg = next(bg_reader)
                finally:
                    bg = bg['ori_img']
            else:
                bg = bg_reader
            postprocess(
                fg,
                alpha,
                data['ori_img'],
                bg=bg,
                trans_info=data['trans_info'],
                writer=writer,
                fg_estimate=fg_estimate)
            postprocess_cost_averager.record(time.time() - postprocess_start)

            preprocess_cost = preprocess_cost_averager.get_average()
            infer_cost = infer_cost_averager.get_average()
            postprocess_cost = postprocess_cost_averager.get_average()
            progbar_pred.update(i + 1, [('preprocess_cost', preprocess_cost),
                                        ('infer_cost cost', infer_cost),
                                        ('postprocess_cost', postprocess_cost)])

            preprocess_cost_averager.reset()
            infer_cost_averager.reset()
            postprocess_cost_averager.reset()
            batch_start = time.time()
    if hasattr(model, 'reset'):
        model.reset()
    loader.dataset.release()
    if isinstance(bg, VideoReader):
        bg_reader.release()

    predict and visualize the video.

    Args:
        model (nn.Layer): Used to predict for input video.
        model_path (str): The path of pretrained model.
        transforms (transforms.Compose): Preprocess for frames of video.
        video_path (str): The video path to be predicted.
        bg_path (str): The background. It can be image path or video path or a string of (r,g,b,w). Default: 'g'.
        save_dir (str, optional): The directory to save the visualized results. Default: 'output'.
        fg_estimate (bool, optional): Whether to estimate foreground when predicting. It is invalid if the foreground is predicted by model. Default: True
    
Matting/ppmatting/core/bg_replace_video.py