beacon_aug/external/textflow/perturb.py
def _pt_noise(self, img, params):
        """
        add noise to text image: Gaussian, uniform, or salt pepper.
        :param img: array
        :param params: list or tuple, [noise_type, std], value range [0, 1]
        :return: array, dtype=uint8
        """
        assert isinstance(params, (list, tuple)) and len(params) == 2
        assert 0 <= params[0] <= 1 and 0 <= params[1] <= 1

        def noise_normal(param):
            return np.random.normal(scale=param, size=img.shape).astype(np.float32)

        def noise_uniform(param):
            return np.random.uniform(low=-param, high=param, size=img.shape).astype(np.float32)

        def noise_salt_pepper(param):
            param = round(param)
            min_param = 10
            tmp = np.random.randint(max(param, min_param), size=img.shape).astype(np.float32)
            tmp[tmp == 0] = -255
            tmp[tmp == (param - 1)] = 255
            return tmp

        noise_types = [
            noise_normal,
            noise_uniform,
            noise_salt_pepper
        ]
        if params[1] == 0:
            params[1] = 1
        idx_noise = int(round(params[0] * (len(noise_types) - 1)))
        im_noise = noise_types[idx_noise](1. / params[1])

        self.bg_lock = True

        return np.clip(img + im_noise, 0, 255).astype(np.uint8)

        add noise to text image: Gaussian, uniform, or salt pepper.
        :param img: array
        :param params: list or tuple, [noise_type, std], value range [0, 1]
        :return: array, dtype=uint8
        
add noise to text image: Gaussian, uniform, or salt pepper.
add noise to text image : Gaussian uniform or salt pepper .
def _pt_noise(self, img, params):
        assert isinstance(params, (list, tuple)) and len(params) == 2
        assert 0 <= params[0] <= 1 and 0 <= params[1] <= 1
        def noise_normal(param):
            return np.random.normal(scale=param, size=img.shape).astype(np.float32)
        def noise_uniform(param):
            return np.random.uniform(low=-param, high=param, size=img.shape).astype(np.float32)
        def noise_salt_pepper(param):
            param = round(param)
            min_param = 10
            tmp = np.random.randint(max(param, min_param), size=img.shape).astype(np.float32)
            tmp[tmp == 0] = -255
            tmp[tmp == (param - 1)] = 255
            return tmp
        noise_types = [
            noise_normal,
            noise_uniform,
            noise_salt_pepper
        ]
        if params[1] == 0:
            params[1] = 1
        idx_noise = int(round(params[0] * (len(noise_types) - 1)))
        im_noise = noise_types[idx_noise](1. / params[1])
        self.bg_lock = True
        return np.clip(img + im_noise, 0, 255).astype(np.uint8)
def _pt_noise ( self , img , params ) : assert isinstance ( params , ( list , tuple ) ) and len ( params ) == 2 assert 0 <= params [ 0 ] <= 1 and 0 <= params [ 1 ] <= 1 def noise_normal ( param ) : return np . random . normal ( scale = param , size = img . shape ) . astype ( np . float32 ) def noise_uniform ( param ) : return np . random . uniform ( low = - param , high = param , size = img . shape ) . astype ( np . float32 ) def noise_salt_pepper ( param ) : param = round ( param ) min_param = 10 tmp = np . random . randint ( max ( param , min_param ) , size = img . shape ) . astype ( np . float32 ) tmp [ tmp == 0 ] = - 255 tmp [ tmp == ( param - 1 ) ] = 255 return tmp noise_types = [ noise_normal , noise_uniform , noise_salt_pepper ] if params [ 1 ] == 0 : params [ 1 ] = 1 idx_noise = int ( round ( params [ 0 ] * ( len ( noise_types ) - 1 ) ) ) im_noise = noise_types [ idx_noise ] ( 1. / params [ 1 ] ) self . bg_lock = True return np . clip ( img + im_noise , 0 , 255 ) . astype ( np . uint8 )
add noise to text image: Gaussian, uniform, or salt pepper.
add noise to text image : Gaussian uniform or salt pepper .
"""
        add noise to text image: Gaussian, uniform, or salt pepper.
        :param img: array
        :param params: list or tuple, [noise_type, std], value range [0, 1]
        :return: array, dtype=uint8
        """
None
self
img
None
params
list or tuple, [noise_type, std], value range [0, 1]
list or tuple [ noise_type std ] value range [ 0 1 ]