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, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg) if apply(self.cfg.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=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, 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 apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) return word_img, word
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) if self.space_ratio > 0 and len(word) > 2: word = list(word) for i in range(1, len(word) - 1): if word[i-1] != ' ' and random.random() < self.space_ratio: # 不允许出现连续的空格 word[i] = ' ' word = ''.join(word) self.dmsg("after 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, text_box_pnts, word_color = self.draw_text_on_bg(word, font, bg) self.dmsg("After draw_text_on_bg") if apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply(word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_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) self.dmsg("After perspective transform") if self.debug: _, 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) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") # word_img = cv2.resize(word_img, None, fx=0.5, fy=0.5) return word_img, word
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) self.dmsg("after 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, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg) self.dmsg("After draw_text_on_bg") if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_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) self.dmsg("After perspective transform") if self.debug: _, 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) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") return word_img, word
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) self.dmsg("选择字体后") # 背景的高度应该远远大于原始文字图像的高度, # 以确保我们可以裁剪完整的字图像后,应用透视 bg = self.gen_bg(width=word_size[0] * 8, height=word_size[1] * 8) # 在背景图上写字,text_box_pnts,文字位置 word_img, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg) self.dmsg("将文字渲染到背景图完成") if apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("将线条渲染完成") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("重新映射完成") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_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) self.dmsg("角度变换完成") if self.debug: _, 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) self.dmsg("裁切图片完成") # self.cfg配置参数 if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("噪点处理完毕") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("模糊处理完毕") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("prydown处理完毕") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("反转颜色处理完毕") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("浮雕处理完毕") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("锐化处理完毕") return word_img, word
def gen_img(self, img_index, inv_alph_dict): # not blur image, like real image is_pass = random.random() < 0.2 while True: # check word is valid # limit font size to avoid to scale word img word, font, word_size = self.pick_font(img_index, 25 if is_pass else None) # delete invalid word invalid = False for c in word: if c not in inv_alph_dict: invalid = True break if not invalid: break if self.space_ratio > 0 and len(word) > 2: word = list(word) for i in range(1, len(word) - 1): if word[i-1] != ' ' and random.random() < self.space_ratio: # 不允许出现连续的空格 word[i] = ' ' word = ''.join(word) self.dmsg("after 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, text_box_pnts, word_color = self.draw_text_on_bg(word, font, bg, is_pass) self.dmsg("After draw_text_on_bg") if not is_pass and apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply(word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) if is_pass: # avoid to rotation img # the font size is 25 word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_pnts, max_x=0.01, max_y=0.01, max_z=0.01, gpu=self.gpu) else: word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_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) self.dmsg("After perspective transform") if self.debug: _, 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) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if not is_pass: if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if not is_pass and apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if not is_pass and apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if not is_pass and apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") # word_img = cv2.resize(word_img, None, fx=0.5, fy=0.5) word_img = word_img.astype(np.uint8) return word_img, word
def gen_img(self, img_index): # print(0, time.time() - self.start) word, font, word_size, font2, font_size = self.pick_font(img_index) # print(1, time.time() - self.start) self.dmsg("after 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, text_box_pnts, word_color = self.draw_text_on_bg( word, font, bg, font2, font_size) self.dmsg("After draw_text_on_bg") # print(2, time.time() - self.start) if apply(self.cfg.crop): text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) # print(3, time.time() - self.start) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts) self.dmsg("After draw line") # print(4, time.time() - self.start) if self.debug: word_img = draw_box(word_img, text_box_pnts, (0, 255, 155)) # print(5, time.time() - self.start) if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("After remapping") # print(6, time.time() - self.start) if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) # print(7, time.time() - self.start) word_img = self.mix_seamless_bg(word_img, bg) if apply(self.cfg.extra_words): word_img = self.draw_extra_random_word( word_img, text_box_pnts, img_index) self.dmsg("After add extra words") word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_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) self.dmsg("After perspective transform") # print(8, time.time() - self.start) if self.debug: _, 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) self.dmsg("After crop_img") # print(9, time.time() - self.start) if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") # print(10, time.time() - self.start) if apply(self.cfg.blur): word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") word_img = np.clip(word_img, 0., 255.) # print(11, time.time() - self.start) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") # print(12, time.time() - self.start) if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") # print(13, time.time() - self.start) if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") return word_img, word
def gen_img(self, img_index): word, font, word_size = self.pick_font(img_index) self.dmsg("after pick font") #_________________________________Change_Spaces______________________________________________________ word = self.gen_word() r = random.randint(0, 3) if len(word) == 8: pass else: if r == 0: word += ' ' * rnd.randint(1, 16) else: pass # 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, piece_widths = self.draw_text_on_bg( word, font, bg) self.dmsg("After draw_text_on_bg") if True: text_box_pnts = self.apply_crop(text_box_pnts, self.cfg.crop) if apply(self.cfg.line): word_img, text_box_pnts = self.liner.apply(word_img, text_box_pnts, word_color) self.dmsg("After draw line") #if self.debug: #___________________________________Change________________________________________________________ if True: word_img = draw_box(word_img, text_box_pnts, (0, 0, 0)) # Drawing boxes x_init, y = text_box_pnts[0][0], text_box_pnts[0][1] w_init, h = text_box_pnts[2][0] - x_init, text_box_pnts[2][1] - y x = x_init #piece_widths[-1] = w_init for i in range(len(word)): w = piece_widths[i] + x_init bbox_points = [x, y, w, h] word_img = draw_bbox(word_img, bbox_points, (0, 0, 0)) x = w if apply(self.cfg.curve): word_img, text_box_pnts = self.remaper.apply( word_img, text_box_pnts, word_color) self.dmsg("After remapping") if self.debug: word_img = draw_box(word_img, text_box_pnts, (155, 255, 0)) word_img, img_pnts_transformed, text_box_pnts_transformed = \ self.apply_perspective_transform(word_img, text_box_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) self.dmsg("After perspective transform") if self.debug: _, 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) self.dmsg("After crop_img") if apply(self.cfg.noise): word_img = np.clip(word_img, 0., 255.) word_img = self.noiser.apply(word_img) self.dmsg("After noiser") blured = False if apply(self.cfg.blur): blured = True word_img = self.apply_blur_on_output(word_img) self.dmsg("After blur") if not blured: if apply(self.cfg.prydown): word_img = self.apply_prydown(word_img) self.dmsg("After prydown") word_img = np.clip(word_img, 0., 255.) if apply(self.cfg.reverse_color): word_img = self.reverse_img(word_img) self.dmsg("After reverse_img") if apply(self.cfg.emboss): word_img = self.apply_emboss(word_img) self.dmsg("After emboss") if apply(self.cfg.sharp): word_img = self.apply_sharp(word_img) self.dmsg("After sharp") return word_img, word