Commit 60a10f57 authored by dmMaze's avatar dmMaze
Browse files

fix img find & page flip bugs

parent c8a4f2aa
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -305,12 +305,7 @@ class DeeplTranslator(TranslatorBase):

    concate_text = True
    setup_params: Dict = {
        'api_key': '', 
        'device': {
            'type': 'selector',
            'options': ['cpu', 'cuda'],
            'select': 'cpu'
        }
        'api_key': ''
    }

    def _setup_translator(self):
+1 −4
Original line number Diff line number Diff line
@@ -728,6 +728,3 @@ class DLManager(QObject):
    def on_inpainter_checker_changed(self, is_checked: bool):
        self.dl_config.check_need_inpaint = is_checked
        InpainterBase.check_need_inpaint = is_checked
 No newline at end of file
        

    
+12 −4
Original line number Diff line number Diff line
@@ -174,7 +174,7 @@ class MainWindow(QMainWindow):
        self.dl_manager.page_trans_finished.connect(self.on_pagtrans_finished)
        self.dl_manager.progress_msgbox.showed.connect(self.on_imgtrans_progressbox_showed)

        self.leftBar.run_imgtrans.connect(dl_manager.runImgtransPipeline)
        self.leftBar.run_imgtrans.connect(self.on_run_imgtrans)
        self.bottomBar.ocrcheck_statechanged.connect(dl_manager.setOCRMode)
        self.bottomBar.transcheck_statechanged.connect(dl_manager.setTransMode)
        self.bottomBar.translatorStatusbtn.clicked.connect(self.translatorStatusBtnPressed)
@@ -387,9 +387,10 @@ class MainWindow(QMainWindow):
            
        img = QImage(self.canvas.imgLayer.pixmap().size(), QImage.Format.Format_ARGB32)

        restore_textblock_mode = False
        if self.config.imgtrans_textblock:
            self.bottomBar.textblockChecker.setChecked(False)
            self.setTextBlockMode()
            restore_textblock_mode = True
            self.bottomBar.textblockChecker.click()

        hide_tsc = False
        if self.st_manager.txtblkShapeControl.isVisible():
@@ -406,6 +407,8 @@ class MainWindow(QMainWindow):
        imsave_path = self.imgtrans_proj.get_result_path(self.imgtrans_proj.current_img)
        self.imsave_thread.saveImg(imsave_path, img)
            
        if restore_textblock_mode:
            self.bottomBar.textblockChecker.click()
        if hide_tsc:
            self.st_manager.txtblkShapeControl.show()
        self.canvas.setProjSaveState(False)
@@ -473,3 +476,8 @@ class MainWindow(QMainWindow):
            mb.exec()
            return
        self.close()

    def on_run_imgtrans(self):
        if self.bottomBar.textblockChecker.isChecked():
            self.bottomBar.textblockChecker.click()
        self.dl_manager.runImgtransPipeline()
 No newline at end of file
+3 −130
Original line number Diff line number Diff line
import json, re, cv2, glob
import json, os, cv2
import os.path as osp
import numpy as np
from pathlib import Path
@@ -22,17 +22,14 @@ class NumpyEncoder(json.JSONEncoder):
                return int(obj)
        return json.JSONEncoder.default(self, obj)



def find_all_imgs(img_dir, abs_path=False):
    imglist = []
    for filep in glob.glob(osp.join(img_dir, "*")):
        filename = osp.basename(filep)
    for filename in os.listdir(img_dir):
        file_suffix = Path(filename).suffix
        if file_suffix.lower() not in IMG_EXT:
            continue
        if abs_path:
            imglist.append(filep)
            imglist.append(osp.join(img_dir, filename))
        else:
            imglist.append(filename)
    return imglist
@@ -53,42 +50,11 @@ def imwrite(img_path, img, ext='.png'):
        img_path += ext
    cv2.imencode(ext, img)[1].tofile(img_path)


def show_img_by_dict(imgdicts):
    for keyname in imgdicts.keys():
        cv2.imshow(keyname, imgdicts[keyname])
    cv2.waitKey(0)

widths = [
  (126,  1), (159,  0), (687,   1), (710,  0), (711,  1),
  (727,  0), (733,  1), (879,   0), (1154, 1), (1161, 0),
  (4347,  1), (4447,  2), (7467,  1), (7521, 0), (8369, 1),
  (8426,  0), (9000,  1), (9002,  2), (11021, 1), (12350, 2),
  (12351, 1), (12438, 2), (12442,  0), (19893, 2), (19967, 1),
  (55203, 2), (63743, 1), (64106,  2), (65039, 1), (65059, 0),
  (65131, 2), (65279, 1), (65376,  2), (65500, 1), (65510, 2),
  (120831, 1), (262141, 2), (1114109, 1),
]
def get_width(o):
    global widths
    o = ord(o)

    if o == 0xe or o == 0xf:
        return 0
    for num, wid in widths:
        if o <= num:
            return wid
    return 1

def get_text_width(text):
    '''get text width'''
    # text = text.encode("utf-8")
    # text = text.decode("utf-8")
    width = 0
    for t in text:
        width += get_width(t) 
    return width

def text_is_empty(text) -> bool:
    if isinstance(text, str):
        if text.strip() == '':
@@ -102,96 +68,3 @@ def text_is_empty(text) -> bool:
    elif text is None:
        return True
def bubdict_template(source_lang="en",
                     target_lang="chi",
                     seg_mode=0, 
                    #  inpaint_type=0, 
                     default_font_size=15,
                     default_font_family=-1,
                     vert_text=False,
                     line_spacing=85):
    template_dict = {  
                     "source_lang": source_lang,
                     "target_lang": target_lang,
                     "seg_mode": seg_mode,
                     "inner_rect": [-1, -1, -1, -1, -1],
                     "xywh": [-1, -1, -1, -1, -1],
                    #  "inpaint_type": inpaint_type,
                     "bgr": [0, 0, 0],
                     "bground_bgr":[255,255,255],
                     "translation": "",
                     "ocr": "",
                     "default_font_size": default_font_size,
                     "default_font_family": default_font_family,
                     "vert_text": vert_text,
                     "alignment": 0,
                     "line_spacing": line_spacing}
    return template_dict


# def gen_projconfig(proj_path):
#     config_path = osp.join(proj_path, osp.basename(proj_path) + "_proj.json")
#     imglist = find_all_imgs(proj_path)
    
#     config = {"stage":0, "new_img_num":0, "img_info":{}, "all_boxes":{}, "mask_ext":".bmp", "result_ext": ".png"}
#     for imgp in imglist:
#         config["img_info"][imgp] = {}

#     format_save(config_path, config)
#     return config




def load_proj_dict(proj_dict_path, check_img_update=True):
    proj_dict_path = proj_dict_path.replace("/", "\\")
    proj_path = proj_dict_path[:proj_dict_path.rfind("\\")]
    imglist = find_all_imgs(proj_path)
    assert osp.exists(proj_dict_path)
    with open(proj_dict_path, "r", encoding="utf-8") as f:
        proj_dict = json.loads(f.read())
        img_info_dict = proj_dict["img_info"]
        img_names = list(img_info_dict.keys())
        for name in img_names:
            if osp.exists(osp.join(proj_path, name)) == False:
                proj_dict["img_info"].pop(name)
        if check_img_update:
            proj_dict["new_img_num"] = 0
            for imgl in imglist:
                if imgl in img_names:
                    continue
                proj_dict["img_info"][imgl] = {}
                proj_dict["new_img_num"] += 1
    # else:
    #     proj_dict = gen_projconfig(proj_path)

    format_save(proj_dict_path, proj_dict)
    return proj_dict
        
def format_save(config_path, config_dict):
    with open(config_path, "w", encoding="utf-8") as f:
        f.write(json.dumps(config_dict, ensure_ascii=False, indent=4, separators=(',', ':'), cls=TextBlkEncoder))
    # with open(config_path, "r", encoding="utf-8") as f:
    #     lines = f.readlines()
    #     wr = ""
    #     skip_ln, lrst = False, 0
    #     for l in lines:
    #         if re.findall("\[", l):
    #             skip_ln, lrst = True, -1
    #         if re.findall("\]", l):
    #             skip_ln = False
    #             l = l.lstrip()
    #         if skip_ln:
    #             l = l.replace("\n", "")
    #             if lrst == 0:
    #                 l = l.lstrip()
    #             wr += l
    #             if lrst == -1:
    #                 lrst = 0
    #         else: wr += l

    # with open(config_path, "w", encoding="utf-8") as f:
    #     f.write(wr)