Loading ballontranslator/dl/textdetector/textblock.py +7 −6 Original line number Diff line number Diff line Loading @@ -35,14 +35,14 @@ class TextBlock(object): bold: bool = False, underline: bool = False, italic: bool = False, alignment: int = -1, _alignment: int = -1, alpha: float = 255, rich_text: str = "", _bounding_rect: List = None, accumulate_color = True, default_stroke_width = 0.2, font_weight = 50, target_lang: str = "", _target_lang: str = "", **kwargs) -> None: self.xyxy = [int(num) for num in xyxy] # boundingbox of textblock self.lines = [] if lines is None else lines # polygons of textlines Loading Loading @@ -78,9 +78,8 @@ class TextBlock(object): self.alpha = alpha self.rich_text = rich_text self.line_spacing = line_spacing # self.alignment = alignment self._alignment = alignment self._target_lang = target_lang self._alignment = _alignment self._target_lang = _target_lang self._bounding_rect = _bounding_rect self.default_stroke_width = default_stroke_width Loading Loading @@ -240,7 +239,7 @@ class TextBlock(object): return 0 lines = self.lines_array() if len(lines) == 1: return 0 return 1 angled = self.angle != 0 polygons = lines.reshape(-1, 8) if angled: Loading Loading @@ -330,6 +329,8 @@ def examine_textblk(blk: TextBlock, im_w: int, im_h: int, eval_orientation: bool blk.angle = rotation_angle if vertical: blk.angle -= 90 if abs(blk.angle) < 3: blk.angle = 0 blk.font_size = font_size if eval_orientation: blk.vertical = vertical Loading ballontranslator/ui/configpanel.py +7 −1 Original line number Diff line number Diff line Loading @@ -347,8 +347,11 @@ class ConfigPanel(Widget): self.let_fntsize_combox.currentIndexChanged.connect(self.on_fntsize_flag_changed) self.let_fntstroke_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('stroke'), target_block=letblk_0) self.let_fntstroke_combox.currentIndexChanged.connect(self.on_fntstroke_flag_changed) self.let_fntcolor_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font & stroke color')) self.let_fntcolor_combox, letblk_1 = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font & stroke color')) self.let_fntcolor_combox.currentIndexChanged.connect(self.on_fontcolor_flag_changed) self.let_alignment_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('alignment'), target_block=letblk_1) self.let_alignment_combox.currentIndexChanged.connect(self.on_alignment_flag_changed) self.let_autolayout_checker = generalConfigPanel.addCheckBox(self.tr('Auto layout'), discription=self.tr('Split translation into multi-lines according to the extracted balloon region. The font size will be adaptively resized if it is set to \"decide by program.\"')) self.let_autolayout_checker.stateChanged.connect(self.on_autolayout_changed) Loading Loading @@ -401,6 +404,9 @@ class ConfigPanel(Widget): def on_fontcolor_flag_changed(self): self.config.let_fntcolor_flag = self.let_fntcolor_combox.currentIndex() def on_alignment_flag_changed(self): self.config.let_alignment_flag = self.let_alignment_combox.currentIndex() def on_source_flag_changed(self): self.config.src_choice_flag = self.src_choice_combox.currentIndex() Loading ballontranslator/ui/mainwindow.py +23 −18 Original line number Diff line number Diff line Loading @@ -172,6 +172,7 @@ class MainWindow(QMainWindow): self.configPanel.let_fntsize_combox.setCurrentIndex(self.config.let_fntsize_flag) self.configPanel.let_fntstroke_combox.setCurrentIndex(self.config.let_fntstroke_flag) self.configPanel.let_fntcolor_combox.setCurrentIndex(self.config.let_fntcolor_flag) self.configPanel.let_alignment_combox.setCurrentIndex(self.config.let_alignment_flag) self.configPanel.let_autolayout_checker.setChecked(self.config.let_autolayout_flag) self.configPanel.let_uppercase_checker.setChecked(self.config.let_uppercase_flag) self.configPanel.save_config.connect(self.save_config) Loading Loading @@ -469,12 +470,17 @@ class MainWindow(QMainWindow): self.pageListCurrentItemChanged() def on_pagtrans_finished(self, page_index: int): if self.config.dl.translate_target not in LANG_SUPPORT_VERTICAL: for blk in self.imgtrans_proj.get_blklist_byidx(page_index): if blk.vertical: blk._alignment = 1 blk.vertical = False # override font format if necessary override_fnt_size = self.config.let_fntsize_flag == 1 override_fnt_stroke = self.config.let_fntstroke_flag == 1 override_fnt_color = self.config.let_fntcolor_flag if override_fnt_size or override_fnt_stroke: override_fnt_color = self.config.let_fntcolor_flag == 1 override_alignment = self.config.let_alignment_flag == 1 gf = self.textPanel.formatpanel.global_format blk_list = self.imgtrans_proj.get_blklist_byidx(page_index) for blk in blk_list: Loading @@ -484,17 +490,16 @@ class MainWindow(QMainWindow): blk.default_stroke_width = gf.stroke_width if override_fnt_color: blk.set_font_colors(gf.frgb, gf.srgb, accumulate=False) if self.config.dl.translate_target not in LANG_SUPPORT_VERTICAL: for blk in self.imgtrans_proj.get_blklist_byidx(page_index): blk.vertical = False if override_alignment: blk._alignment = gf.alignment blk.line_spacing = gf.line_spacing self.st_manager.auto_textlayout_flag = self.config.let_autolayout_flag if page_index != 0: if page_index != self.pageList.currentIndex().row(): self.pageList.setCurrentRow(page_index) else: self.imgtrans_proj.set_current_img_byidx(0) self.imgtrans_proj.set_current_img_byidx(page_index) self.canvas.updateCanvas() self.st_manager.updateSceneTextitems() self.saveCurrentPage(False, False) Loading ballontranslator/ui/misc.py +2 −0 Original line number Diff line number Diff line Loading @@ -456,6 +456,7 @@ class ProgramConfig: let_fntsize_flag: int = 0, let_fntstroke_flag: int = 0, let_fntcolor_flag: int = 0, let_alignment_flag: int = 0, let_autolayout_flag: bool = True, let_uppercase_flag: bool = True) -> None: Loading Loading @@ -487,6 +488,7 @@ class ProgramConfig: self.let_fntsize_flag = let_fntsize_flag self.let_fntstroke_flag = let_fntstroke_flag self.let_fntcolor_flag = let_fntcolor_flag self.let_alignment_flag = let_alignment_flag self.let_autolayout_flag = let_autolayout_flag self.let_uppercase_flag = let_uppercase_flag Loading ballontranslator/ui/scenetext_manager.py +12 −13 Original line number Diff line number Diff line Loading @@ -447,7 +447,7 @@ class SceneTextManager(QObject): if mask is None: bounding_rect = blkitem.absBoundingRect() enlarge_ratio = min(max(bounding_rect[2] / bounding_rect[3], bounding_rect[3] / bounding_rect[2]), 3.0) enlarge_ratio = min(max(bounding_rect[2] / bounding_rect[3], bounding_rect[3] / bounding_rect[2]) * 1.5, 3) mask, ballon_area, mask_xyxy, region_rect = extract_ballon_region(img, bounding_rect, enlarge_ratio=enlarge_ratio, cal_region_rect=True) else: mask_xyxy = [bounding_rect[0], bounding_rect[1], bounding_rect[0]+bounding_rect[2], bounding_rect[1]+bounding_rect[3]] Loading @@ -468,11 +468,8 @@ class SceneTextManager(QObject): line_height = int(round(fmt.line_spacing * h)) delimiter_len = text_size_func(delimiter)[0] font_scale_ratio = 1.0 # # if self.auto_textlayout_flag: # if self.config.let_fntsize_flag == 0 and text: adaptive_fntsize = False if self.auto_textlayout_flag and self.config.let_fntsize_flag == 0: adaptive_fntsize = True if adaptive_fntsize: area_ratio = ballon_area / (w * h) Loading @@ -488,12 +485,14 @@ class SceneTextManager(QObject): line_height = int(line_height * resize_ratio) delimiter_len = int(delimiter_len * resize_ratio) padding = pt2px(blk_font.pointSize()) // 4 # dummpy padding variable padding = pt2px(blk_font.pointSize()) + 20 # dummpy padding variable new_text, xywh = layout_text(mask, mask_xyxy, region_rect, words, wl_list, delimiter, delimiter_len, blkitem.blk.angle, line_height, fmt.alignment, fmt.vertical, padding) # font size post adjustment if adaptive_fntsize: downscale_constraint = 0.7 post_resize_ratio = max(region_rect[2] / xywh[2], downscale_constraint) downscale_constraint = 0.6 w = xywh[2] - padding * 2 post_resize_ratio = max(region_rect[2] / w, downscale_constraint) if post_resize_ratio < 1: resize_ratio *= post_resize_ratio cx, cy = xywh[0] + xywh[2] / 2, xywh[1] + xywh[3] / 2 Loading @@ -504,7 +503,6 @@ class SceneTextManager(QObject): blkitem.textCursor().clearSelection() set_textblk_fontsize(blkitem, new_font_size) scale = blkitem.scale() if scale != 1: xywh = (np.array(xywh, np.float64) * blkitem.scale()).astype(np.int32).tolist() Loading Loading @@ -635,7 +633,8 @@ class SceneTextManager(QObject): def get_text_size(fm: QFontMetrics, text: str) -> Tuple[int, int]: brt = fm.tightBoundingRect(text) return brt.width(), brt.height() br = fm.boundingRect(text) return br.width(), brt.height() def get_words_length_list(fm: QFontMetrics, words: List[str]) -> List[int]: return [fm.tightBoundingRect(word).width() for word in words] No newline at end of file Loading
ballontranslator/dl/textdetector/textblock.py +7 −6 Original line number Diff line number Diff line Loading @@ -35,14 +35,14 @@ class TextBlock(object): bold: bool = False, underline: bool = False, italic: bool = False, alignment: int = -1, _alignment: int = -1, alpha: float = 255, rich_text: str = "", _bounding_rect: List = None, accumulate_color = True, default_stroke_width = 0.2, font_weight = 50, target_lang: str = "", _target_lang: str = "", **kwargs) -> None: self.xyxy = [int(num) for num in xyxy] # boundingbox of textblock self.lines = [] if lines is None else lines # polygons of textlines Loading Loading @@ -78,9 +78,8 @@ class TextBlock(object): self.alpha = alpha self.rich_text = rich_text self.line_spacing = line_spacing # self.alignment = alignment self._alignment = alignment self._target_lang = target_lang self._alignment = _alignment self._target_lang = _target_lang self._bounding_rect = _bounding_rect self.default_stroke_width = default_stroke_width Loading Loading @@ -240,7 +239,7 @@ class TextBlock(object): return 0 lines = self.lines_array() if len(lines) == 1: return 0 return 1 angled = self.angle != 0 polygons = lines.reshape(-1, 8) if angled: Loading Loading @@ -330,6 +329,8 @@ def examine_textblk(blk: TextBlock, im_w: int, im_h: int, eval_orientation: bool blk.angle = rotation_angle if vertical: blk.angle -= 90 if abs(blk.angle) < 3: blk.angle = 0 blk.font_size = font_size if eval_orientation: blk.vertical = vertical Loading
ballontranslator/ui/configpanel.py +7 −1 Original line number Diff line number Diff line Loading @@ -347,8 +347,11 @@ class ConfigPanel(Widget): self.let_fntsize_combox.currentIndexChanged.connect(self.on_fntsize_flag_changed) self.let_fntstroke_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('stroke'), target_block=letblk_0) self.let_fntstroke_combox.currentIndexChanged.connect(self.on_fntstroke_flag_changed) self.let_fntcolor_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font & stroke color')) self.let_fntcolor_combox, letblk_1 = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font & stroke color')) self.let_fntcolor_combox.currentIndexChanged.connect(self.on_fontcolor_flag_changed) self.let_alignment_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('alignment'), target_block=letblk_1) self.let_alignment_combox.currentIndexChanged.connect(self.on_alignment_flag_changed) self.let_autolayout_checker = generalConfigPanel.addCheckBox(self.tr('Auto layout'), discription=self.tr('Split translation into multi-lines according to the extracted balloon region. The font size will be adaptively resized if it is set to \"decide by program.\"')) self.let_autolayout_checker.stateChanged.connect(self.on_autolayout_changed) Loading Loading @@ -401,6 +404,9 @@ class ConfigPanel(Widget): def on_fontcolor_flag_changed(self): self.config.let_fntcolor_flag = self.let_fntcolor_combox.currentIndex() def on_alignment_flag_changed(self): self.config.let_alignment_flag = self.let_alignment_combox.currentIndex() def on_source_flag_changed(self): self.config.src_choice_flag = self.src_choice_combox.currentIndex() Loading
ballontranslator/ui/mainwindow.py +23 −18 Original line number Diff line number Diff line Loading @@ -172,6 +172,7 @@ class MainWindow(QMainWindow): self.configPanel.let_fntsize_combox.setCurrentIndex(self.config.let_fntsize_flag) self.configPanel.let_fntstroke_combox.setCurrentIndex(self.config.let_fntstroke_flag) self.configPanel.let_fntcolor_combox.setCurrentIndex(self.config.let_fntcolor_flag) self.configPanel.let_alignment_combox.setCurrentIndex(self.config.let_alignment_flag) self.configPanel.let_autolayout_checker.setChecked(self.config.let_autolayout_flag) self.configPanel.let_uppercase_checker.setChecked(self.config.let_uppercase_flag) self.configPanel.save_config.connect(self.save_config) Loading Loading @@ -469,12 +470,17 @@ class MainWindow(QMainWindow): self.pageListCurrentItemChanged() def on_pagtrans_finished(self, page_index: int): if self.config.dl.translate_target not in LANG_SUPPORT_VERTICAL: for blk in self.imgtrans_proj.get_blklist_byidx(page_index): if blk.vertical: blk._alignment = 1 blk.vertical = False # override font format if necessary override_fnt_size = self.config.let_fntsize_flag == 1 override_fnt_stroke = self.config.let_fntstroke_flag == 1 override_fnt_color = self.config.let_fntcolor_flag if override_fnt_size or override_fnt_stroke: override_fnt_color = self.config.let_fntcolor_flag == 1 override_alignment = self.config.let_alignment_flag == 1 gf = self.textPanel.formatpanel.global_format blk_list = self.imgtrans_proj.get_blklist_byidx(page_index) for blk in blk_list: Loading @@ -484,17 +490,16 @@ class MainWindow(QMainWindow): blk.default_stroke_width = gf.stroke_width if override_fnt_color: blk.set_font_colors(gf.frgb, gf.srgb, accumulate=False) if self.config.dl.translate_target not in LANG_SUPPORT_VERTICAL: for blk in self.imgtrans_proj.get_blklist_byidx(page_index): blk.vertical = False if override_alignment: blk._alignment = gf.alignment blk.line_spacing = gf.line_spacing self.st_manager.auto_textlayout_flag = self.config.let_autolayout_flag if page_index != 0: if page_index != self.pageList.currentIndex().row(): self.pageList.setCurrentRow(page_index) else: self.imgtrans_proj.set_current_img_byidx(0) self.imgtrans_proj.set_current_img_byidx(page_index) self.canvas.updateCanvas() self.st_manager.updateSceneTextitems() self.saveCurrentPage(False, False) Loading
ballontranslator/ui/misc.py +2 −0 Original line number Diff line number Diff line Loading @@ -456,6 +456,7 @@ class ProgramConfig: let_fntsize_flag: int = 0, let_fntstroke_flag: int = 0, let_fntcolor_flag: int = 0, let_alignment_flag: int = 0, let_autolayout_flag: bool = True, let_uppercase_flag: bool = True) -> None: Loading Loading @@ -487,6 +488,7 @@ class ProgramConfig: self.let_fntsize_flag = let_fntsize_flag self.let_fntstroke_flag = let_fntstroke_flag self.let_fntcolor_flag = let_fntcolor_flag self.let_alignment_flag = let_alignment_flag self.let_autolayout_flag = let_autolayout_flag self.let_uppercase_flag = let_uppercase_flag Loading
ballontranslator/ui/scenetext_manager.py +12 −13 Original line number Diff line number Diff line Loading @@ -447,7 +447,7 @@ class SceneTextManager(QObject): if mask is None: bounding_rect = blkitem.absBoundingRect() enlarge_ratio = min(max(bounding_rect[2] / bounding_rect[3], bounding_rect[3] / bounding_rect[2]), 3.0) enlarge_ratio = min(max(bounding_rect[2] / bounding_rect[3], bounding_rect[3] / bounding_rect[2]) * 1.5, 3) mask, ballon_area, mask_xyxy, region_rect = extract_ballon_region(img, bounding_rect, enlarge_ratio=enlarge_ratio, cal_region_rect=True) else: mask_xyxy = [bounding_rect[0], bounding_rect[1], bounding_rect[0]+bounding_rect[2], bounding_rect[1]+bounding_rect[3]] Loading @@ -468,11 +468,8 @@ class SceneTextManager(QObject): line_height = int(round(fmt.line_spacing * h)) delimiter_len = text_size_func(delimiter)[0] font_scale_ratio = 1.0 # # if self.auto_textlayout_flag: # if self.config.let_fntsize_flag == 0 and text: adaptive_fntsize = False if self.auto_textlayout_flag and self.config.let_fntsize_flag == 0: adaptive_fntsize = True if adaptive_fntsize: area_ratio = ballon_area / (w * h) Loading @@ -488,12 +485,14 @@ class SceneTextManager(QObject): line_height = int(line_height * resize_ratio) delimiter_len = int(delimiter_len * resize_ratio) padding = pt2px(blk_font.pointSize()) // 4 # dummpy padding variable padding = pt2px(blk_font.pointSize()) + 20 # dummpy padding variable new_text, xywh = layout_text(mask, mask_xyxy, region_rect, words, wl_list, delimiter, delimiter_len, blkitem.blk.angle, line_height, fmt.alignment, fmt.vertical, padding) # font size post adjustment if adaptive_fntsize: downscale_constraint = 0.7 post_resize_ratio = max(region_rect[2] / xywh[2], downscale_constraint) downscale_constraint = 0.6 w = xywh[2] - padding * 2 post_resize_ratio = max(region_rect[2] / w, downscale_constraint) if post_resize_ratio < 1: resize_ratio *= post_resize_ratio cx, cy = xywh[0] + xywh[2] / 2, xywh[1] + xywh[3] / 2 Loading @@ -504,7 +503,6 @@ class SceneTextManager(QObject): blkitem.textCursor().clearSelection() set_textblk_fontsize(blkitem, new_font_size) scale = blkitem.scale() if scale != 1: xywh = (np.array(xywh, np.float64) * blkitem.scale()).astype(np.int32).tolist() Loading Loading @@ -635,7 +633,8 @@ class SceneTextManager(QObject): def get_text_size(fm: QFontMetrics, text: str) -> Tuple[int, int]: brt = fm.tightBoundingRect(text) return brt.width(), brt.height() br = fm.boundingRect(text) return br.width(), brt.height() def get_words_length_list(fm: QFontMetrics, words: List[str]) -> List[int]: return [fm.tightBoundingRect(word).width() for word in words] No newline at end of file