generate_videomatte_with_background_image.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. python generate_videomatte_with_background_image.py \
  3. --videomatte-dir ../matting-data/VideoMatte240K_JPEG_HD/test \
  4. --background-dir ../matting-data/Backgrounds/valid \
  5. --num-samples 25 \
  6. --resize 512 288 \
  7. --out-dir ../matting-data/evaluation/vidematte_static_sd/
  8. """
  9. import argparse
  10. import os
  11. import pims
  12. import numpy as np
  13. import random
  14. from PIL import Image
  15. from tqdm import tqdm
  16. parser = argparse.ArgumentParser()
  17. parser.add_argument('--videomatte-dir', type=str, required=True)
  18. parser.add_argument('--background-dir', type=str, required=True)
  19. parser.add_argument('--num-samples', type=int, default=20)
  20. parser.add_argument('--num-frames', type=int, default=100)
  21. parser.add_argument('--resize', type=int, default=None, nargs=2)
  22. parser.add_argument('--out-dir', type=str, required=True)
  23. parser.add_argument('--extension', type=str, default='.png')
  24. args = parser.parse_args()
  25. random.seed(10)
  26. videomatte_filenames = [(clipname, sorted(os.listdir(os.path.join(args.videomatte_dir, 'fgr', clipname))))
  27. for clipname in sorted(os.listdir(os.path.join(args.videomatte_dir, 'fgr')))]
  28. background_filenames = os.listdir(args.background_dir)
  29. random.shuffle(background_filenames)
  30. for i in range(args.num_samples):
  31. clipname, framenames = videomatte_filenames[i % len(videomatte_filenames)]
  32. out_path = os.path.join(args.out_dir, str(i).zfill(4))
  33. os.makedirs(os.path.join(out_path, 'fgr'), exist_ok=True)
  34. os.makedirs(os.path.join(out_path, 'pha'), exist_ok=True)
  35. os.makedirs(os.path.join(out_path, 'com'), exist_ok=True)
  36. os.makedirs(os.path.join(out_path, 'bgr'), exist_ok=True)
  37. with Image.open(os.path.join(args.background_dir, background_filenames[i])) as bgr:
  38. bgr = bgr.convert('RGB')
  39. base_t = random.choice(range(len(framenames) - args.num_frames))
  40. for t in tqdm(range(args.num_frames), desc=str(i).zfill(4)):
  41. with Image.open(os.path.join(args.videomatte_dir, 'fgr', clipname, framenames[base_t + t])) as fgr, \
  42. Image.open(os.path.join(args.videomatte_dir, 'pha', clipname, framenames[base_t + t])) as pha:
  43. fgr = fgr.convert('RGB')
  44. pha = pha.convert('L')
  45. if args.resize is not None:
  46. fgr = fgr.resize(args.resize, Image.BILINEAR)
  47. pha = pha.resize(args.resize, Image.BILINEAR)
  48. if i // len(videomatte_filenames) % 2 == 1:
  49. fgr = fgr.transpose(Image.FLIP_LEFT_RIGHT)
  50. pha = pha.transpose(Image.FLIP_LEFT_RIGHT)
  51. fgr.save(os.path.join(out_path, 'fgr', str(t).zfill(4) + args.extension))
  52. pha.save(os.path.join(out_path, 'pha', str(t).zfill(4) + args.extension))
  53. if t == 0:
  54. bgr = bgr.resize(fgr.size, Image.BILINEAR)
  55. bgr.save(os.path.join(out_path, 'bgr', str(t).zfill(4) + args.extension))
  56. else:
  57. os.symlink(str(0).zfill(4) + args.extension, os.path.join(out_path, 'bgr', str(t).zfill(4) + args.extension))
  58. pha = np.asarray(pha).astype(float)[:, :, None] / 255
  59. com = Image.fromarray(np.uint8(np.asarray(fgr) * pha + np.asarray(bgr) * (1 - pha)))
  60. com.save(os.path.join(out_path, 'com', str(t).zfill(4) + args.extension))