generate_videomatte_with_background_video.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. python generate_videomatte_with_background_video.py \
  3. --videomatte-dir ../matting-data/VideoMatte240K_JPEG_HD/test \
  4. --background-dir ../matting-data/BackgroundVideos_mp4/test \
  5. --resize 512 288 \
  6. --out-dir ../matting-data/evaluation/vidematte_motion_sd/
  7. """
  8. import argparse
  9. import os
  10. import pims
  11. import numpy as np
  12. import random
  13. from PIL import Image
  14. from tqdm import tqdm
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument('--videomatte-dir', type=str, required=True)
  17. parser.add_argument('--background-dir', type=str, required=True)
  18. parser.add_argument('--num-samples', type=int, default=20)
  19. parser.add_argument('--num-frames', type=int, default=100)
  20. parser.add_argument('--resize', type=int, default=None, nargs=2)
  21. parser.add_argument('--out-dir', type=str, required=True)
  22. args = parser.parse_args()
  23. # Hand selected a list of videos
  24. background_filenames = [
  25. "0000.mp4",
  26. "0007.mp4",
  27. "0008.mp4",
  28. "0010.mp4",
  29. "0013.mp4",
  30. "0015.mp4",
  31. "0016.mp4",
  32. "0018.mp4",
  33. "0021.mp4",
  34. "0029.mp4",
  35. "0033.mp4",
  36. "0035.mp4",
  37. "0039.mp4",
  38. "0050.mp4",
  39. "0052.mp4",
  40. "0055.mp4",
  41. "0060.mp4",
  42. "0063.mp4",
  43. "0087.mp4",
  44. "0086.mp4",
  45. "0090.mp4",
  46. "0101.mp4",
  47. "0110.mp4",
  48. "0117.mp4",
  49. "0120.mp4",
  50. "0122.mp4",
  51. "0123.mp4",
  52. "0125.mp4",
  53. "0128.mp4",
  54. "0131.mp4",
  55. "0172.mp4",
  56. "0176.mp4",
  57. "0181.mp4",
  58. "0187.mp4",
  59. "0193.mp4",
  60. "0198.mp4",
  61. "0220.mp4",
  62. "0221.mp4",
  63. "0224.mp4",
  64. "0229.mp4",
  65. "0233.mp4",
  66. "0238.mp4",
  67. "0241.mp4",
  68. "0245.mp4",
  69. "0246.mp4"
  70. ]
  71. random.seed(10)
  72. videomatte_filenames = [(clipname, sorted(os.listdir(os.path.join(args.videomatte_dir, 'fgr', clipname))))
  73. for clipname in sorted(os.listdir(os.path.join(args.videomatte_dir, 'fgr')))]
  74. random.shuffle(background_filenames)
  75. for i in range(args.num_samples):
  76. bgrs = pims.PyAVVideoReader(os.path.join(args.background_dir, background_filenames[i % len(background_filenames)]))
  77. clipname, framenames = videomatte_filenames[i % len(videomatte_filenames)]
  78. out_path = os.path.join(args.out_dir, str(i).zfill(4))
  79. os.makedirs(os.path.join(out_path, 'fgr'), exist_ok=True)
  80. os.makedirs(os.path.join(out_path, 'pha'), exist_ok=True)
  81. os.makedirs(os.path.join(out_path, 'com'), exist_ok=True)
  82. os.makedirs(os.path.join(out_path, 'bgr'), exist_ok=True)
  83. base_t = random.choice(range(len(framenames) - args.num_frames))
  84. for t in tqdm(range(args.num_frames), desc=str(i).zfill(4)):
  85. with Image.open(os.path.join(args.videomatte_dir, 'fgr', clipname, framenames[base_t + t])) as fgr, \
  86. Image.open(os.path.join(args.videomatte_dir, 'pha', clipname, framenames[base_t + t])) as pha:
  87. fgr = fgr.convert('RGB')
  88. pha = pha.convert('L')
  89. if args.resize is not None:
  90. fgr = fgr.resize(args.resize, Image.BILINEAR)
  91. pha = pha.resize(args.resize, Image.BILINEAR)
  92. if i // len(videomatte_filenames) % 2 == 1:
  93. fgr = fgr.transpose(Image.FLIP_LEFT_RIGHT)
  94. pha = pha.transpose(Image.FLIP_LEFT_RIGHT)
  95. fgr.save(os.path.join(out_path, 'fgr', str(t).zfill(4) + '.png'))
  96. pha.save(os.path.join(out_path, 'pha', str(t).zfill(4) + '.png'))
  97. bgr = Image.fromarray(bgrs[t])
  98. bgr = bgr.resize(fgr.size, Image.BILINEAR)
  99. bgr.save(os.path.join(out_path, 'bgr', str(t).zfill(4) + '.png'))
  100. pha = np.asarray(pha).astype(float)[:, :, None] / 255
  101. com = Image.fromarray(np.uint8(np.asarray(fgr) * pha + np.asarray(bgr) * (1 - pha)))
  102. com.save(os.path.join(out_path, 'com', str(t).zfill(4) + '.png'))