bg_replace_video.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) 2022 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 argparse
  15. import os
  16. import sys
  17. import paddle
  18. import paddleseg
  19. from paddleseg.cvlibs import manager
  20. from paddleseg.utils import get_sys_env, logger
  21. LOCAL_PATH = os.path.dirname(os.path.abspath(__file__))
  22. sys.path.append(os.path.join(LOCAL_PATH, '..'))
  23. manager.BACKBONES._components_dict.clear()
  24. manager.TRANSFORMS._components_dict.clear()
  25. import ppmatting
  26. from ppmatting.core import bg_replace_video
  27. from ppmatting.utils import Config, MatBuilder
  28. def parse_args():
  29. parser = argparse.ArgumentParser(description='Model training')
  30. parser.add_argument(
  31. "--config", dest="cfg", help="The config file.", default=None, type=str)
  32. parser.add_argument(
  33. '--model_path',
  34. dest='model_path',
  35. help='The path of model for prediction',
  36. type=str,
  37. default=None)
  38. parser.add_argument(
  39. '--video_path',
  40. dest='video_path',
  41. help='The path of video',
  42. default=None)
  43. parser.add_argument(
  44. '--background',
  45. dest='background',
  46. help='Background for replacing. It is a string which specifies the background color (r,g,b,w) or a path to background image or video.'
  47. 'If not specified, a green background is used.',
  48. type=str,
  49. default='g')
  50. parser.add_argument(
  51. '--save_dir',
  52. dest='save_dir',
  53. help='The directory for saving the model snapshot',
  54. type=str,
  55. default='./output/results')
  56. parser.add_argument(
  57. '--fg_estimate',
  58. default=True,
  59. type=eval,
  60. choices=[True, False],
  61. help='Whether to estimate foreground when predicting.')
  62. parser.add_argument(
  63. '--device',
  64. dest='device',
  65. help='Set the device type, which may be GPU, CPU or XPU.',
  66. default='gpu',
  67. type=str)
  68. return parser.parse_args()
  69. def main(args):
  70. assert args.cfg is not None, \
  71. 'No configuration file specified, please set --config'
  72. cfg = Config(args.cfg)
  73. builder = MatBuilder(cfg)
  74. paddleseg.utils.show_env_info()
  75. paddleseg.utils.show_cfg_info(cfg)
  76. paddleseg.utils.set_device(args.device)
  77. model = builder.model
  78. transforms = ppmatting.transforms.Compose(builder.val_transforms)
  79. bg_replace_video(
  80. model,
  81. model_path=args.model_path,
  82. transforms=transforms,
  83. video_path=args.video_path,
  84. bg_path=args.background,
  85. save_dir=args.save_dir,
  86. fg_estimate=args.fg_estimate)
  87. if __name__ == '__main__':
  88. args = parse_args()
  89. main(args)