beacon_aug/external/textflow/synthesis.py
def _get_text_regions(self, num_regions=5, is_top_left_origin=True, ensure_num_regions=False, check_collision=True):
        """
        get smooth quadrilateral regions for placing text
        :param n_regions: int, number of regions required, 1 to 10
        :param is_top_left_origin: whether keep the first point in a region vector as the top-left corner
        :param ensure_num_regions: sometimes, there are not enough suitable regions for placing text
            True: force to get num_regions regions
            False: only return suitable regions, may less than num_regions
        :param check_collision: bool, whether check collision of regions
        :return: list, [4x2 array, ...], each item denotes four corners of a quadrilateral region
        """

        def top_left_origin(pts):
            dist, pts_new_order = [], []
            n_pts = len(pts)
            for pt in pts:
                dist.append(np.linalg.norm(pt))
            idx_top_left = int(np.argmin(dist))
            for idx in range(idx_top_left, idx_top_left + n_pts):
                pts_new_order.append(pts[idx % n_pts])
            return np.array(pts_new_order)

        # assert isinstance(num_regions, (int, float)) and 1 <= num_regions <= 30
        num_regions = round(num_regions)

        """ constraints """
        min_seg_img_area_ratio = .01
        min_seg_bbox_area_ratio = .6

        """ segmentation """
        h, w = self.image.shape[:2]
        n_segments = min(max(1, int(h * w / 128. ** 2)), 10)
        segments, labels, areas = TextSynthesis.image_segmentation(
            self.image, n_segments=n_segments)

        """ get smooth regions """
        # TODO: region_filter

        """ define quadrilateral for text """
        indices = np.arange(len(labels)).astype(np.int32)
        np.random.shuffle(indices)
        text_regions = []

        h, w = self.image.shape[:2]
        collision_mask = np.zeros((h, w))
        trash_regions = []
        cnt = 0
        for idx in indices:
            if cnt >= num_regions:
                break
            if float(areas[idx]) / (h * w) < min_seg_img_area_ratio:
                continue

            """ min bounding rectangle """
            ys, xs = np.where(segments == labels[idx])
            coords = np.c_[xs, ys].astype('float32')
            # hull = cv2.convexHull(coords, clockwise=False, returnPoints=True)
            # hull = np.array(hull).squeeze()
            rect = cv2.minAreaRect(coords)
            if float(areas[idx]) / (rect[1][0] * rect[1][1]) < min_seg_bbox_area_ratio:
                continue
            box = np.array(cv2.boxPoints(rect))

            """ shrink the rectangle  """
            # mask_center = np.array([np.mean(xs), np.mean(ys)])
            # shift = np.abs(mask_center - np.array(rect[0]))
            # rect = (tuple(mask_center), tuple((np.array(rect[1]) - shift)), rect[-1])

            """ perspective transformation according to depth info """
            # TODO:

            """ fit inside of the image """
            box[box < 0] = 0
            box[box[:, 0] > w - 1, 0] = w - 1
            box[box[:, 1] > h - 1, 1] = h - 1

            """ check collision """
            if check_collision:
                mask = cv2.drawContours(
                    np.zeros((h, w)), [box.astype(np.int32)], 0, 1, thickness=cv2.FILLED)
                if np.sum(mask * collision_mask):
                    # shrink
                    continue
                else:
                    collision_mask += mask
            else:
                collision_mask = cv2.drawContours(
                    collision_mask, [box.astype(np.int32)], 0, 1, thickness=cv2.FILLED)

            """ arrange the corners to keep the first corner is the top-left corner """
            if is_top_left_origin:
                box = top_left_origin(box)

            if rect[1][0] * rect[1][1] / float(h * w) < min_seg_img_area_ratio:
                trash_regions.append([box, rect[1][0] * rect[1][1]])
                continue

            text_regions.append(box)
            cnt += 1

        if cnt < num_regions and ensure_num_regions:
            trash_regions = sorted(trash_regions, key=lambda x: x[-1], reverse=True)
            for r in trash_regions:
                text_regions.append(r[0])
                cnt += 1
                if cnt >= num_regions:
                    break

        return text_regions

        get smooth quadrilateral regions for placing text
        :param n_regions: int, number of regions required, 1 to 10
        :param is_top_left_origin: whether keep the first point in a region vector as the top-left corner
        :param ensure_num_regions: sometimes, there are not enough suitable regions for placing text
            True: force to get num_regions regions
            False: only return suitable regions, may less than num_regions
        :param check_collision: bool, whether check collision of regions
        :return: list, [4x2 array, ...], each item denotes four corners of a quadrilateral region
        
get smooth quadrilateral regions for placing text
get smooth quadrilateral regions for placing text
def _get_text_regions(self, num_regions=5, is_top_left_origin=True, ensure_num_regions=False, check_collision=True):
        def top_left_origin(pts):
            dist, pts_new_order = [], []
            n_pts = len(pts)
            for pt in pts:
                dist.append(np.linalg.norm(pt))
            idx_top_left = int(np.argmin(dist))
            for idx in range(idx_top_left, idx_top_left + n_pts):
                pts_new_order.append(pts[idx % n_pts])
            return np.array(pts_new_order)
        num_regions = round(num_regions)
        min_seg_img_area_ratio = .01
        min_seg_bbox_area_ratio = .6
        h, w = self.image.shape[:2]
        n_segments = min(max(1, int(h * w / 128. ** 2)), 10)
        segments, labels, areas = TextSynthesis.image_segmentation(
            self.image, n_segments=n_segments)
        indices = np.arange(len(labels)).astype(np.int32)
        np.random.shuffle(indices)
        text_regions = []
        h, w = self.image.shape[:2]
        collision_mask = np.zeros((h, w))
        trash_regions = []
        cnt = 0
        for idx in indices:
            if cnt >= num_regions:
                break
            if float(areas[idx]) / (h * w) < min_seg_img_area_ratio:
                continue
            ys, xs = np.where(segments == labels[idx])
            coords = np.c_[xs, ys].astype('float32')
            rect = cv2.minAreaRect(coords)
            if float(areas[idx]) / (rect[1][0] * rect[1][1]) < min_seg_bbox_area_ratio:
                continue
            box = np.array(cv2.boxPoints(rect))
            box[box < 0] = 0
            box[box[:, 0] > w - 1, 0] = w - 1
            box[box[:, 1] > h - 1, 1] = h - 1
            if check_collision:
                mask = cv2.drawContours(
                    np.zeros((h, w)), [box.astype(np.int32)], 0, 1, thickness=cv2.FILLED)
                if np.sum(mask * collision_mask):
                    continue
                else:
                    collision_mask += mask
            else:
                collision_mask = cv2.drawContours(
                    collision_mask, [box.astype(np.int32)], 0, 1, thickness=cv2.FILLED)
            if is_top_left_origin:
                box = top_left_origin(box)
            if rect[1][0] * rect[1][1] / float(h * w) < min_seg_img_area_ratio:
                trash_regions.append([box, rect[1][0] * rect[1][1]])
                continue
            text_regions.append(box)
            cnt += 1
        if cnt < num_regions and ensure_num_regions:
            trash_regions = sorted(trash_regions, key=lambda x: x[-1], reverse=True)
            for r in trash_regions:
                text_regions.append(r[0])
                cnt += 1
                if cnt >= num_regions:
                    break
        return text_regions
def _get_text_regions ( self , num_regions = 5 , is_top_left_origin = True , ensure_num_regions = False , check_collision = True ) : def top_left_origin ( pts ) : dist , pts_new_order = [ ] , [ ] n_pts = len ( pts ) for pt in pts : dist . append ( np . linalg . norm ( pt ) ) idx_top_left = int ( np . argmin ( dist ) ) for idx in range ( idx_top_left , idx_top_left + n_pts ) : pts_new_order . append ( pts [ idx % n_pts ] ) return np . array ( pts_new_order ) num_regions = round ( num_regions ) """ constraints """ min_seg_img_area_ratio = .01 min_seg_bbox_area_ratio = .6 """ segmentation """ h , w = self . image . shape [ : 2 ] n_segments = min ( max ( 1 , int ( h * w / 128. ** 2 ) ) , 10 ) segments , labels , areas = TextSynthesis . image_segmentation ( self . image , n_segments = n_segments ) """ get smooth regions """ """ define quadrilateral for text """ indices = np . arange ( len ( labels ) ) . astype ( np . int32 ) np . random . shuffle ( indices ) text_regions = [ ] h , w = self . image . shape [ : 2 ] collision_mask = np . zeros ( ( h , w ) ) trash_regions = [ ] cnt = 0 for idx in indices : if cnt >= num_regions : break if float ( areas [ idx ] ) / ( h * w ) < min_seg_img_area_ratio : continue """ min bounding rectangle """ ys , xs = np . where ( segments == labels [ idx ] ) coords = np . c_ [ xs , ys ] . astype ( 'float32' ) rect = cv2 . minAreaRect ( coords ) if float ( areas [ idx ] ) / ( rect [ 1 ] [ 0 ] * rect [ 1 ] [ 1 ] ) < min_seg_bbox_area_ratio : continue box = np . array ( cv2 . boxPoints ( rect ) ) """ shrink the rectangle  """ """ perspective transformation according to depth info """ """ fit inside of the image """ box [ box < 0 ] = 0 box [ box [ : , 0 ] > w - 1 , 0 ] = w - 1 box [ box [ : , 1 ] > h - 1 , 1 ] = h - 1 """ check collision """ if check_collision : mask = cv2 . drawContours ( np . zeros ( ( h , w ) ) , [ box . astype ( np . int32 ) ] , 0 , 1 , thickness = cv2 . FILLED ) if np . sum ( mask * collision_mask ) : continue else : collision_mask += mask else : collision_mask = cv2 . drawContours ( collision_mask , [ box . astype ( np . int32 ) ] , 0 , 1 , thickness = cv2 . FILLED ) """ arrange the corners to keep the first corner is the top-left corner """ if is_top_left_origin : box = top_left_origin ( box ) if rect [ 1 ] [ 0 ] * rect [ 1 ] [ 1 ] / float ( h * w ) < min_seg_img_area_ratio : trash_regions . append ( [ box , rect [ 1 ] [ 0 ] * rect [ 1 ] [ 1 ] ] ) continue text_regions . append ( box ) cnt += 1 if cnt < num_regions and ensure_num_regions : trash_regions = sorted ( trash_regions , key = lambda x : x [ - 1 ] , reverse = True ) for r in trash_regions : text_regions . append ( r [ 0 ] ) cnt += 1 if cnt >= num_regions : break return text_regions
get smooth quadrilateral regions for placing text
get smooth quadrilateral regions for placing text
"""
        get smooth quadrilateral regions for placing text
        :param n_regions: int, number of regions required, 1 to 10
        :param is_top_left_origin: whether keep the first point in a region vector as the top-left corner
        :param ensure_num_regions: sometimes, there are not enough suitable regions for placing text
            True: force to get num_regions regions
            False: only return suitable regions, may less than num_regions
        :param check_collision: bool, whether check collision of regions
        :return: list, [4x2 array, ...], each item denotes four corners of a quadrilateral region
        """ # assert isinstance(num_regions, (int, float)) and 1 <= num_regions <= 30 """ constraints """ """ segmentation """ """ get smooth regions """ # TODO: region_filter """ define quadrilateral for text """ """ min bounding rectangle """ # hull = cv2.convexHull(coords, clockwise=False, returnPoints=True) # hull = np.array(hull).squeeze() """ shrink the rectangle  """ # mask_center = np.array([np.mean(xs), np.mean(ys)]) # shift = np.abs(mask_center - np.array(rect[0])) # rect = (tuple(mask_center), tuple((np.array(rect[1]) - shift)), rect[-1]) """ perspective transformation according to depth info """ # TODO: """ fit inside of the image """ """ check collision """ # shrink """ arrange the corners to keep the first corner is the top-left corner """
list, [4x2 array, ...], each item denotes four corners of a quadrilateral region
list [ 4x2 array ... ] each item denotes four corners of a quadrilateral region
self
num_regions
is_top_left_origin
whether keep the first point in a region vector as the top-left corner
whether keep the first point in a region vector as the top - left corner
ensure_num_regions
sometimes, there are not enough suitable regions for placing text
True: force to get num_regions regions
False: only return suitable regions, may less than num_regions
sometimes there are not enough suitable regions for placing text True : force to get num_regions regions False : only return suitable regions may less than num_regions
check_collision
bool, whether check collision of regions
bool whether check collision of regions
n_regions
int, number of regions required, 1 to 10
int number of regions required 1 to 10