Team Ai
Apppublic

Samanth/I3D_Sign_Language_Classification

sourceHugging Faceapache-2.0updated 5y agoView on Hugging Face
0likes
videotransforms.py103 linesDownload Raw Back to root
1import numpy as np2import numbers3import random4 5class RandomCrop(object):6    """Crop the given video sequences (t x h x w) at a random location.7    Args:8        size (sequence or int): Desired output size of the crop. If size is an9            int instead of sequence like (h, w), a square crop (size, size) is10            made.11    """12 13    def __init__(self, size):14        if isinstance(size, numbers.Number):15            self.size = (int(size), int(size))16        else:17            self.size = size18 19    @staticmethod20    def get_params(img, output_size):21        """Get parameters for ``crop`` for a random crop.22        Args:23            img (PIL Image): Image to be cropped.24            output_size (tuple): Expected output size of the crop.25        Returns:26            tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.27        """28        t, h, w, c = img.shape29        th, tw = output_size30        if w == tw and h == th:31            return 0, 0, h, w32 33        i = random.randint(0, h - th) if h!=th else 034        j = random.randint(0, w - tw) if w!=tw else 035        return i, j, th, tw36 37    def __call__(self, imgs):38        39        i, j, h, w = self.get_params(imgs, self.size)40 41        imgs = imgs[:, i:i+h, j:j+w, :]42        return imgs43 44    def __repr__(self):45        return self.__class__.__name__ + '(size={0})'.format(self.size)46 47class CenterCrop(object):48    """Crops the given seq Images at the center.49    Args:50        size (sequence or int): Desired output size of the crop. If size is an51            int instead of sequence like (h, w), a square crop (size, size) is52            made.53    """54 55    def __init__(self, size):56        if isinstance(size, numbers.Number):57            self.size = (int(size), int(size))58        else:59            self.size = size60 61    def __call__(self, imgs):62        """63        Args:64            img (PIL Image): Image to be cropped.65        Returns:66            PIL Image: Cropped image.67        """68        t, h, w, c = imgs.shape69        th, tw = self.size70        i = int(np.round((h - th) / 2.))71        j = int(np.round((w - tw) / 2.))72 73        return imgs[:, i:i+th, j:j+tw, :]74 75 76    def __repr__(self):77        return self.__class__.__name__ + '(size={0})'.format(self.size)78 79 80class RandomHorizontalFlip(object):81    """Horizontally flip the given seq Images randomly with a given probability.82    Args:83        p (float): probability of the image being flipped. Default value is 0.584    """85 86    def __init__(self, p=0.5):87        self.p = p88 89    def __call__(self, imgs):90        """91        Args:92            img (seq Images): seq Images to be flipped.93        Returns:94            seq Images: Randomly flipped seq images.95        """96        if random.random() < self.p:97            # t x h x w98            return np.flip(imgs, axis=2).copy()99        return imgs100 101    def __repr__(self):102        return self.__class__.__name__ + '(p={})'.format(self.p)103