beacon_aug/external/textflow/perturb.py
def _pt_texture(self, img, params):
        """
        put texture on text or background
        :param img: 2D or 3D array
        :param params: [array/str/None, array/str/None]
        :return: array
        """
        try:
            assert isinstance(params, (list, tuple)) and len(params) == 2

            if img.ndim == 2:
                img = np.stack([img] * 3, axis=-1)

            if params[0]:  # text texture
                im_fg = imread(params[0], mode='RGB') if isinstance(params[0], str) else params[0]
                h, w = img.shape[:2]
                im_fg = imresize(im_fg, (h, w), interp='bicubic')
                img = self.fg_mask[:, :, None] / 255. * im_fg + \
                    (1 - self.fg_mask[:, :, None] / 255.) * img

            if params[1]:  # background texture
                im_bg = imread(params[1], mode='RGB') if isinstance(params[1], str) else params[1]
                h, w = img.shape[:2]

                # random crop from background image
                h2, w2 = im_bg.shape[:2]
                top = np.random.choice(max(h2-h, 1))
                left = np.random.choice(max(w2-w, 1))
                im_bg = im_bg[top:min(top+10, h2), left:min(left+10, w2), ...]
                im_bg = imresize(im_bg, (h, w), interp='bicubic')

                # adjust text color for higher contrast
                bg_color_mean = np.mean(im_bg[self.fg_mask > 0, :], axis=0)
                # if img.shape[0] != self.fg_mask.shape[0] or img.shape[1] != self.fg_mask.shape[1]:
                #     print(img.shape, self.fg_mask.shape)
                text_color = img[self.fg_mask > 0, :][0]
                contrast = self._contrast(bg_color_mean, text_color)
                threshold = 100
                if abs(contrast) < threshold:
                    # print('Text color is adjusted to make it readable on the background image!')
                    # print(contrast)
                    # text_color = text_color - text_color * (threshold - contrast) / threshold
                    text_color = text_color - np.sign(contrast) * (threshold - abs(contrast))
                    img = self.fg_mask[:, :, None] / np.max(self.fg_mask).astype(np.float32) * np.ones_like(
                        img) * np.clip(text_color, 0, 255)[None, None, :]  # + (1 - self.fg_mask[:, :, None] / 255.) * img

                img = self.fg_mask[:, :, None] / np.max(self.fg_mask).astype(np.float32) * img + (
                    1 - self.fg_mask[:, :, None] / np.max(self.fg_mask).astype(np.float32)) * im_bg
                # img = img + (1 - self.fg_mask[:, :, None] / 255.) * im_bg

            self.bg_lock = True
            return img.astype(np.uint8)
        except:
            import traceback
            traceback.print_exc()
            print(img.shape, self.fg_mask.shape)
            print(img[self.fg_mask >= 255, :])
            print(np.max(self.fg_mask), np.min(self.fg_mask))
            # cv2.imwrite('%d.png' % np.random.randint(0, 100), self.fg_mask.astype(np.uint8))
            exit()

        put texture on text or background
        :param img: 2D or 3D array
        :param params: [array/str/None, array/str/None]
        :return: array
        
put texture on text or background
put texture on text or background
def _pt_texture(self, img, params):
        try:
            assert isinstance(params, (list, tuple)) and len(params) == 2
            if img.ndim == 2:
                img = np.stack([img] * 3, axis=-1)
            if params[0]:  
                im_fg = imread(params[0], mode='RGB') if isinstance(params[0], str) else params[0]
                h, w = img.shape[:2]
                im_fg = imresize(im_fg, (h, w), interp='bicubic')
                img = self.fg_mask[:, :, None] / 255. * im_fg + \
                    (1 - self.fg_mask[:, :, None] / 255.) * img
            if params[1]:  
                im_bg = imread(params[1], mode='RGB') if isinstance(params[1], str) else params[1]
                h, w = img.shape[:2]
                h2, w2 = im_bg.shape[:2]
                top = np.random.choice(max(h2-h, 1))
                left = np.random.choice(max(w2-w, 1))
                im_bg = im_bg[top:min(top+10, h2), left:min(left+10, w2), ...]
                im_bg = imresize(im_bg, (h, w), interp='bicubic')
                bg_color_mean = np.mean(im_bg[self.fg_mask > 0, :], axis=0)
                text_color = img[self.fg_mask > 0, :][0]
                contrast = self._contrast(bg_color_mean, text_color)
                threshold = 100
                if abs(contrast) < threshold:
                    text_color = text_color - np.sign(contrast) * (threshold - abs(contrast))
                    img = self.fg_mask[:, :, None] / np.max(self.fg_mask).astype(np.float32) * np.ones_like(
                        img) * np.clip(text_color, 0, 255)[None, None, :]  
                img = self.fg_mask[:, :, None] / np.max(self.fg_mask).astype(np.float32) * img + (
                    1 - self.fg_mask[:, :, None] / np.max(self.fg_mask).astype(np.float32)) * im_bg
            self.bg_lock = True
            return img.astype(np.uint8)
        except:
            import traceback
            traceback.print_exc()
            print(img.shape, self.fg_mask.shape)
            print(img[self.fg_mask >= 255, :])
            print(np.max(self.fg_mask), np.min(self.fg_mask))
            exit()
def _pt_texture ( self , img , params ) : try : assert isinstance ( params , ( list , tuple ) ) and len ( params ) == 2 if img . ndim == 2 : img = np . stack ( [ img ] * 3 , axis = - 1 ) if params [ 0 ] : im_fg = imread ( params [ 0 ] , mode = 'RGB' ) if isinstance ( params [ 0 ] , str ) else params [ 0 ] h , w = img . shape [ : 2 ] im_fg = imresize ( im_fg , ( h , w ) , interp = 'bicubic' ) img = self . fg_mask [ : , : , None ] / 255. * im_fg + ( 1 - self . fg_mask [ : , : , None ] / 255. ) * img if params [ 1 ] : im_bg = imread ( params [ 1 ] , mode = 'RGB' ) if isinstance ( params [ 1 ] , str ) else params [ 1 ] h , w = img . shape [ : 2 ] h2 , w2 = im_bg . shape [ : 2 ] top = np . random . choice ( max ( h2 - h , 1 ) ) left = np . random . choice ( max ( w2 - w , 1 ) ) im_bg = im_bg [ top : min ( top + 10 , h2 ) , left : min ( left + 10 , w2 ) , ... ] im_bg = imresize ( im_bg , ( h , w ) , interp = 'bicubic' ) bg_color_mean = np . mean ( im_bg [ self . fg_mask > 0 , : ] , axis = 0 ) text_color = img [ self . fg_mask > 0 , : ] [ 0 ] contrast = self . _contrast ( bg_color_mean , text_color ) threshold = 100 if abs ( contrast ) < threshold : text_color = text_color - np . sign ( contrast ) * ( threshold - abs ( contrast ) ) img = self . fg_mask [ : , : , None ] / np . max ( self . fg_mask ) . astype ( np . float32 ) * np . ones_like ( img ) * np . clip ( text_color , 0 , 255 ) [ None , None , : ] img = self . fg_mask [ : , : , None ] / np . max ( self . fg_mask ) . astype ( np . float32 ) * img + ( 1 - self . fg_mask [ : , : , None ] / np . max ( self . fg_mask ) . astype ( np . float32 ) ) * im_bg self . bg_lock = True return img . astype ( np . uint8 ) except : import traceback traceback . print_exc ( ) print ( img . shape , self . fg_mask . shape ) print ( img [ self . fg_mask >= 255 , : ] ) print ( np . max ( self . fg_mask ) , np . min ( self . fg_mask ) ) exit ( )
put texture on text or background
put texture on text or background
"""
        put texture on text or background
        :param img: 2D or 3D array
        :param params: [array/str/None, array/str/None]
        :return: array
        """ # text texture # background texture # random crop from background image # adjust text color for higher contrast # if img.shape[0] != self.fg_mask.shape[0] or img.shape[1] != self.fg_mask.shape[1]: #     print(img.shape, self.fg_mask.shape) # print('Text color is adjusted to make it readable on the background image!') # print(contrast) # text_color = text_color - text_color * (threshold - contrast) / threshold # + (1 - self.fg_mask[:, :, None] / 255.) * img # img = img + (1 - self.fg_mask[:, :, None] / 255.) * im_bg # cv2.imwrite('%d.png' % np.random.randint(0, 100), self.fg_mask.astype(np.uint8))
None
self
img
2D or 3D array
2D or 3D array
params
None