beacon_aug/external/textflow/synthesis.py
def _place_text_on_image(self, text_list, font_list, region_list=None, effect_list=None,
                             is_top_left_origin=True, ensure_num_regions=True, is_keep_ratio=True):
        """
        place multiple text on a image
        :param text_list: list of str
        :param font_list: list of font file path
        :param region_list: list of regions, a region is a list of 4 corners in x-y coord
        :param effect_list: list of effects, a effect is a dict, please refer to self._place_text_on_region()
        :param is_top_left_origin: whether keep the first point as the top-left corner in a region vector
        :param ensure_num_regions: whether ensure the number of text placed on the image
        :param is_keep_ratio: whether keep the ratio of rendered text
        :return: RGB image with text on it, and annotations, i.e., bounding box, character box, baseline, and true text
        """

        n_text, n_font = len(text_list), len(font_list)
        # assert 1 <= n_text <= 10
        self.list_text = []
        self.list_bound = []
        self.list_char_box = []
        self.list_base_line = []

        """ get regions """
        if region_list is None:
            region_list = self._get_text_regions(
                num_regions=n_text, is_top_left_origin=is_top_left_origin, ensure_num_regions=ensure_num_regions)
            """ match text length to regains """
            sorted_idx_region = np.argsort([max(r[:, 0]) - min(r[:, 0]) for r in region_list])
            sorted_idx_text = np.argsort([len(t) for t in text_list])
        else:
            if not len(region_list) == n_text:
                logging.info('Mismatched length between region_list and text_list')
                return None
            """ match text length to regains """
            sorted_idx_region = np.arange(n_text)
            sorted_idx_text = np.arange(n_text)

        """ place text on each region """
        if effect_list is not None:
            if not len(effect_list) == n_text:
                logging.info('Mismatched length between effect_list and text_list')
                return None
        for idx in range(len(region_list)):
            if self._place_text_on_region(
                    text=text_list[sorted_idx_text[idx]],
                    font=font_list[idx % n_font],  # font[np.random.randint(len(font))],
                    region=np.array(region_list[sorted_idx_region[idx]]),
                    effects=effect_list[sorted_idx_text[idx]] if effect_list is not None else None,
                    is_keep_ratio=is_keep_ratio):
                self.list_text.append(self.text)
                self.list_bound.append(self.bound_quadrilateral)
                self.list_char_box.append(self.char_box)
                self.list_base_line.append(self.base_line)

        """ prepare return data """
        return {
            'image': self.image.astype(np.uint8),  # synthesize image with text
            'text': self.list_text,  # list of text placed on the image
            'wbb': self.list_bound,  # list of bounding quadrilateral
            'cbb': self.list_char_box,  # list of character-wise bounding quadrilateral
            'base': self.list_base_line,  # list of baseline
            'mask': self.text_mask
        }

        place multiple text on a image
        :param text_list: list of str
        :param font_list: list of font file path
        :param region_list: list of regions, a region is a list of 4 corners in x-y coord
        :param effect_list: list of effects, a effect is a dict, please refer to self._place_text_on_region()
        :param is_top_left_origin: whether keep the first point as the top-left corner in a region vector
        :param ensure_num_regions: whether ensure the number of text placed on the image
        :param is_keep_ratio: whether keep the ratio of rendered text
        :return: RGB image with text on it, and annotations, i.e., bounding box, character box, baseline, and true text
        
place multiple text on a image
place multiple text on a image
def _place_text_on_image(self, text_list, font_list, region_list=None, effect_list=None,
                             is_top_left_origin=True, ensure_num_regions=True, is_keep_ratio=True):
        n_text, n_font = len(text_list), len(font_list)
        self.list_text = []
        self.list_bound = []
        self.list_char_box = []
        self.list_base_line = []
        if region_list is None:
            region_list = self._get_text_regions(
                num_regions=n_text, is_top_left_origin=is_top_left_origin, ensure_num_regions=ensure_num_regions)
            sorted_idx_region = np.argsort([max(r[:, 0]) - min(r[:, 0]) for r in region_list])
            sorted_idx_text = np.argsort([len(t) for t in text_list])
        else:
            if not len(region_list) == n_text:
                logging.info('Mismatched length between region_list and text_list')
                return None
            sorted_idx_region = np.arange(n_text)
            sorted_idx_text = np.arange(n_text)
        if effect_list is not None:
            if not len(effect_list) == n_text:
                logging.info('Mismatched length between effect_list and text_list')
                return None
        for idx in range(len(region_list)):
            if self._place_text_on_region(
                    text=text_list[sorted_idx_text[idx]],
                    font=font_list[idx % n_font],  
                    region=np.array(region_list[sorted_idx_region[idx]]),
                    effects=effect_list[sorted_idx_text[idx]] if effect_list is not None else None,
                    is_keep_ratio=is_keep_ratio):
                self.list_text.append(self.text)
                self.list_bound.append(self.bound_quadrilateral)
                self.list_char_box.append(self.char_box)
                self.list_base_line.append(self.base_line)
        return {
            'image': self.image.astype(np.uint8),  
            'text': self.list_text,  
            'wbb': self.list_bound,  
            'cbb': self.list_char_box,  
            'base': self.list_base_line,  
            'mask': self.text_mask
        }
def _place_text_on_image ( self , text_list , font_list , region_list = None , effect_list = None , is_top_left_origin = True , ensure_num_regions = True , is_keep_ratio = True ) : n_text , n_font = len ( text_list ) , len ( font_list ) self . list_text = [ ] self . list_bound = [ ] self . list_char_box = [ ] self . list_base_line = [ ] """ get regions """ if region_list is None : region_list = self . _get_text_regions ( num_regions = n_text , is_top_left_origin = is_top_left_origin , ensure_num_regions = ensure_num_regions ) """ match text length to regains """ sorted_idx_region = np . argsort ( [ max ( r [ : , 0 ] ) - min ( r [ : , 0 ] ) for r in region_list ] ) sorted_idx_text = np . argsort ( [ len ( t ) for t in text_list ] ) else : if not len ( region_list ) == n_text : logging . info ( 'Mismatched length between region_list and text_list' ) return None """ match text length to regains """ sorted_idx_region = np . arange ( n_text ) sorted_idx_text = np . arange ( n_text ) """ place text on each region """ if effect_list is not None : if not len ( effect_list ) == n_text : logging . info ( 'Mismatched length between effect_list and text_list' ) return None for idx in range ( len ( region_list ) ) : if self . _place_text_on_region ( text = text_list [ sorted_idx_text [ idx ] ] , font = font_list [ idx % n_font ] , region = np . array ( region_list [ sorted_idx_region [ idx ] ] ) , effects = effect_list [ sorted_idx_text [ idx ] ] if effect_list is not None else None , is_keep_ratio = is_keep_ratio ) : self . list_text . append ( self . text ) self . list_bound . append ( self . bound_quadrilateral ) self . list_char_box . append ( self . char_box ) self . list_base_line . append ( self . base_line ) """ prepare return data """ return { 'image' : self . image . astype ( np . uint8 ) , 'text' : self . list_text , 'wbb' : self . list_bound , 'cbb' : self . list_char_box , 'base' : self . list_base_line , 'mask' : self . text_mask }
place multiple text on a image
place multiple text on a image
"""
        place multiple text on a image
        :param text_list: list of str
        :param font_list: list of font file path
        :param region_list: list of regions, a region is a list of 4 corners in x-y coord
        :param effect_list: list of effects, a effect is a dict, please refer to self._place_text_on_region()
        :param is_top_left_origin: whether keep the first point as the top-left corner in a region vector
        :param ensure_num_regions: whether ensure the number of text placed on the image
        :param is_keep_ratio: whether keep the ratio of rendered text
        :return: RGB image with text on it, and annotations, i.e., bounding box, character box, baseline, and true text
        """ # assert 1 <= n_text <= 10 """ get regions """ """ match text length to regains """ """ match text length to regains """ """ place text on each region """ # font[np.random.randint(len(font))], """ prepare return data """ # synthesize image with text # list of text placed on the image # list of bounding quadrilateral # list of character-wise bounding quadrilateral # list of baseline
RGB image with text on it, and annotations, i.e., bounding box, character box, baseline, and true text
RGB image with text on it and annotations i . e . bounding box character box baseline and true text
self
text_list
list of str
list of str
font_list
list of font file path
list of font file path
region_list
list of regions, a region is a list of 4 corners in x-y coord
list of regions a region is a list of 4 corners in x - y coord
effect_list
list of effects, a effect is a dict, please refer to self._place_text_on_region()
list of effects a effect is a dict please refer to self . _place_text_on_region ()
is_top_left_origin
whether keep the first point as the top-left corner in a region vector
whether keep the first point as the top - left corner in a region vector
ensure_num_regions
whether ensure the number of text placed on the image
whether ensure the number of text placed on the image
is_keep_ratio
whether keep the ratio of rendered text
whether keep the ratio of rendered text