Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Mizu
Created July 25, 2024

Fast and useful translation on desktop

Our desktop translation will allow us to play games in other languages.

5
Fast and useful translation on desktop

Things used in this project

Software apps and online services

Windows 10
Microsoft Windows 10

Story

Read more

Code

main.py

Python
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)

requirements.txt

Plain text
annotated-types==0.6.0
anyio==4.3.0
certifi==2024.2.2
charset-normalizer==3.3.2
colorama==0.4.6
distro==1.9.0
easyocr==1.7.1
filelock==3.13.4
fsspec==2024.3.1
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
huggingface-hub==0.22.2
idna==3.7
imageio==2.34.1
intel-openmp==2021.4.0
Jinja2==3.1.3
lazy_loader==0.4
MarkupSafe==2.1.5
mkl==2021.4.0
MouseInfo==0.1.3
mpmath==1.3.0
networkx==3.3
ninja==1.11.1.1
numpy==1.26.4
openai==1.25.1
opencv-python-headless==4.9.0.80
packaging==24.0
pillow==10.3.0
protobuf==5.26.1
PyAutoGUI==0.9.54
pyclipper==1.3.0.post5
pydantic==2.7.1
pydantic_core==2.18.2
PyGetWindow==0.0.9
PyMsgBox==1.0.9
pyocr==0.8.5
pyperclip==1.8.2
PyRect==0.2.0
PyScreeze==0.1.30
python-bidi==0.4.2
pytweening==1.2.0
PyYAML==6.0.1
regex==2024.4.16
requests==2.31.0
safetensors==0.4.3
scikit-image==0.23.2
scipy==1.13.0
sentencepiece==0.2.0
shapely==2.0.4
six==1.16.0
sniffio==1.3.1
sympy==1.12
tbb==2021.12.0
tifffile==2024.4.24
tokenizers==0.19.1
torch==2.3.0
torchvision==0.18.0
tqdm==4.66.2
transformers==4.40.0
typing_extensions==4.11.0
urllib3==2.2.1
win11toast==0.34
winsdk==1.0.0b10

Credits

Mizu
1 project • 0 followers

Comments