Skip to content

Commit

Permalink
fix: ocr子线程判断
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhldr committed Sep 17, 2024
1 parent 7164f0d commit 8a8f75f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
21 changes: 21 additions & 0 deletions lfy/api/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import hashlib


def s2ks(s):
"""_summary_
Expand Down Expand Up @@ -30,3 +33,21 @@ def is_text(cf):
or cf.contain_mime_type("text/plain;charset=utf-8"):
return True
return False


def cal_md5(file_path):
"""md5测试
Args:
file_path (_type_): _description_
Returns:
_type_: _description_
"""
md5_hash = hashlib.md5()
with open(file_path, "rb") as file:
# 以二进制方式读取文件内容
# 较大的文件可以分块读取,以避免一次性加载整个文件到内存中
for chunk in iter(lambda: file.read(4096), b""):
md5_hash.update(chunk)
return md5_hash.hexdigest()
29 changes: 15 additions & 14 deletions lfy/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# main.py
import hashlib
import os
import threading
import time
Expand Down Expand Up @@ -67,19 +66,23 @@ def __init__(self, app_id, version):
if Settings.get().auto_check_update:
threading.Thread(target=self.find_update, daemon=True).start()

def get_translate_win(self):
win = self.props.active_window # pylint: disable=E1101
if not win:
width, height = Settings.get().window_size
win = TranslateWindow(
application=self, default_height=height, default_width=width, )
win.present()
return win

def do_activate(self, s="", ocr=False):
"""翻译
Args:
s (str, optional): _description_. Defaults to "".
ocr (bool, optional): _description_. Defaults to False.
"""
win = self.props.active_window # pylint: disable=E1101
if not win:
width, height = Settings.get().window_size
win = TranslateWindow(
application=self, default_height=height, default_width=width, )
win.present()
win = self.get_translate_win()
if ocr:
win.update_ocr(s)
else:
Expand Down Expand Up @@ -214,18 +217,16 @@ def on_active_copy(cb2, res):
self.do_activate(cb2.read_text_finish(res))

def save_img(cb2, res):
ss = time.time()
texture = cb2.read_texture_finish(res)
print("1", time.time()-ss)
pixbuf = Gdk.pixbuf_get_from_texture(texture)

md5_hash = hashlib.md5(pixbuf.get_pixels()).hexdigest()
# 防止wayland多次识别
if self.img_md5 == md5_hash:
print("same image, no ocr")
return
self.img_md5 = md5_hash
print("2", time.time()-ss)

path = "/tmp/lfy.png"
pixbuf.savev(path, "png", (), ())
print("3", time.time()-ss)

self.do_activate(path, ocr=True)

span = time.time() - self.last_clip
Expand Down
20 changes: 17 additions & 3 deletions lfy/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
server_key2i)
from lfy.api.constant import NO_TRANSLATED_TXTS
from lfy.api.server import Server
from lfy.api.utils import cal_md5
from lfy.api.utils.notify import nf_t
from lfy.settings import Settings
from lfy.widgets.theme_switcher import ThemeSwitcher
Expand Down Expand Up @@ -67,6 +68,7 @@ def __init__(self, **kwargs):
self.app = self.get_application()

self.setting = Settings.get()
self.img_md5 = ""

# 可能包含上次的追加内容
self.last_text = ""
Expand Down Expand Up @@ -262,9 +264,21 @@ def request_text(self, s, server, lk=None):
s (str): _description_
server (Server): _description_
"""
GLib.idle_add(self.update_ui, "", lk is None)
is_ocr = lk is None
if is_ocr:
print(s)
ss = time.time()
md5_hash = cal_md5(s)
print("4", time.time()-ss)
# 防止wayland多次识别
if self.img_md5 == md5_hash:
print("same image, no ocr", md5_hash)
return
self.img_md5 = md5_hash

GLib.idle_add(self.update_ui, "", is_ocr)
try:
if lk is None:
if is_ocr:
_ok, text = server.ocr_image(s)
if self.cbtn_del_wrapping.get_active():
text = process_text(text)
Expand All @@ -275,7 +289,7 @@ def request_text(self, s, server, lk=None):
error_msg = _("something error:")
error_msg2 = f"{str(e)}\n\n{traceback.format_exc()}"
text = f"{error_msg}{server.name}\n\n{error_msg2}"
GLib.idle_add(self.update_ui, text, lk is None)
GLib.idle_add(self.update_ui, text, is_ocr)

def update_ui(self, s="", ocr=False):
"""更新界面
Expand Down

0 comments on commit 8a8f75f

Please sign in to comment.