import easyocr
import numpy as np
import time
import tkinter as tk
import pyautogui
from win11toast import toast
from transformers import pipeline
import keyboard
# Set not to display if the number of characters is less than a certain number
Letter_cut = 5
translator = pipeline(
"translation",
model="facebook/mbart-large-50-one-to-many-mmt",
src_lang="en_XX",#Adapt to the language you use
tgt_lang="ja_XX",
)
reader = easyocr.Reader(lang_list=["ja", "en"])
toast("On standby. Press Ctrl+Alt+X to take a screenshot and start OCR.")
while True:
if keyboard.is_pressed("ctrl+alt+x"):
img = pyautogui.screenshot("tmp.png")
toast("Screenshot taken Please wait for the OCR to complete")
t = time.time()
result = reader.readtext(np.array(img))
# Create a transparent main_window
main_window = tk.Tk()
main_window.wm_attributes("-transparentcolor", "white")
main_window.attributes("-topmost", True)
tk.Frame(main_window, background="white").pack(expand=True, fill=tk.BOTH)
main_window.attributes("-fullscreen", True)
filler = tk.Toplevel(main_window)
filler.overrideredirect(True)
filler.transient(main_window)
filler.attributes("-alpha", 0.01)
def on_configure_let_filler_track(_):
filler.geometry(
f"{main_window.winfo_width()}x{main_window.winfo_height()}+{main_window.winfo_rootx()}+{main_window.winfo_rooty()}"
)
filler.lower(main_window)
main_window.bind("<Configure>", on_configure_let_filler_track)
# Close window by left or right mouse click or keystroke
filler.bind("<Button-1>", lambda _: main_window.destroy())
filler.bind("<Button-3>", lambda _: main_window.destroy())
filler.bind("<Key>", lambda _: main_window.destroy())
labels = []
for detection in result:
print(detection[1])
top_left = tuple(detection[0][0])
top_right = tuple(detection[0][1])
bottom_right = tuple(detection[0][2])
bottom_left = tuple(detection[0][3])
text = detection[1]
if len(text) < Letter_cut:
continue
if top_right[0] - top_left[0] < 10 * len(text):
continue
text = translator(text)[0]["translation_text"]
print(text)
label = tk.Label(main_window, text=text).place(
x=top_left[0],
y=top_left[1],
width=top_right[0] - top_left[0],
height=bottom_left[1] - top_left[1],
)
labels.append(label)
print("elapsed_time:{0}".format(time.time() - t) + "[sec]")
main_window.mainloop()
time.sleep(0.1)
Comments