-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (58 loc) · 2.45 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from PIL import Image, ImageGrab #tratamiento de imagen
import pytesseract #herramienta de recnocimiento
import cutlet #herramienta para leer simbolos raros
import os, time #herramientas del sistema
import translators as ts #herramienta de traduccion
import translators.server as tss
import wx #herramienta para la interfaz
class Translator():
def get_lines(self, x1, y1, x2, y2):
bbox = (x1, y1, x2, y2)
im = ImageGrab.grab(bbox)
# config --psm 11 es la forma en la que lee el texto, existen mas
text_raw = pytesseract.image_to_string(im, lang='jpn', config='--psm 11')
text = text_raw.replace('/','!')
#Borrar espacios en blanco
text = "\n".join([linea.rstrip() for linea in text.splitlines() if linea.strip()])
lines = text.splitlines()
return lines
#print(pytesseract.get_languages())
def start_reading(self, x1, y1, x2, y2):
katsu = cutlet.Cutlet()
katsu.use_foreign_spelling = False
lines = ''
while True:
new_lines = self.get_lines(x1,y1,x2,y2)
if lines != new_lines:
os.system('clear') #se usa clear en linux. En Windows seria cls
lines = new_lines
for line in lines:
print(line)
try:
translation = ts.translate_text(line, to_language='es')#English -> ts.translate_text(line)
print(translation)
except KeyError:
print("Error: No se pudo traducir la línea correctamente.")
print('---------')
time.sleep(0.1)
class DesktopController(wx.Frame):
def __init__(self, parent, title):
super(DesktopController, self).__init__(parent, title=title)
self.SetTransparent(50)
self.button = wx.Button(self)
self.Bind(wx.EVT_BUTTON, self.on_button_click, self.button)
self.SetPosition(wx.Point(0,0))
self.Show()
def on_button_click(self, event):
print(self.button.Size)
size = self.button.Size
x1,y1 = self.button.GetScreenPosition()
x2,y2 = x1 + size[0], y1 + size[1]
print(x1,y1,x2,y2)
self.Hide() #oculta el rectangulo
#Iniciar la traduccion...
Translator().start_reading(x1,y1,x2,y2)
if __name__ == '__main__':
app = wx.App()
mf = DesktopController(None, title="Selecciona area a traducir")
app.MainLoop()