temporal_transforms.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import random
  2. import math
  3. class LoopPadding(object):
  4. def __init__(self, size):
  5. self.size = size
  6. def __call__(self, frame_indices):
  7. out = frame_indices
  8. for index in out:
  9. if len(out) >= self.size:
  10. break
  11. out.append(index)
  12. return out
  13. class TemporalCenterCrop(object):
  14. """Temporally crop the given frame indices at a center.
  15. If the number of frames is less than the size,
  16. loop the indices as many times as necessary to satisfy the size.
  17. Args:
  18. size (int): Desired output size of the crop.
  19. """
  20. def __init__(self, size):
  21. self.size = size
  22. def __call__(self, frame_indices):
  23. """
  24. Args:
  25. frame_indices (list): frame indices to be cropped.
  26. Returns:
  27. list: Cropped frame indices.
  28. """
  29. center_index = len(frame_indices) // 2
  30. begin_index = max(0, center_index - (self.size // 2))
  31. end_index = min(begin_index + self.size, len(frame_indices))
  32. out = frame_indices[begin_index:end_index]
  33. for index in out:
  34. if len(out) >= self.size:
  35. break
  36. out.append(index)
  37. return out