-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_Project.py
339 lines (260 loc) · 11.5 KB
/
Final_Project.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import tkinter as tk
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter import ttk,messagebox
from tkinter import filedialog
from tkinter.ttk import Combobox
import customtkinter
import googletrans
import textblob
import pyttsx3
import speech_recognition as sr
from PyDictionary import PyDictionary
import os
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
root = customtkinter.CTk()
root.title('Text And Speech Tool')
root.geometry("900x450+200+200")
root.resizable(False, False)
image_icon=PhotoImage(file="speak.png")
root.iconphoto(False,image_icon)
#Top Frame
Top_frame=Frame(root,bg="#308695",width=900,height=100)
Top_frame.place(x=0,y=0)
#Logo=PhotoImage(file="Logo")
#Label(Top_frame,image=Logo,bg="#282828").place(x=10,y=5)
Label(Top_frame,text="TEXT AND SPEECH TOOL" ,font="arial 20 bold",bg="#455054",fg="#D4CFC9").place(x=250,y=30)
def TextToSpeech():
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
texttospeechwindow = customtkinter.CTk()
texttospeechwindow.title('Text To Speech Convertor')
texttospeechwindow.geometry("900x450+200+200")
texttospeechwindow.resizable(False, False)
# Top Frame
Top_frame=Frame(texttospeechwindow,bg="#308695",width=900,height=100)
Top_frame.place(x=0,y=0)
#Logo=PhotoImage(file="Logo")
#Label(Top_frame,image=Logo,bg="#282828").place(x=10,y=5)
Label(Top_frame,text="TEXT TO SPEECH CONVERTER" ,font="arial 20 bold",bg="#455054",fg="#D4CFC9").place(x=250,y=30)
########
engine = pyttsx3.init()
def speak():
text=text_area.get(1.0,END)
gender=gender_combobox.get()
speed=speed_combobox.get()
voices=engine.getProperty('voices')
def setvoice():
if(gender == 'Male'):
engine.setProperty('voice',voices[0].id)
engine.say(text)
engine.runAndWait()
else:
engine.setProperty('voice',voices[1].id)
engine.say(text)
engine.runAndWait()
if(text):
if(speed =="Fast"):
engine.setProperty('rate',250)
setvoice()
elif(speed=="Normal"):
engine.setProperty('rate', 150)
setvoice()
else:
engine.setProperty('rate', 60)
setvoice()
def download():
text=text_area.get(1.0,END)
gender=gender_combobox.get()
speed=speed_combobox.get()
voices=engine.getProperty('voices')
def setvoice():
if(gender == 'Male'):
engine.setProperty('voice',voices[0].id)
path=filedialog.askdirectory()
os.chdir(path)
engine.save_to_file(text,'text.mp3')
engine.runAndWait()
else:
engine.setProperty('voice',voices[1].id)
path=filedialog.askdirectory()
os.chdir(path)
engine.save_to_file(text,'text.mp3')
engine.runAndWait()
if(text):
if(speed =="Fast"):
engine.setProperty('rate',250)
setvoice()
elif(speed=="Normal"):
engine.setProperty('rate', 150)
setvoice()
else:
engine.setProperty('rate', 60)
setvoice()
text_area=Text(texttospeechwindow,font="Robote 20",bg="white",relief=GROOVE,wrap=WORD)
text_area.place(x=10,y=150,width=500,height=250)
Label(texttospeechwindow,text="VOICE",font="arial 15 bold",bg="#181818",fg="#305065").place(x=580,y=160)
Label(texttospeechwindow,text="SPEED",font="arial 15 bold",bg="#181818",fg="#305065").place(x=760,y=160)
gender_combobox=Combobox(texttospeechwindow,values=['Male','Female'],font="arial 14",state='r',width=10)
gender_combobox.place(x=550,y=200)
gender_combobox.set('Male')
speed_combobox=Combobox(texttospeechwindow,values=['Fast','Normal','Slow'],font="arial 14",state='r',width=10)
speed_combobox.place(x=730,y=200)
speed_combobox.set('Normal')
#imageicon=PhotoImage(file="speak.png")
btn=Button(texttospeechwindow,text="Speak",width=10,font="arial 14 bold",command=speak)
btn.place(x=550,y=280)
#imageicon2=PhotoImage(file="speak.png")
save=Button(texttospeechwindow,text="Save",width=10,bg="#39c790",font="arial 14 bold",command=download)
save.place(x=730,y=280)
texttospeechwindow.mainloop()
def SpeechToText():
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
speechtotextwindow = customtkinter.CTk()
speechtotextwindow.title('Speech To Text Converter')
speechtotextwindow.geometry("900x450+200+200")
speechtotextwindow.resizable(False, False)
# Top Frame
Top_frame=Frame(speechtotextwindow,bg="#308695",width=900,height=100)
Top_frame.place(x=0,y=0)
#Logo=PhotoImage(file="Logo")
#Label(Top_frame,image=Logo,bg="#282828").place(x=10,y=5)
Label(Top_frame,text="SPEECH TO TEXT CONVERTER" ,font="arial 20 bold",bg="#455054",fg="#D4CFC9").place(x=250,y=30)
########
text1=''
file_name=("D:/Project (22-23)/Speech.wav")
def recordvoice():
while True:
text1 =''
r = sr.Recognizer()
with sr.Microphone() as source:
audio=r.listen(source)
try:
text1 = r.recognize_google(audio)
except:
pass
return text1
def Filevoice():
while True:
text1 =''
r = sr.Recognizer()
with sr.AudioFile(file_name) as source: # open file
audio=r.listen(source)
try:
text1 = r.recognize_google(audio)
except:
pass
return text1
def Reset():
text.delete(1.0,END)
text = Text(speechtotextwindow, font=12, height=5, width=30)
text.place(x=10,y=150,width=500,height=250)
recordbutton = Button(speechtotextwindow,text="Record",width=10,bg="#39c790",font="arial 14 bold", command=lambda: text.insert(END, recordvoice()))
recordbutton.place(x=550, y=200)
filebutton = Button(speechtotextwindow, text='Import',width=10, bg='#39c790',font="arial 14 bold", command=lambda: text.insert(END, Filevoice()))
filebutton.place(x=730, y=200)
speechtotextwindow.mainloop()
def Translator():
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
root= customtkinter.CTk()
root.title('Translator')
root.geometry("900x450+200+200")
root.resizable(False, False)
# Top Frame
Top_frame=Frame(root,bg="#308695",width=900,height=100)
Top_frame.place(x=0,y=0)
#Logo=PhotoImage(file="Logo")
#Label(Top_frame,image=Logo,bg="#282828").place(x=10,y=5)
Label(Top_frame,text="Translator" ,font="arial 20 bold",bg="#455054",fg="#D4CFC9").place(x=365,y=30)
########
def translate_it():
# Delete Any Previous Translations
translated_text.delete(1.0, END)
try:
# Get Languages From Dictionary Keys
# Get the From Langauage Key
for key, value in languages.items():
if (value == original_combo.get()):
from_language_key = key
# Get the To Language Key
for key, value in languages.items():
if (value == translated_combo.get()):
to_language_key = key
# Turn Original Text into a TextBlob
words = textblob.TextBlob(original_text.get(1.0, END))
# Translate Text
words = words.translate(from_lang=from_language_key , to=to_language_key)
# Output translated text to screen
translated_text.insert(1.0, words)
except Exception as e:
messagebox.showerror("Translator", e)
def clear():
# Clear the text boxes
original_text.delete(1.0, END)
translated_text.delete(1.0, END)
#language_list = (1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,16,1,1,1,1,1,1,1,1,1,1,1,1,1)
# Grab Language List From GoogleTrans
languages = googletrans.LANGUAGES
# Convert to list
language_list = list(languages.values())
# Text Boxes
original_text = Text(root, height=10, width=40,bg="#7E909A")
original_text.place(x=40,y=130)
#translate_button = Button(root, text="Translate!", font=("Helvetica", 24), command=translate_it)
#translate_button.place(x=450,y=180)
translated_text = Text(root, height=10, width=40,bg="#7E909A")
translated_text.place(x=530,y=130)
# Combo boxes
original_combo = ttk.Combobox(root, width=50, value=language_list)
original_combo.current(21)
original_combo.place(x=40,y=320)
translated_combo = ttk.Combobox(root, width=50, value=language_list)
translated_combo.current(26)
translated_combo.place(x=530,y=320)
translated_button = customtkinter.CTkButton(root, text="<-- Translate -->", command=translate_it)
translated_button.place(x=377,y=200)
# Clear butt
#clear_button = Button(root, text="Clear", command=clear)
#clear_button.grid(row=2, column=1)
clear_button = customtkinter.CTkButton(root, text="Clear", command=clear)
clear_button.place(x=377,y=315)
root.mainloop()
def Dictionary():
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
root = customtkinter.CTk()
root.title('Dictionary')
root.geometry("900x450+200+200")
root.resizable(False, False)
def search():
my_text.delete(1.0, END)
dictionary = PyDictionary()
definition = dictionary.meaning(my_entry.get())
for key,value in definition.items():
my_text.insert(END, key + '\n\n')
for values in value:
my_text.insert(END, f'- {values}\n\n')
my_labelframe = customtkinter.CTkFrame(root, corner_radius=10)
my_labelframe.pack(pady=20)
my_entry = customtkinter.CTkEntry(my_labelframe, width=400, height=40, border_width=1, placeholder_text="Enter A Word", text_color="silver")
my_entry.grid(row=0, column=0, padx=10, pady=10)
my_button = customtkinter.CTkButton(my_labelframe, text="Search", command=search)
my_button.grid(row=0, column=1, padx=10)
text_frame = customtkinter.CTkFrame(root, corner_radius=10)
text_frame.pack(pady=10)
my_text = Text(text_frame, height=20, width=67, wrap=WORD, bd=0, bg="#292929", fg="silver")
my_text.pack(pady=10, padx=10)
root.mainloop()
texttospeechbutton = customtkinter.CTkButton(root, text='Text-To-Speech Conversion', font=('Times New Roman', 24), command=TextToSpeech)
texttospeechbutton.place(x=280, y=150)
speechtotextbutton = customtkinter.CTkButton(root, text='Speech-To-Text Conversion', font=('Times New Roman', 24), command=SpeechToText)
speechtotextbutton.place(x=280, y=210)
translatorbutton = customtkinter.CTkButton(root,text= ' Language Translator ',font=('Times New Roman', 24), command=Translator)
translatorbutton.place(x=280, y=270)
dictionarybutton = customtkinter.CTkButton(root,text= ' Word Dictionary ',font=('Times New Roman', 24), command=Dictionary)
dictionarybutton.place(x=280, y=330)
root.update()
root.mainloop()