def apply_crop(self, text_box_pnts, crop_cfg): """ Random crop text box height top or bottom, we don't need image information in this step, only change box pnts :param text_box_pnts: bbox of text [left-top, right-top, right-bottom, left-bottom] :param crop_cfg: :return: croped_text_box_pnts """ height = abs(text_box_pnts[0][1] - text_box_pnts[3][1]) scale = float(height) / float(self.out_height) croped_text_box_pnts = text_box_pnts if prob(0.5): top_crop = int(random.randint(crop_cfg.top.min, crop_cfg.top.max) * scale) self.dmsg("top crop %d" % top_crop) croped_text_box_pnts[0][1] += top_crop croped_text_box_pnts[1][1] += top_crop else: bottom_crop = int(random.randint(crop_cfg.bottom.min, crop_cfg.bottom.max) * scale) self.dmsg("bottom crop %d " % bottom_crop) croped_text_box_pnts[2][1] -= bottom_crop croped_text_box_pnts[3][1] -= bottom_crop return croped_text_box_pnts
def gen_img(self): word, font, word_size = self.pick_font() # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, char_pnts, text_box_pnts, word_color = self.draw_text_on_bg_char_pts( word, font, bg) if self.cfg.line.enable and prob(self.cfg.line.fraction): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) word_img, img_pnts_transformed, char_pnts_transformed = \ self.apply_perspective_transform(word_img, char_pnts, max_x=self.cfg.perspective_transform.max_x, max_y=self.cfg.perspective_transform.max_y, max_z=self.cfg.perspective_transform.max_z, gpu=self.gpu) if self.debug: word_img = draw_box(word_img, img_pnts_transformed, (0, 255, 0)) word_img = draw_box(word_img, char_pnts_transformed, (0, 0, 255)) # _, crop_bbox = self.crop_img(word_img, char_pnts_transformed) # word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, char_pnts_transformed = self.crop_img( word_img, char_pnts_transformed) if self.cfg.noise.enable and prob(self.cfg.noise.fraction): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) blured = False if self.cfg.blur.enable and prob(self.cfg.blur.fraction): blured = True word_img = self.apply_blur_on_output(word_img) if not blured: if self.cfg.prydown.enable and prob(self.cfg.prydown.fraction): word_img = self.apply_prydown(word_img) word_img = np.clip(word_img, 0., 255.) return word_img, word, char_pnts_transformed
def gen_img(self): word = self.corpus.get_sample() font, word_size = self.pick_font(word) # Background's height should much larger than raw word image's height, # to make sure we can crop full word image after apply perspective bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) word_img, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg) if self.texteffect.line and prob(self.textstate.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=25, max_y=25, max_z=5, gpu=self.gpu) if self.debug: word_img = draw_box(word_img, img_pnts_transformed, (0, 255, 0)) word_img = draw_box(word_img, text_box_pnts_transformed, (0, 0, 255)) _, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) word_img = draw_bbox(word_img, crop_bbox, (255, 0, 0)) else: word_img, crop_bbox = self.crop_img(word_img, text_box_pnts_transformed) if self.texteffect.noise and prob(self.textstate.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) blured = False if self.texteffect.blur and prob(self.textstate.blur): blured = True word_img = self.apply_blur_on_output(word_img) if not blured: if self.texteffect.prydown and prob(self.textstate.prydown): word_img = self.apply_prydown(word_img) word_img = np.clip(word_img, 0., 255.) return word_img, word
def get_sample(self): if self.char_freq_counted(): if prob(BalanceCorpus.LESS_CHAR_FRACTION): # 找到低频词所在的位置,从这个位置截取文字段 key = random.choice(self.less_chars_index_keys) index = random.choice(self.less_chars_index[key]) num_prefix_chars = random.choice(range(0, self.length)) start = max(index - num_prefix_chars, 0) else: start = np.random.randint(0, len(self.corpus) - self.length) else: start = np.random.randint(0, len(self.corpus) - self.length) word = self.corpus[start:start + self.length] return word
def apply_blur_on_output(self, img): if prob(0.5): return self.apply_gauss_blur(img, [3, 5]) else: return self.apply_norm_blur(img)
def gen_bg(self, width, height): if prob(0.5): bg = self.gen_rand_bg(int(width), int(height)) else: bg = self.gen_bg_from_image(int(width), int(height)) return bg
def apply_under_line(self, word_img, text_box_pnts, word_color,word,font,): y_offset = random.choice([1,2,3,4,5,6]) text_box_pnts[2][1] += y_offset text_box_pnts[3][1] += y_offset #print(word, font) line_color = word_color + random.randint(0, 10) # plt.figure('befor line') # plt.imshow(word_img) # plt.show() #if prob(0.5): #0.5的概率是虚线下划线 leftBottomX, leftBottomY = text_box_pnts[3][0], text_box_pnts[3][1] rightBottomX, rightBottomY = text_box_pnts[2][0], text_box_pnts[2][1] #divded = len(word) chars_size = [] leftBottomX_tmp = leftBottomX if prob(0): #0.3概率为部分虚线或者实线下划线 word_split = re.split('\.|,|。|?|!|\n| |;|、|;|":|。"|,"|!"',word) if len(word_split)>0: sub_word_index_list_in_word = [] for i in range(np.random.randint(len(word_split))): # 随机挑选n个word a = np.random.randint(len(word_split)) #选取n个word的index sub_word_index_list_in_word_sub =[(i.start(),i.end())for i in re.finditer(word_split[a],word)] sub_index = np.random.randint(len(sub_word_index_list_in_word_sub)) sub_word_index_list_in_word.append(sub_word_index_list_in_word_sub[sub_index]) # if a not in random_word_index_list: # random_word_index_list.append(a) # else: # continue thickness = np.random.randint(1, 3) for i in range(len(word)): size = font.getsize(word[i]) chars_size.append(size) if word[i]!= ' ': if len(sub_word_index_list_in_word)!=0: for index in sub_word_index_list_in_word: if i >index[0]-1 and i <index[1]+1: if prob(0): #部分虚线 draw_leftBottomX_tmp = leftBottomX_tmp+2 draw_rightBottomX_tmp = leftBottomX_tmp + size[0] - 2 else: draw_leftBottomX_tmp = leftBottomX_tmp draw_rightBottomX_tmp = leftBottomX_tmp + size[0] dst = cv2.line(word_img, (draw_leftBottomX_tmp , leftBottomY ), (draw_rightBottomX_tmp, leftBottomY ), color=line_color, thickness=thickness, lineType=cv2.LINE_AA) leftBottomX_tmp = leftBottomX_tmp + size[0] # print(leftBottomX_tmp) # plt.figure('after line') # plt.imshow(word_img) # plt.show() else: #全部虚线或者实线下划线 thickness = np.random.randint(1, 3) for i in range(len(word)): size = font.getsize(word[i]) chars_size.append(size) # print(size) # xStep = (rightBottomX - leftBottomX) // divded # yStep = (rightBottomY - leftBottomY) // divded # for i in range(0, divded - 1): # print('draw cor',leftBottomX_tmp,leftBottomY) if prob(0):#0.5概率是全部虚线 draw_leftBottomX_tmp = leftBottomX_tmp + 2 draw_rightBottomX_tmp = leftBottomX_tmp + size[0] - 2 if word[i] != ' ': dst = cv2.line(word_img, (draw_leftBottomX_tmp, leftBottomY), (draw_rightBottomX_tmp, leftBottomY), color=line_color, thickness=thickness, lineType=cv2.LINE_AA) else: #全部实线 draw_leftBottomX_tmp = leftBottomX_tmp draw_rightBottomX_tmp = leftBottomX_tmp + size[0] dst = cv2.line(word_img, (draw_leftBottomX_tmp, leftBottomY), (draw_rightBottomX_tmp, leftBottomY), color=line_color, thickness=thickness, lineType=cv2.LINE_AA) leftBottomX_tmp = leftBottomX_tmp + size[0] # plt.figure('22222') # plt.imshow(word_img) # plt.show() return dst, text_box_pnts
def apply_blur_on_output(self, img): # 我这里分别进行测试。 if prob(0.5): return self.apply_gauss_blur(img) else: return self.apply_norm_blur(img)