Loading modules/ocr/ocr_stariver.py +20 −11 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ import base64 from typing import List from .base import register_OCR, OCRBase, TextBlock from utils.error_handling import create_error_dialog, create_info_dialog @register_OCR('stariver_ocr') Loading @@ -13,10 +14,6 @@ class OCRStariver(OCRBase): params = { 'User': "填入你的用户名", 'Password': "填入你的密码。请注意,密码会明文保存,请勿在公共电脑上使用", 'force_refresh_token': { 'type': 'checkbox', 'value': False }, "refine":{ 'type': 'checkbox', 'value': True Loading @@ -40,6 +37,12 @@ class OCRStariver(OCRBase): 'type': 'checkbox', 'value': False, }, 'update_token_btn': { 'type': 'pushbtn', 'value': '', 'description': '删除旧 Token 并重新申请', 'display_name': '更新 Token' }, 'description': '星河云(团子翻译器) OCR API' } Loading @@ -51,10 +54,6 @@ class OCRStariver(OCRBase): def Password(self): return self.params['Password'] @property def force_refresh_token(self): return self.params['force_refresh_token']['value'] @property def expand_ratio(self): return float(self.params['expand_ratio']) Loading Loading @@ -100,7 +99,8 @@ class OCRStariver(OCRBase): "Password": self.Password }).json() if response.get('Status', -1) != "Success": self.logger.error(f'stariver ocr 登录失败,错误信息:{response.get("ErrorMsg", "")}') error_msg = f'stariver ocr 登录失败,错误信息:{response.get("ErrorMsg", "")}' raise Exception(error_msg) token = response.get('Token', '') if token != '': self.logger.info(f'登录成功,token前10位:{token[:10]}') Loading Loading @@ -167,6 +167,7 @@ class OCRStariver(OCRBase): return texts_str def update_token_if_needed(self): token_updated = False if (self.User != self.register_username or self.Password != self.register_password): if self.token_obtained == False: Loading @@ -179,11 +180,19 @@ class OCRStariver(OCRBase): self.register_password = self.Password self.token_obtained = True self.logger.info("Token updated due to credential change.") token_updated = True return token_updated def updateParam(self, param_key: str, param_content): super().updateParam(param_key, param_content) if param_key == 'force_refresh_token': if param_key == 'update_token_btn': self.token_obtained = False # 强制刷新token时,将标志位设置为False self.token = '' # 强制刷新token时,将token置空 self.register_username = None # 强制刷新token时,将用户名置空 self.register_password = None # 强制刷新token时,将密码置空 try: if self.update_token_if_needed(): create_info_dialog('Token 更新成功') except Exception as e: create_error_dialog(e, 'Token 更新失败', 'TokenUpdateFailed') No newline at end of file modules/textdetector/detector_stariver.py +19 −11 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ import requests import base64 from .base import register_textdetectors, TextDetectorBase, TextBlock from utils.error_handling import create_error_dialog, create_info_dialog @register_textdetectors('stariver_ocr') Loading @@ -13,10 +14,6 @@ class StariverDetector(TextDetectorBase): params = { 'User': "填入你的用户名", 'Password': "填入你的密码。请注意,密码会明文保存,请勿在公共电脑上使用", 'force_refresh_token': { 'type': 'checkbox', 'value': False }, 'expand_ratio': "0.01", "refine": { 'type': 'checkbox', Loading @@ -43,6 +40,12 @@ class StariverDetector(TextDetectorBase): "font_size_offset": "0", "font_size_min(set to -1 to disable)": "-1", "font_size_max(set to -1 to disable)": "-1", 'update_token_btn': { 'type': 'pushbtn', 'value': '', 'description': '删除旧 Token 并重新申请', 'display_name': '更新 Token' }, 'description': '星河云(团子翻译器) OCR 文字检测器' } Loading @@ -54,10 +57,6 @@ class StariverDetector(TextDetectorBase): def Password(self): return self.params['Password'] @property def force_refresh_token(self): return self.params['force_refresh_token']['value'] @property def expand_ratio(self): return float(self.params['expand_ratio']) Loading Loading @@ -118,8 +117,8 @@ class StariverDetector(TextDetectorBase): "Password": self.Password }).json() if response.get('Status', -1) != "Success": self.logger.error( f'stariver detector 登录失败,错误信息:{response.get("ErrorMsg", "")}') error_msg = f'stariver ocr 登录失败,错误信息:{response.get("ErrorMsg", "")}' raise Exception(error_msg) token = response.get('Token', '') if token != '': self.logger.info(f'stariver detector 登录成功,token前10位:{token[:10]}') Loading Loading @@ -297,6 +296,7 @@ class StariverDetector(TextDetectorBase): return dilated_mask def update_token_if_needed(self): token_updated = False if (self.User != self.register_username or self.Password != self.register_password): if self.token_obtained == False: Loading @@ -309,11 +309,19 @@ class StariverDetector(TextDetectorBase): self.register_password = self.Password self.token_obtained = True self.logger.info("Token updated due to credential change.") token_updated = True return token_updated def updateParam(self, param_key: str, param_content): super().updateParam(param_key, param_content) if param_key == 'force_refresh_token': if param_key == 'update_token_btn': self.token_obtained = False # 强制刷新token时,将标志位设置为False self.token = '' # 强制刷新token时,将token置空 self.register_username = None # 强制刷新token时,将用户名置空 self.register_password = None # 强制刷新token时,将密码置空 try: if self.update_token_if_needed(): create_info_dialog('Token 更新成功') except Exception as e: create_error_dialog(e, 'Token 更新失败', 'TokenUpdateFailed') ui/mainwindow.py +9 −1 Original line number Diff line number Diff line Loading @@ -66,12 +66,15 @@ class MainWindow(mainwindow_cls): restart_signal = Signal() create_errdialog = Signal(str, str, str) create_infodialog = Signal(str, str) def __init__(self, app: QApplication, config: ProgramConfig, open_dir='', **exec_args) -> None: super().__init__() shared.create_errdialog_in_mainthread = self.create_errdialog.emit self.create_errdialog.connect(self.on_create_errdialog) shared.create_infodialog_in_mainthread = self.create_infodialog.emit self.create_infodialog.connect(self.on_create_infodialog) self.app = app self.setupThread() Loading Loading @@ -1238,3 +1241,8 @@ class MainWindow(mainwindow_cls): shared.showed_exception.remove(exception_type) LOGGER.error('Failed to create error dialog') LOGGER.error(traceback.format_exc()) def on_create_infodialog(self, msg: str, btn_name: str): dialog = QMessageBox() dialog.setText(msg) dialog.exec() No newline at end of file ui/module_parse_widgets.py +33 −4 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ from .stylewidgets import ConfigComboBox, NoBorderPushBtn, CustomComboBox from utils.shared import CONFIG_FONTSIZE_CONTENT, CONFIG_COMBOBOX_MIDEAN, CONFIG_COMBOBOX_LONG, CONFIG_COMBOBOX_SHORT, CONFIG_COMBOBOX_HEIGHT from utils.config import pcfg from qtpy.QtWidgets import QPlainTextEdit, QHBoxLayout, QVBoxLayout, QWidget, QLabel, QComboBox, QCheckBox, QLineEdit, QGridLayout from qtpy.QtWidgets import QPlainTextEdit, QHBoxLayout, QVBoxLayout, QWidget, QLabel, QComboBox, QCheckBox, QLineEdit, QGridLayout, QPushButton from qtpy.QtCore import Qt, Signal from qtpy.QtGui import QFontMetricsF, QDoubleValidator Loading Loading @@ -117,6 +117,25 @@ class ParamCheckBox(QCheckBox): self.paramwidget_edited.emit(self.param_key, self.isChecked()) def get_param_display_name(param_key: str, param_dict: dict = None): if param_dict is not None and isinstance(param_dict, dict): if 'display_name' in param_dict: return param_dict['display_name'] return param_key class ParamPushButton(QPushButton): paramwidget_edited = Signal(str, str) def __init__(self, param_key: str, param_dict: dict = None, *args, **kwargs): super().__init__(*args, **kwargs) self.param_key = param_key self.setText(get_param_display_name(param_key, param_dict)) self.clicked.connect(self.on_clicked) def on_clicked(self): self.paramwidget_edited.emit(self.param_key, '') class ParamWidget(QWidget): paramwidget_edited = Signal(str, dict) Loading @@ -136,8 +155,9 @@ class ParamWidget(QWidget): for ii, param_key in enumerate(params): if param_key == 'description': continue display_param_name = param_key param_label = ParamNameLabel(param_key) require_label = True is_str = isinstance(params[param_key], str) is_digital = isinstance(params[param_key], float) or isinstance(params[param_key], int) Loading @@ -157,6 +177,7 @@ class ParamWidget(QWidget): elif isinstance(params[param_key], dict): param_dict = params[param_key] display_param_name = get_param_display_name(param_key, param_dict) value = params[param_key]['value'] if param_dict['type'] == 'selector': if 'url' in param_key: Loading Loading @@ -188,11 +209,19 @@ class ParamWidget(QWidget): params[param_key]['value'] = value param_widget.setChecked(value) param_widget.paramwidget_edited.connect(self.on_paramwidget_edited) elif param_dict['type'] == 'pushbtn': param_widget = ParamPushButton(param_key, param_dict) param_widget.paramwidget_edited.connect(self.on_paramwidget_edited) require_label = False if 'description' in param_dict: param_widget.setToolTip(param_dict['description']) widget_idx = 0 if require_label: param_label = ParamNameLabel(display_param_name) param_layout.addWidget(param_label, ii, 0) param_layout.addWidget(param_widget, ii, 1) widget_idx = 1 param_layout.addWidget(param_widget, ii, widget_idx) def on_paramwidget_edited(self, param_key, param_content): content_dict = {'content': param_content} Loading utils/error_handling.py +9 −0 Original line number Diff line number Diff line Loading @@ -30,3 +30,12 @@ def create_error_dialog(exception: Exception, error_msg: str = None, exception_t if not shared.HEADLESS: shared.create_errdialog_in_mainthread(error_msg, detail_traceback, exception_type) def create_info_dialog(info_msg, btn_name: str = 'OK'): ''' Popup a info dialog in main thread ''' LOGGER.info(info_msg) if not shared.HEADLESS: shared.create_infodialog_in_mainthread(info_msg, btn_name) Loading
modules/ocr/ocr_stariver.py +20 −11 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ import base64 from typing import List from .base import register_OCR, OCRBase, TextBlock from utils.error_handling import create_error_dialog, create_info_dialog @register_OCR('stariver_ocr') Loading @@ -13,10 +14,6 @@ class OCRStariver(OCRBase): params = { 'User': "填入你的用户名", 'Password': "填入你的密码。请注意,密码会明文保存,请勿在公共电脑上使用", 'force_refresh_token': { 'type': 'checkbox', 'value': False }, "refine":{ 'type': 'checkbox', 'value': True Loading @@ -40,6 +37,12 @@ class OCRStariver(OCRBase): 'type': 'checkbox', 'value': False, }, 'update_token_btn': { 'type': 'pushbtn', 'value': '', 'description': '删除旧 Token 并重新申请', 'display_name': '更新 Token' }, 'description': '星河云(团子翻译器) OCR API' } Loading @@ -51,10 +54,6 @@ class OCRStariver(OCRBase): def Password(self): return self.params['Password'] @property def force_refresh_token(self): return self.params['force_refresh_token']['value'] @property def expand_ratio(self): return float(self.params['expand_ratio']) Loading Loading @@ -100,7 +99,8 @@ class OCRStariver(OCRBase): "Password": self.Password }).json() if response.get('Status', -1) != "Success": self.logger.error(f'stariver ocr 登录失败,错误信息:{response.get("ErrorMsg", "")}') error_msg = f'stariver ocr 登录失败,错误信息:{response.get("ErrorMsg", "")}' raise Exception(error_msg) token = response.get('Token', '') if token != '': self.logger.info(f'登录成功,token前10位:{token[:10]}') Loading Loading @@ -167,6 +167,7 @@ class OCRStariver(OCRBase): return texts_str def update_token_if_needed(self): token_updated = False if (self.User != self.register_username or self.Password != self.register_password): if self.token_obtained == False: Loading @@ -179,11 +180,19 @@ class OCRStariver(OCRBase): self.register_password = self.Password self.token_obtained = True self.logger.info("Token updated due to credential change.") token_updated = True return token_updated def updateParam(self, param_key: str, param_content): super().updateParam(param_key, param_content) if param_key == 'force_refresh_token': if param_key == 'update_token_btn': self.token_obtained = False # 强制刷新token时,将标志位设置为False self.token = '' # 强制刷新token时,将token置空 self.register_username = None # 强制刷新token时,将用户名置空 self.register_password = None # 强制刷新token时,将密码置空 try: if self.update_token_if_needed(): create_info_dialog('Token 更新成功') except Exception as e: create_error_dialog(e, 'Token 更新失败', 'TokenUpdateFailed') No newline at end of file
modules/textdetector/detector_stariver.py +19 −11 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ import requests import base64 from .base import register_textdetectors, TextDetectorBase, TextBlock from utils.error_handling import create_error_dialog, create_info_dialog @register_textdetectors('stariver_ocr') Loading @@ -13,10 +14,6 @@ class StariverDetector(TextDetectorBase): params = { 'User': "填入你的用户名", 'Password': "填入你的密码。请注意,密码会明文保存,请勿在公共电脑上使用", 'force_refresh_token': { 'type': 'checkbox', 'value': False }, 'expand_ratio': "0.01", "refine": { 'type': 'checkbox', Loading @@ -43,6 +40,12 @@ class StariverDetector(TextDetectorBase): "font_size_offset": "0", "font_size_min(set to -1 to disable)": "-1", "font_size_max(set to -1 to disable)": "-1", 'update_token_btn': { 'type': 'pushbtn', 'value': '', 'description': '删除旧 Token 并重新申请', 'display_name': '更新 Token' }, 'description': '星河云(团子翻译器) OCR 文字检测器' } Loading @@ -54,10 +57,6 @@ class StariverDetector(TextDetectorBase): def Password(self): return self.params['Password'] @property def force_refresh_token(self): return self.params['force_refresh_token']['value'] @property def expand_ratio(self): return float(self.params['expand_ratio']) Loading Loading @@ -118,8 +117,8 @@ class StariverDetector(TextDetectorBase): "Password": self.Password }).json() if response.get('Status', -1) != "Success": self.logger.error( f'stariver detector 登录失败,错误信息:{response.get("ErrorMsg", "")}') error_msg = f'stariver ocr 登录失败,错误信息:{response.get("ErrorMsg", "")}' raise Exception(error_msg) token = response.get('Token', '') if token != '': self.logger.info(f'stariver detector 登录成功,token前10位:{token[:10]}') Loading Loading @@ -297,6 +296,7 @@ class StariverDetector(TextDetectorBase): return dilated_mask def update_token_if_needed(self): token_updated = False if (self.User != self.register_username or self.Password != self.register_password): if self.token_obtained == False: Loading @@ -309,11 +309,19 @@ class StariverDetector(TextDetectorBase): self.register_password = self.Password self.token_obtained = True self.logger.info("Token updated due to credential change.") token_updated = True return token_updated def updateParam(self, param_key: str, param_content): super().updateParam(param_key, param_content) if param_key == 'force_refresh_token': if param_key == 'update_token_btn': self.token_obtained = False # 强制刷新token时,将标志位设置为False self.token = '' # 强制刷新token时,将token置空 self.register_username = None # 强制刷新token时,将用户名置空 self.register_password = None # 强制刷新token时,将密码置空 try: if self.update_token_if_needed(): create_info_dialog('Token 更新成功') except Exception as e: create_error_dialog(e, 'Token 更新失败', 'TokenUpdateFailed')
ui/mainwindow.py +9 −1 Original line number Diff line number Diff line Loading @@ -66,12 +66,15 @@ class MainWindow(mainwindow_cls): restart_signal = Signal() create_errdialog = Signal(str, str, str) create_infodialog = Signal(str, str) def __init__(self, app: QApplication, config: ProgramConfig, open_dir='', **exec_args) -> None: super().__init__() shared.create_errdialog_in_mainthread = self.create_errdialog.emit self.create_errdialog.connect(self.on_create_errdialog) shared.create_infodialog_in_mainthread = self.create_infodialog.emit self.create_infodialog.connect(self.on_create_infodialog) self.app = app self.setupThread() Loading Loading @@ -1238,3 +1241,8 @@ class MainWindow(mainwindow_cls): shared.showed_exception.remove(exception_type) LOGGER.error('Failed to create error dialog') LOGGER.error(traceback.format_exc()) def on_create_infodialog(self, msg: str, btn_name: str): dialog = QMessageBox() dialog.setText(msg) dialog.exec() No newline at end of file
ui/module_parse_widgets.py +33 −4 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ from .stylewidgets import ConfigComboBox, NoBorderPushBtn, CustomComboBox from utils.shared import CONFIG_FONTSIZE_CONTENT, CONFIG_COMBOBOX_MIDEAN, CONFIG_COMBOBOX_LONG, CONFIG_COMBOBOX_SHORT, CONFIG_COMBOBOX_HEIGHT from utils.config import pcfg from qtpy.QtWidgets import QPlainTextEdit, QHBoxLayout, QVBoxLayout, QWidget, QLabel, QComboBox, QCheckBox, QLineEdit, QGridLayout from qtpy.QtWidgets import QPlainTextEdit, QHBoxLayout, QVBoxLayout, QWidget, QLabel, QComboBox, QCheckBox, QLineEdit, QGridLayout, QPushButton from qtpy.QtCore import Qt, Signal from qtpy.QtGui import QFontMetricsF, QDoubleValidator Loading Loading @@ -117,6 +117,25 @@ class ParamCheckBox(QCheckBox): self.paramwidget_edited.emit(self.param_key, self.isChecked()) def get_param_display_name(param_key: str, param_dict: dict = None): if param_dict is not None and isinstance(param_dict, dict): if 'display_name' in param_dict: return param_dict['display_name'] return param_key class ParamPushButton(QPushButton): paramwidget_edited = Signal(str, str) def __init__(self, param_key: str, param_dict: dict = None, *args, **kwargs): super().__init__(*args, **kwargs) self.param_key = param_key self.setText(get_param_display_name(param_key, param_dict)) self.clicked.connect(self.on_clicked) def on_clicked(self): self.paramwidget_edited.emit(self.param_key, '') class ParamWidget(QWidget): paramwidget_edited = Signal(str, dict) Loading @@ -136,8 +155,9 @@ class ParamWidget(QWidget): for ii, param_key in enumerate(params): if param_key == 'description': continue display_param_name = param_key param_label = ParamNameLabel(param_key) require_label = True is_str = isinstance(params[param_key], str) is_digital = isinstance(params[param_key], float) or isinstance(params[param_key], int) Loading @@ -157,6 +177,7 @@ class ParamWidget(QWidget): elif isinstance(params[param_key], dict): param_dict = params[param_key] display_param_name = get_param_display_name(param_key, param_dict) value = params[param_key]['value'] if param_dict['type'] == 'selector': if 'url' in param_key: Loading Loading @@ -188,11 +209,19 @@ class ParamWidget(QWidget): params[param_key]['value'] = value param_widget.setChecked(value) param_widget.paramwidget_edited.connect(self.on_paramwidget_edited) elif param_dict['type'] == 'pushbtn': param_widget = ParamPushButton(param_key, param_dict) param_widget.paramwidget_edited.connect(self.on_paramwidget_edited) require_label = False if 'description' in param_dict: param_widget.setToolTip(param_dict['description']) widget_idx = 0 if require_label: param_label = ParamNameLabel(display_param_name) param_layout.addWidget(param_label, ii, 0) param_layout.addWidget(param_widget, ii, 1) widget_idx = 1 param_layout.addWidget(param_widget, ii, widget_idx) def on_paramwidget_edited(self, param_key, param_content): content_dict = {'content': param_content} Loading
utils/error_handling.py +9 −0 Original line number Diff line number Diff line Loading @@ -30,3 +30,12 @@ def create_error_dialog(exception: Exception, error_msg: str = None, exception_t if not shared.HEADLESS: shared.create_errdialog_in_mainthread(error_msg, detail_traceback, exception_type) def create_info_dialog(info_msg, btn_name: str = 'OK'): ''' Popup a info dialog in main thread ''' LOGGER.info(info_msg) if not shared.HEADLESS: shared.create_infodialog_in_mainthread(info_msg, btn_name)