Skip to content

Commit

Permalink
fix: ocr错误信息提示
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhldr committed Sep 24, 2024
1 parent 3fa38e4 commit 592630f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
4 changes: 2 additions & 2 deletions data/cool.ldr.lfy.in.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
<description>Baidu API Key and Secret Key for ocr, used in the middle | split</description>
</key>
<key type="s" name="server-sk-pytesseract-ocr">
<default>"chi_sim+eng"</default>
<default>"chi_sim | eng"</default>
<description>The lang parameters of the pytesseract module, such as Chinese and English by default</description>
</key>
<key type="s" name="server-sk-easyocr-ocr">
<default>"ch_sim+en"</default>
<default>"ch_sim | en"</default>
<description>The lang parameters of the easyocr module, such as Chinese and English by default, split by +</description>
</key>
<key type="s" name="ocr-baidu-token">
Expand Down
4 changes: 2 additions & 2 deletions lfy/api/server/ocr/easyocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def ocr_image(self, img_path: str, lang_keys=None):
try:
import easyocr
if lang_keys is None:
lang_keys = self.get_api_key_s_ocr().split('+')
lang_keys = self.get_api_key_s_ocr().split('|')
reader = easyocr.Reader(lang_keys)
s = " ".join(reader.readtext(img_path, detail=0))
return True, s.strip()
except ModuleNotFoundError as e:
print(e)
get_logger().error(e)
s = _("please install python whl")
s += (str(e).replace("No module named", ""))
s += str(e).replace("No module named", "")
return False, s

except Exception as e: # pylint: disable=W0718
Expand Down
35 changes: 31 additions & 4 deletions lfy/api/server/ocr/pytesseract.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ def ocr_image(self, img_path: str, lang=None):
import pytesseract
if lang is None:
lang = self.get_api_key_s_ocr()
print(lang)
return True, pytesseract.image_to_string(img_path, lang=lang)
except ModuleNotFoundError as e:
print(e)
get_logger().error(e)
s = _("please install python whl")
s += str(e).replace("No module named", "")
return False, s
except Exception as e: # pylint: disable=W0718
except Exception as e: # pylint: disable=W0718
print(e)
get_logger().error(e)
return False, str(e)

Expand All @@ -44,10 +46,35 @@ def get_api_key_s_ocr(self):
return Settings.get().server_sk_pytesseract_ocr

def check_ocr(self, api_key_ocr_s):
"""ocr环境
Args:
api_key_ocr_s (str): _description_
Returns:
_type_: _description_
"""
path = gen_img("success")
lang_str = "+".join(api_key_ocr_s.split("|"))
try:
import pytesseract
langs = pytesseract.get_languages()
langs_ = []
for lang in lang_str.split("+"):
if lang not in langs:
langs_.append(lang)
if len(langs_) > 0:
return False, _("Tesseract OCR database {} is not installed").format("-".join(langs_))
except ModuleNotFoundError as e:
print(e)
get_logger().error(e)
s = _("please install python whl")
s += str(e).replace("No module named", "")
return False, s

ok, text = self.ocr_image(path, api_key_ocr_s)
if ok:
Settings.get().server_sk_pytesseract_ocr = api_key_ocr_s
return ok, text
if not ok:
return ok, text

Settings.get().server_sk_pytesseract_ocr = api_key_ocr_s
return ok, text
24 changes: 9 additions & 15 deletions lfy/widgets/server_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def _on_api_key_apply(self, _row):
api_key = re.sub(r'\s*\|\s*', " | ", api_key.strip())
self.api_key_entry.set_text(api_key)
self.api_key_entry.set_sensitive(False)
self.api_key_stack.set_visible_child_name('spinner')
self.api_key_spinner.start()

check_ot = self.server.check_translate
Expand All @@ -76,25 +75,20 @@ def update_ui(self, valid, entry, spinner):
valid (_type_): _description_
"""
ok, text = valid

entry.set_sensitive(True)
spinner.stop()

if ok:
entry.remove_css_class('error')
self.dialog.add_toast(Adw.Toast.new(text.strip()))
else:
entry.add_css_class('error')
entry.props.sensitive = True
spinner.stop()

# 创建 AlertDialog
dialog = Adw.AlertDialog.new(text)
dialog.set_title("错误")
dialog.set_body(text)
dialog.add_response("ok", "确定")
dialog.set_default_response("ok")
# 设置对话框为模态
dialog.set_modal(True)
# 连接响应信号
dialog.connect("response", lambda dialog, response: dialog.hide())
# 显示对话框
dialog.show()
dialog = Adw.AlertDialog.new(_("error message"))
dialog.set_body(text)
dialog.add_response("confirm", _("Confirm"))
dialog.present(self.get_root())

def request_text(self, fun, api_key, entry, spinner):
"""验证服务api是否靠谱
Expand Down
10 changes: 9 additions & 1 deletion po/lfy.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: cool.ldr.lfy\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2024-09-22 18:27+0800\n"
"POT-Creation-Date: 2024-09-24 13:36+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -376,6 +376,14 @@ msgstr ""
msgid "Text recognition"
msgstr ""

#: lfy/widgets/server_preferences.py:88
msgid "error message"
msgstr ""

#: lfy/widgets/server_preferences.py:90
msgid "Confirm"
msgstr ""

#: data/resources/translate.ui:71
msgid "Alt + D: Remove symbols such as line breaks"
msgstr ""
Expand Down
10 changes: 9 additions & 1 deletion po/zh_CN.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: cool.ldr.lfy\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2024-09-22 18:27+0800\n"
"POT-Creation-Date: 2024-09-24 13:36+0800\n"
"PO-Revision-Date: 2023-11-12 21:02+0800\n"
"Last-Translator: <[email protected]>\n"
"Language-Team: Chinese (simplified) <[email protected]>\n"
Expand Down Expand Up @@ -377,6 +377,14 @@ msgstr "翻译服务"
msgid "Text recognition"
msgstr "文本识别"

#: lfy/widgets/server_preferences.py:88
msgid "error message"
msgstr "错误信息"

#: lfy/widgets/server_preferences.py:90
msgid "Confirm"
msgstr "确认"

#: data/resources/translate.ui:71
msgid "Alt + D: Remove symbols such as line breaks"
msgstr "Alt + D: 移除换行符"
Expand Down

0 comments on commit 592630f

Please sign in to comment.