Skip to content

Commit

Permalink
fix: 拦截wayland重复ocr识别
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhldr committed Sep 17, 2024
1 parent c6b4f1c commit 7164f0d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lfy/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# main.py
import hashlib
import os
import threading
import time
Expand Down Expand Up @@ -31,6 +32,7 @@ def __init__(self, app_id, version):
self._version = version
self._application_id = app_id
self.translate_now = GLib.Variant.new_boolean(True)
self.img_md5 = ""

action_trans_now = Gio.SimpleAction.new_stateful(
'copy2translate', None, self.translate_now)
Expand Down Expand Up @@ -214,6 +216,14 @@ def on_active_copy(cb2, res):
def save_img(cb2, res):
texture = cb2.read_texture_finish(res)
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

path = "/tmp/lfy.png"
pixbuf.savev(path, "png", (), ())
self.do_activate(path, ocr=True)
Expand Down

0 comments on commit 7164f0d

Please sign in to comment.