Exemplo n.º 1
0
 def __init__(self, data_dir, max_time=None):
     self.text_renderer = tu.RenderFont(data_dir)
     self.colorizer = Colorize(data_dir)
     self.min_char_height = 8  # px
     self.min_asp_ratio = 0.4  #
     self.max_text_regions = 7
     self.max_time = max_time
Exemplo n.º 2
0
    def __init__(self, data_dir, max_time=None, text_number=1, font_color_user = None, font_size_user = 18, text_content_user = "******") :
        self.text_renderer = tu.RenderFont( data_dir, font_size_user, text_content_user)
        self.colorizer = Colorize(data_dir, font_color= font_color_user)
        self.NUM_REP = text_number

        self.min_char_height = 8 #px
        self.min_asp_ratio = 0.4 #
        self.max_text_regions = 7
        self.max_time = max_time
Exemplo n.º 3
0
    def __init__(self, data_dir, max_time=None):
        self.text_renderer = tu.RenderFont(data_dir)
        self.colorizer = Colorize(data_dir)
        #self.colorizerV2 = colorV2.Colorize(data_dir)

        self.min_char_height = 8#8 #px
        self.min_asp_ratio = 0.4#0.4 #

        self.max_text_regions = 1#Rohit changed from 7 to 1

        self.max_time = max_time
Exemplo n.º 4
0
    def RenderWordImageA(self,
                         Height,
                         Width,
                         LightMap,
                         mask,
                         WordColor=(255, 255, 255, 200),
                         SaveID=None):
        """
        While True:
            img0 = render single word
            imgs.append(img0)
            if ar < target ar: break
            if random.random() < 0.2 break
        pad
        output
        :param Height:
        :param Width:
        :param WordColor:
        :return: Img[HxWx4], Texts(List[str]), CBOX[nx4x2], BBOX[nx4x2] (N_box, corner, x/y)
        """
        #LightMap_float = LightMap.astype(np.float)
        UL_x, UL_y, BR_x, BR_y = mask
        bg_arr = LightMap[UL_y:BR_y,
                          UL_x:BR_x, :]  #cv2.bitwise_and(LightMap, mask)#
        #bg_img = np.zeros((bg_arr.shape[0],bg_arr.shape[1],4))
        #bg_img[:,:,:3] = LightMap
        #bg_img[:,:,3] = (bg_arr[:,:,0]>0).astype(np.float32)
        FontSize = self.getFontSize(Height, Width)
        if FontSize > min(Height, Width):
            FontSize //= 2
        self._RandomLoadFont(FontSize)
        TextCrops = []
        CurrentHeight = 0
        MaxWidth = 0
        while True:
            CharImg, texts, text_joint, cbox, bbox = self.RenderStraightTextLine(
                Width, SpacingFactor=np.random.uniform(0, 0.4))
            if CharImg is None:
                FontSize = self.getFontSize(Height, Width)
                self._RandomLoadFont(FontSize)
                continue
            TextCrops.append([CharImg, texts, text_joint, cbox, bbox])
            try:
                CurrentHeight += CharImg.shape[0]
            except:
                TextCrops = TextCrops[:-1]
                continue
            MaxWidth = max(MaxWidth, CharImg.shape[1])
            if CurrentHeight > Height * 1.2 and len(TextCrops) > 1:
                CurrentHeight -= TextCrops[-1][0].shape[0]
                TextCrops = TextCrops[:-1]
                break
            if CurrentHeight > Height:
                break
            if len(TextCrops) > 5:
                break

        Color = np.array(
            [[[WordColor[0], WordColor[1], WordColor[2], WordColor[3]]]],
            dtype=np.uint8)

        Img = np.zeros(shape=[
            max(CurrentHeight, int(random.uniform(1.0, 1.05) * Height)),
            max(MaxWidth, Width), 4
        ],
                       dtype=np.uint8)
        Texts = []
        BBOX = []
        CBOX = []
        currentheight = 0
        try:
            H, W, _ = Img.shape
            for CharImg, texts, text_joint, cbox, bbox in TextCrops:
                h, w, _ = CharImg.shape
                left_pad = random.randint(0, W - w)
                if currentheight + h > H: break
                Img[currentheight:currentheight + h,
                    left_pad:w + left_pad] = CharImg
                Texts += texts
                bbox[:, ::2] += left_pad
                cbox[:, ::2] += left_pad
                BBOX.append(bbox)
                CBOX.append(cbox)
                BBOX[-1][:, 1::2] += currentheight
                CBOX[-1][:, 1::2] += currentheight
                if random.random() < 0.5:
                    currentheight += int(h * random.uniform(1.0, 1.5))
                else:
                    currentheight += h
            BBOX = np.concatenate(BBOX, axis=0)
            CBOX = np.concatenate(CBOX, axis=0)

        except:
            pdb.set_trace()

        if random.random() < 0.5:
            Color = Color * np.random.uniform(0.85, 1.0, size=Img.shape)
        Img = (Img / 255 * Color).astype(np.uint8)
        #text_mask = Img[:,:,3]
        #print(text_mask)
        #plt.imshow(text_mask,cmap='gray')
        #plt.show()
        CBOX = np.reshape(np.stack([
            CBOX[:, 0], CBOX[:, 1], CBOX[:, 2], CBOX[:, 1], CBOX[:, 2],
            CBOX[:, 3], CBOX[:, 0], CBOX[:, 3]
        ],
                                   axis=-1),
                          newshape=[-1, 4, 2])
        BBOX = np.reshape(np.stack([
            BBOX[:, 0], BBOX[:, 1], BBOX[:, 2], BBOX[:, 1], BBOX[:, 2],
            BBOX[:, 3], BBOX[:, 0], BBOX[:, 3]
        ],
                                   axis=-1),
                          newshape=[-1, 4, 2])
        text_mask = Img[:, :, 3]

        print(bg_arr.shape, text_mask.shape)
        bg_img_ = cv2.resize(bg_arr[:, :, :3],
                             (text_mask.shape[1], text_mask.shape[0]))
        #plt.imshow(cv2.cvtColor(bg_arr,cv2.COLOR_BGR2RGB))
        #plt.show()
        Colorizer = Colorize()
        Img = Colorizer.process(text_mask, bg_img_, LightMap, FontSize)

        #Img = Colorizer.color(bg_img,text_mask,np.array([FontSize]))
        if SaveID is not None:
            path = osp.join(self.ContentPath, f'word-{SaveID}.png')

            cv2.imwrite(path, Img)
            H, W, _ = Img.shape
            CBOX = CBOX.astype(np.float)
            BBOX = BBOX.astype(np.float)
            CBOX[:, :, 0] /= W
            CBOX[:, :, 1] /= H
            BBOX[:, :, 0] /= W
            BBOX[:, :, 1] /= H
            return path, Texts, CBOX, BBOX, W, H

        return Img, Texts, CBOX, BBOX
Exemplo n.º 5
0
if __name__ == '__main__':
    # testing:
    Renderer = WordRenderer(is_debug=True, Language="Vietnamese")
    for i in (range(40)):
        print(f"{i}--------")
        # Img[HxWx4], Texts(List[str]), CBOX[nx4x2], BBOX[nx4x2] (N_box, corner, x/y)
        bg_img = cv2.imread("img_2.jpg")
        img, texts, cbox, bbox = Renderer.RenderWordImageA(
            660, 1000, (30, 30, 30, 240))
        text_mask = img[:, :, 3]

        print(text_mask.shape)
        print(bg_img.shape)
        bg_img = cv2.resize(bg_img, (text_mask.shape[1], text_mask.shape[0]))
        Colorizer = Colorize()
        img = Colorizer.color(bg_img, np.array([text_mask]), np.array([30]))
        print(texts)
        # print(img.shape)

        # visualize boxes
        img_b = img.copy()
        for box in bbox:
            img_b[box[0, 1]:box[1, 1] + 2,
                  box[0, 0]:box[1, 0] + 2] = [255, 0, 0, 255]
            img_b[box[2, 1]:box[3, 1] + 2,
                  box[3, 0]:box[2, 0] + 2] = [0, 255, 0, 255]
            img_b[box[0, 1]:box[3, 1] + 2,
                  box[0, 0]:box[3, 0] + 2] = [255, 0, 255, 255]
            img_b[box[1, 1]:box[2, 1] + 2,
                  box[1, 0]:box[2, 0] + 2] = [0, 255, 255, 255]