-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
132 lines (101 loc) · 3.67 KB
/
translate.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import languages
import threading
import pathlib
import speechTranslate as st
import textToSpeech as tts
import imageInterpreter as ii
# Translates input audio into English
def speechtranslate():
try:
targetLang = langVariable.get()
translatedText = st.recordAudio(targetLang)
textBox.insert(1.0, translatedText)
translatebutton.config(text="Translate")
except:
translatebutton.config(text="Translate")
# Translates text into desired language
def texttranslate():
input = textBox.get("1.0",END)
lang = langVariable.get()
result=st.translate(input,lang)
textBox.delete('1.0', END)
textBox.insert(1.0, result)
# Calls the speechtranslate while threading
def listen():
textBox.delete('1.0', END)
translatebutton.config(text="Listening...")
thread = threading.Thread(target=speechtranslate)
thread.start()
# Changes text to speech
def speak():
language = langVariable.get()
text = textBox.get("1.0",END)
if len(text) == 1:
text = st.translate("No text available",language)
print(type(text))
tts.playAudio(language,text)
# Calls translate function upon language change
def change(event):
input = textBox.get("1.0",END)
lang = langVariable.get()
result=st.translate(input,lang)
textBox.delete('1.0', END)
textBox.insert(1.0, result)
# upload image fuction
def UploadAction(event=None):
filename = filedialog.askopenfilename()
print('Selected:', filename)
result = ii.getTextFromImage(filename)
lang = langVariable.get()
result=st.translate(result,lang)
textBox.delete('1.0', END)
textBox.insert(1.0, result)
#path of current file
path = pathlib.Path(__file__).parent.absolute()
#App icon image
filename= str(path) + '/images/image.png'
#Window components
window = Tk()
window.iconphoto(False, PhotoImage(file=filename))
window.title("Speech Translate")
window.geometry('400x500')
window.configure(background = "#161d25")
window.resizable(False, False)
#Body Frame
frame = Frame(window,bg = "#161d25",width=400,height=500)
frame.grid(row=0,column=0,sticky="NW")
frame.grid_propagate(0)
frame.update()
OPTIONS = languages.getLanguages()
#Language Button
langVariable = StringVar(frame)
langVariable.set("english") # default value
languageOption = OptionMenu(frame, langVariable, *OPTIONS, command = change)
languageOption.place(x=200, y=20, anchor="center")
languageOption.config(bg = "#161d25",font=("Courier", 15),height = 10, width = 15)
#TextBox
textBox = Text(frame)
textBox.place(x=200, y=150, anchor="center")
textBox.config(font=("Courier", 15),height = 12,width = 40)
#Speech Translate Button
iconFile = str(path) + '/images/icon.png'
photo = PhotoImage(file = iconFile)
speechbutton = Button(frame, text="Speech Translate", image = photo , command=listen)
speechbutton.place(x=200, y=290, anchor="center")
speechbutton.config(fg = "#161d25",font=("Courier", 15),height = 50, width = 50)
#Text Translate Button
translatebutton = Button(frame, text="Translate", command=texttranslate)
translatebutton.place(x=120, y=350, anchor="center")
translatebutton.config(fg = "#161d25",font=("Courier", 15),height = 2, width = 16)
#Play Audio Button
Audiobutton = Button(frame, text="Play Audio", command=speak)
Audiobutton.place(x=280, y=350, anchor="center")
Audiobutton.config(fg = "#161d25",font=("Courier", 15),height = 2, width = 16)
#Upload Image Button
imageButton = Button(frame, text="Image Upload", command=UploadAction)
imageButton.place(x=200, y=420, anchor="center")
imageButton.config(fg = "#161d25",font=("Courier", 15),height = 2, width = 16)
window.mainloop()