-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.py
180 lines (141 loc) · 5.98 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import threading
import tkinter as tk
from gtts import gTTS
from tkinter import ttk
import speech_recognition as sr
from playsound import playsound
from deep_translator import GoogleTranslator
from google.transliteration import transliterate_text
# Create an instance of Tkinter frame or window
win= tk.Tk()
# Set the geometry of tkinter frame
win.geometry("700x450")
win.title("Real-Time Voice🎙️ Translator🔊")
icon = tk.PhotoImage(file="icon.png")
win.iconphoto(False, icon)
# Create labels and text boxes for the recognized and translated text
input_label = tk.Label(win, text="Recognized Text ⮯")
input_label.pack()
input_text = tk.Text(win, height=5, width=50)
input_text.pack()
output_label = tk.Label(win, text="Translated Text ⮯")
output_label.pack()
output_text = tk.Text(win, height=5, width=50)
output_text.pack()
blank_space = tk.Label(win, text="")
blank_space.pack()
# Create a dictionary of language names and codes
language_codes = {
"English": "en",
"Hindi": "hi",
"Bengali": "bn",
"Spanish": "es",
"Chinese (Simplified)": "zh-CN",
"Russian": "ru",
"Japanese": "ja",
"Korean": "ko",
"German": "de",
"French": "fr",
"Tamil": "ta",
"Telugu": "te",
"Kannada": "kn",
"Gujarati": "gu",
"Punjabi": "pa"
}
language_names = list(language_codes.keys())
# Create dropdown menus for the input and output languages
input_lang_label = tk.Label(win, text="Select Input Language:")
input_lang_label.pack()
input_lang = ttk.Combobox(win, values=language_names)
def update_input_lang_code(event):
selected_language_name = event.widget.get()
selected_language_code = language_codes[selected_language_name]
# Update the selected language code
input_lang.set(selected_language_code)
input_lang.bind("<<ComboboxSelected>>", lambda e: update_input_lang_code(e))
if input_lang.get() == "": input_lang.set("auto")
input_lang.pack()
down_arrow = tk.Label(win, text="▼")
down_arrow.pack()
output_lang_label = tk.Label(win, text="Select Output Language:")
output_lang_label.pack()
output_lang = ttk.Combobox(win, values=language_names)
def update_output_lang_code(event):
selected_language_name = event.widget.get()
selected_language_code = language_codes[selected_language_name]
# Update the selected language code
output_lang.set(selected_language_code)
output_lang.bind("<<ComboboxSelected>>", lambda e: update_output_lang_code(e))
if output_lang.get() == "": output_lang.set("en")
output_lang.pack()
blank_space = tk.Label(win, text="")
blank_space.pack()
keep_running = False
def update_translation():
global keep_running
if keep_running:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak Now!\n")
audio = r.listen(source)
try:
speech_text = r.recognize_google(audio)
# print(speech_text)
speech_text_transliteration = transliterate_text(speech_text, lang_code=input_lang.get()) if input_lang.get() not in ('auto', 'en') else speech_text
input_text.insert(tk.END, f"{speech_text_transliteration}\n")
if speech_text.lower() in {'exit', 'stop'}:
keep_running = False
return
translated_text = GoogleTranslator(source=input_lang.get(), target=output_lang.get()).translate(text=speech_text_transliteration)
# print(translated_text)
voice = gTTS(translated_text, lang=output_lang.get())
voice.save('voice.mp3')
playsound('voice.mp3')
os.remove('voice.mp3')
output_text.insert(tk.END, translated_text + "\n")
except sr.UnknownValueError:
output_text.insert(tk.END, "Could not understand!\n")
except sr.RequestError:
output_text.insert(tk.END, "Could not request from Google!\n")
win.after(100, update_translation)
def run_translator():
global keep_running
if not keep_running:
keep_running = True
update_translation_thread = threading.Thread(target=update_translation) # using multi threading for efficient cpu usage
update_translation_thread.start()
def kill_execution():
global keep_running
keep_running = False
def open_about_page(): # about page
about_window = tk.Toplevel()
about_window.title("About")
about_window.iconphoto(False, icon)
# Create a link to the GitHub repository
github_link = ttk.Label(about_window, text="github.com/SamirPaulb/real-time-voice-translator", underline=True, foreground="blue", cursor="hand2")
github_link.bind("<Button-1>", lambda e: open_webpage("https://github.com/SamirPaulb/real-time-voice-translator"))
github_link.pack()
# Create a text widget to display the about text
about_text = tk.Text(about_window, height=10, width=50)
about_text.insert("1.0", """
A machine learning project that translates voice from one language to another in real time while preserving the tone and emotion of the speaker, and outputs the result in MP3 format. Choose input and output languages from the dropdown menu and start the translation!
""")
about_text.pack()
# Create a "Close" button
close_button = tk.Button(about_window, text="Close", command=about_window.destroy)
close_button.pack()
def open_webpage(url): # Opens a web page in the user's default web browser.
import webbrowser
webbrowser.open(url)
# Create the "Run" button
run_button = tk.Button(win, text="Start Translation", command=run_translator)
run_button.place(relx=0.25, rely=0.9, anchor="c")
# Create the "Kill" button
kill_button = tk.Button(win, text="Kill Execution", command=kill_execution)
kill_button.place(relx=0.5, rely=0.9, anchor="c")
# Open about page button
about_button = tk.Button(win, text="About this project", command=open_about_page)
about_button.place(relx=0.75, rely=0.9, anchor="c")
# Run the Tkinter event loop
win.mainloop()