Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TLiley2 authored Dec 14, 2024
1 parent 0d033e5 commit 131852b
Show file tree
Hide file tree
Showing 3 changed files with 428 additions and 0 deletions.
141 changes: 141 additions & 0 deletions OCR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import cv2
from pytesseract import *
import easyocr
import tkinter as tk
from screeninfo import get_monitors
from PIL import Image
import torch
import os
import sys
#import googletrans
import requests

sys.stderr = open(os.devnull, 'w') # Prevent printing warnings
b=[]
reader = easyocr.Reader(['ja'], gpu=True) # this needs to run only once to load the model into memory

def google_tran(inpoot):
# Define the input text and target language
text = inpoot
source_lang = "ja"
target_lang = "en"

# URL for Google Translate might not be accessible directly. Example assumes a public translation API endpoint.
url = f"https://translate.googleapis.com/translate_a/single"
params = {
"client": "gtx",
"sl": source_lang,
"tl": target_lang,
"dt": "t",
#"q": text
"q": "\n".join(text)
}

# Make the GET request
response = requests.get(url, params=params)

if response.status_code == 200:
#return (f"{response.json()[0][0][0]}")
return [item[0] for item in response.json()[0]]
else:
print("Failed to retrieve translation.")

def prime(imgap):
#print("prime")
# Load image
img = cv2.imread(imgap)
if img is None:
print("Image not found at path")

# Inverts Image
invert = cv2.bitwise_not(img)

# Cranks up contrast Image
contrast = cv2.convertScaleAbs(invert, alpha=1.5, beta=1)

# Greyscales Image
gray = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)

# Apply threshold to convert to binary image
threshold_img = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

#os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
#torch.device('cpu')
#print("halit")

result = reader.readtext(threshold_img, text_threshold=0.6, low_text=0.2)
processed_results = [
([list(map(int, bbox)) for bbox in item[0]], item[1], item[2])
for item in result if 0.09 < item[2] < 1
]
# [width x hiegth]

# Send all text for batch transaltion
all_text = [item[1] for item in processed_results]
translated = google_tran(all_text)

# translated=[]
# for x in range(len(all_text)):
# ddd = all_text[x]
# gg = google_tran(ddd)
# translated.append(gg)

for i, item in enumerate(processed_results):
min_x, min_y = item[0][0][0], item[0][0][1]
b.append([min_x, min_y, translated[i]])
# cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
# cv2.resizeWindow("Image", 500, 500)
# cv2.imshow("Image", gray)
# cv2.waitKey(0)

def exec(ima):
b.clear()
prime(ima)
#print(b)
for item in b:
s = int(float(item[0]))
e = int(float(item[1]))
# Pass them to the overlay function
overlay(s, e, item[2], ima)
# Start the mainloop only if the window exists
if hasattr(overlay, "win") and overlay.win.winfo_exists():
overlay.win.mainloop()

def overlay(xval, yval, tra_text, imge):
s = f"{xval}" # x is the x-coordinate for .place()
e = f"{yval}" # y is the y-coordinate for .place()
g = f"{tra_text}" # Only tra_text is the label text

# Create the main window if it's not created already
if not hasattr(overlay, "win") or not overlay.win.winfo_exists():
overlay.win = tk.Toplevel() # Use Toplevel instead of Tk for overlays
overlay.win.transient() # Make it modal relative to the parent window
overlay.win.grab_set() # Block interaction with other windows

# Set the geometry and color of the window
overlay.win.wm_overrideredirect(True)
overlay.win.geometry("{0}x{1}+0+0".format(overlay.win.winfo_screenwidth(), overlay.win.winfo_screenheight()))
overlay.win.config(bg='#add123') # Set background color
overlay.win.wm_attributes('-transparentcolor', '#add123') # Make window transparent

# Handle close event - window won't be destroyed
overlay.win.protocol("WM_DELETE_WINDOW", lambda: overlay.win.destroy()) # Hide window instead of destroying
overlay.win.bind("<Button-1>", lambda evt: overlay.win.destroy()) # Hide on click

overlay.win.update_idletasks()
drawable_height = overlay.win.winfo_height()
img = Image.open(imge)
img_height = img.height

# If label goes paset tkinker's drawable_height
if int(e) > int(drawable_height)-100:
reduct_val = int(img_height) - int(drawable_height)
e = int(e) - reduct_val
label = tk.Label(overlay.win, text=g, font=("Arial", 12), fg="black")
label.place(x=int(s), y=int(e))
#print(int(e))

overlay.win.deiconify()

# d=r"C:\Users\tnu20\Downloads\New folder\SLPM-67003_20241208234657.png"
# exec(d)
162 changes: 162 additions & 0 deletions backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import psutil
import tkinter as tk
import pygetwindow as gw
import pyautogui
from PIL import Image
import subprocess
import os
import sys
from pathlib import Path

#Reads of Shirtcut key in setting
def read_Short():
if getattr(sys, 'frozen', False):
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
else:
script_dir = os.path.dirname(os.path.abspath(__file__))
path_set = os.path.join(script_dir, "settings.ini")
with open(path_set, 'r') as file:
content = file.readlines()
row_without_None = [name.strip() for name in content]
for item in row_without_None: # 'item' takes each value from the list in turn
if "Shortcut" in item:
cu = item
index = row_without_None.index(cu)
lines = content[index].strip()
spl_word = '= '
res = lines.split(spl_word, 1)
splitString = res[1]
return splitString

#Reads of API key in setting
def read_API():
if getattr(sys, 'frozen', False):
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
else:
script_dir = os.path.dirname(os.path.abspath(__file__))
path_set = os.path.join(script_dir, "settings.ini")
with open(path_set, 'r') as file:
content = file.readlines()
row_without_None = [name.strip() for name in content]
for item in row_without_None: # 'item' takes each value from the list in turn
if "API_key" in item:
cu = item
index = row_without_None.index(cu)
lines = content[index].strip()
spl_word = '= '
res = lines.split(spl_word, 1)
splitString = res[1]
return splitString

#Modifies testing var in setting
# def modi_var():
# if getattr(sys, 'frozen', False):
# script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
# else:
# script_dir = os.path.dirname(os.path.abspath(__file__))
# # Construct the path for saving the screenshot
# path_set = os.path.join(script_dir, "settings.ini")
# with open(path_set, 'r+') as file:
# content = file.readlines()
# row_without_None = [name.strip() for name in content]
# for item in row_without_None:
# if "testing_co" in item:
# cu = item
# index = row_without_None.index(cu)
# print(index)
# lines = content[index].strip()
# last_element = lines.split()[-1]
# s1 = lines.replace(last_element, "3")
# file.seek(0)
# content[index] = s1 + '\n'
# file.writelines(content)
# return s1

#Modifies of API key in setting
def modi_API(key):
if getattr(sys, 'frozen', False):
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
else:
script_dir = os.path.dirname(os.path.abspath(__file__))
path_set = os.path.join(script_dir, "settings.ini")
with open(path_set, 'r+') as file:
content = file.readlines()
row_without_None = [name.strip() for name in content]
for item in row_without_None:
if "API_key" in item:
cu = item
index = row_without_None.index(cu)
lines = content[index].strip()
last_element = lines.split()[-1]
s1 = lines.replace(last_element, key)
file.seek(0)
content[index] = s1 + '\n'
file.writelines(content)
file.truncate()
return s1

# Checks if the seleceted .exe is currently open
def check_Status(exe_name):
mic_Check=exe_name in (i.name() for i in psutil.process_iter())
if mic_Check == True:
#print(exe_name)
print("yes1")
appl_name=exe_name.replace(".exe","")
screenshot(appl_name)
elif mic_Check == False :
NOPE = tk.Tk()
NOPE.title("Error 404")
NOPE.minsize(300, 200)
NOPE.maxsize(600, 200)
NOPE.configure(bg='grey')
NOPE.columnconfigure(0, weight=1)
NOPE.columnconfigure(1, weight=1)
NOPE.columnconfigure(2, weight=1)
NOPE.rowconfigure(0, weight=1)
l = tk.Label(NOPE,text="Error 404: Program is not runnning \n 1.)Please fully close this application\n 2.)Start your desired game\n 3.)restart this application and try again\n Sorry for the inconvience ;-)", font=("Noto Sans JP Bold", 10))
l.grid(column=1, row=0)
NOPE.mainloop()

# Takes screenshot of the program
def screenshot(app_name):
exe_ad = app_name

# Returns the programs current window title
cmd = f'powershell -Command "$OutputEncoding = [System.Text.Encoding]::UTF8; chcp 65001; Get-Process -Name {exe_ad} | ForEach-Object {{ $_.MainWindowTitle }}"'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

w = None
for line in proc.stdout:
if line.strip(): # Skip empty lines
try:
w = line.decode('utf-8').strip() # Attempt UTF-8 decoding
except UnicodeDecodeError:
print("Failed to decode output with UTF-8. Trying UTF-16.")
w = line.decode('utf-16-le').strip() # Try UTF-16LE as a fallback

if not w:
print("No window title found. Is the application running?")
return

window_title = w # Replace with the actual window title
#print(window_title)

# Returns the the dimensions of the app's window
window = gw.getWindowsWithTitle(window_title)[0]
left, top, right, bottom = window.left, window.top, window.right, window.bottom

if getattr(sys, 'frozen', False):
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
else:
script_dir = os.path.dirname(os.path.abspath(__file__))
path_set = os.path.join(script_dir, "result.png")
print(path_set)

# Take a screenshot of the window region and save it to the specified path
screenshot = pyautogui.screenshot(region=(left, top, right - left, bottom - top))
screenshot.save(path_set)

#Activates OCR file
import OCR
OCR.exec(path_set)

Loading

0 comments on commit 131852b

Please sign in to comment.