Guest User

COR

a guest
Dec 11th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. import cv2
  2. import pytesseract
  3. from pytesseract import *
  4. import easyocr
  5. import tkinter as tk
  6. import re
  7. from screeninfo import get_monitors
  8. from PIL import Image
  9. import torch
  10. import os
  11. import warnings
  12. import sys
  13. #import googletrans
  14. import requests
  15. #warnings.filterwarnings("ignore") # Prevent printing warnings
  16. sys.stderr = open(os.devnull, 'w')
  17. b=[]
  18.  
  19. def google_tran(inpoot):
  20. # Define the input text and target language
  21. text = inpoot
  22. source_lang = "ja"
  23. target_lang = "en"
  24.  
  25. # URL for Google Translate might not be accessible directly. Example assumes a public translation API endpoint.
  26. url = f"https://translate.googleapis.com/translate_a/single"
  27. params = {
  28. "client": "gtx",
  29. "sl": source_lang,
  30. "tl": target_lang,
  31. "dt": "t",
  32. "q": text
  33. }
  34.  
  35. # Make the GET request
  36. response = requests.get(url, params=params)
  37.  
  38. if response.status_code == 200:
  39. translation = response.json()[0][0][0] # Extract translation result
  40. translated = (f"{translation}")
  41. return translated
  42. else:
  43. print("Failed to retrieve translation.")
  44.  
  45. def prime(imgap):
  46. pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
  47.  
  48. # Load image
  49. img = cv2.imread(imgap)
  50. if img is None:
  51. print("Image not found at path")
  52.  
  53. # Inverts Image
  54. invert = cv2.bitwise_not(img)
  55.  
  56. # Cranks up contrast Image
  57. contrast = cv2.convertScaleAbs(invert, alpha=1.5, beta=1)
  58.  
  59. # Greyscales Image
  60. gray = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)
  61.  
  62. # Apply threshold to convert to binary image
  63. threshold_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
  64.  
  65. #os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
  66. #torch.device('cpu')
  67. reader = easyocr.Reader(['ja'], gpu=True) # this needs to run only once to load the model into memory
  68. result = reader.readtext(threshold_img, text_threshold=0.6, low_text=0.2)
  69. #print(result)
  70. processed_results = [
  71. ([list(map(int, bbox)) for bbox in result[0]], result[1], result[2])
  72. for result in result
  73. ]
  74. #print(processed_results)
  75. # [width x hiegth]
  76.  
  77. all_text = [item[1] for item in processed_results]
  78. #print(all_text)
  79. translated=[]
  80. for x in range(len(all_text)):
  81. ddd = all_text[x]
  82. gg = google_tran(ddd)
  83. translated.append(gg)
  84.  
  85. min_y = [item[0][0][1] for item in processed_results]
  86. #print(min_y)
  87. min_x = [item[0][0][0] for item in processed_results]
  88. #print(min_x)
  89.  
  90. for x in range(len(processed_results)):
  91. b.extend([[min_x[x], min_y[x], translated[x]]])
  92. #print(b)
  93.  
  94. def exec(ima):
  95. prime(ima)
  96. #print(b)
  97. for item in b:
  98. s = int(float(item[0]))
  99. e = int(float(item[1])) # Ensure s is converted to an integer
  100. overlay(s, e, item[2], ima) # Pass them to the overlay function
  101. if hasattr(overlay, "win") and overlay.win.winfo_exists(): # Start the mainloop only if the window exists
  102. overlay.win.mainloop()
  103.  
  104. def overlay(xval, yval, tra_text, imge):
  105. s = f"{xval}" # x is the x-coordinate for .place()
  106. e = f"{yval}" # y is the y-coordinate for .place()
  107. g = f"{tra_text}" # Only tra_text is the label text
  108.  
  109. # Create the main window if it's not created already
  110. if not hasattr(overlay, "win") or not overlay.win.winfo_exists(): # Check if window exists and is not destroyed
  111. overlay.win = tk.Tk()
  112.  
  113. # Set the geometry of the window
  114. overlay.win.wm_overrideredirect(True)
  115. overlay.win.geometry("{0}x{1}+0+0".format(overlay.win.winfo_screenwidth(), overlay.win.winfo_screenheight()))
  116. overlay.win.config(bg='#add123') # Set background color
  117. overlay.win.wm_attributes('-transparentcolor', '#add123') # Make window transparent
  118.  
  119. # Handle close event - window won't be destroyed
  120. overlay.win.protocol("WM_DELETE_WINDOW", lambda: overlay.win.destroy()) # Hide window instead of destroying
  121. overlay.win.bind("<Button-1>", lambda evt: overlay.win.destroy()) # Hide on click
  122.  
  123. overlay.win.update_idletasks()
  124. drawable_height = overlay.win.winfo_height()
  125. img = Image.open(imge)
  126. img_height = img.height
  127. if int(e) > int(drawable_height)-100:
  128. reduct_val = int(img_height) - int(drawable_height)
  129. e = int(e) - reduct_val
  130. label = tk.Label(overlay.win, text=g, font=("Arial", 12), fg="black")
  131. label.place(x=int(s), y=int(e))
  132. #print(int(e))
  133.  
  134. overlay.win.deiconify() # Make sure the window is shown if it was hidden before
  135.  
  136. # d=r"C:\Users\tnu20\Downloads\New folder\swstella_system_cg1.jpg"
  137. # exec(d)
Advertisement
Add Comment
Please, Sign In to add comment