-
Notifications
You must be signed in to change notification settings - Fork 0
/
projm1.py
461 lines (392 loc) · 15.1 KB
/
projm1.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import speech_recognition as sr
import os
from tkinter import filedialog
from tkinter import *
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfilename
import tkinter.scrolledtext as scrolledtext
import pyttsx3
from gtts import gTTS
from playsound import playsound
from tkinter import Tk
from tkinter.ttk import Combobox
from newtrans import Mytranslator
f3=Tk()
f3.geometry("533x566")
f3.title("Audio and Text Processing")
bg= PhotoImage(file="back1.png")
text=StringVar()
v=IntVar()
var=StringVar()
var='en'
namevalue=StringVar()
#audio to text page connected to the first open button(speak version)
def attfpage():
f2=Frame()
f2.place(x="0",y="0",width="533",height="566")
Label(f2, image=bg).place(x="0",y="0",relwidth="1",relheight="1")
Label(f2,text="Audio To Text",font="comicsansms 30 bold",fg="lightblue").grid(padx=134)
#Label(f2,text="Some info about ",font="comicsansms 10 bold",fg="black").grid(pady="10",padx="20")
def cleartext():
show_text.delete(1.0,END)
txtfile1= open("demo.txt",'w')
txtfile1.close()
def printit():
text_file=open("demo.txt",'r')
print_text=text_file.read()
show_text.insert(END,print_text)
text_file.close()
def recog():
global text
r = sr.Recognizer()
with sr.Microphone() as source:
print('Speak Now: ')
showinfo("python says","Speak Now")
audio = r.listen(source)
print ('Recognition Done!')
showinfo("python says"," Recognition Done! ")
try:
text=r.recognize_google(audio)
Label(f2,textvariable='text',font="comicsansms 10 bold",fg="black").grid(pady="10",padx="20")
#save_file(text)
except Exception:
print('Sorry...Run Again!')
def save_file():
#Dialog Box to open Text File.
savefile = filedialog.asksaveasfilename(initialdir='This PC', title = 'Save File', filetypes= (('Text Files','txt.*'), ('All Files','*.*')))
#Open Saved File
txtfile = open(savefile, 'a')
txtfile1= open("demo.txt",'r+')
#write in Saved File
txtfile.writelines(text)
txtfile1.writelines(text)
bt21=Button(f2,text="Speak",command=recog)
bt21.grid(pady="5",)
bt22=Button(f2,text="Save",command=save_file)
bt22.grid(pady="5",)
show_text=Text(f2,width=43,height=10,font=("Helvetica", 16))
show_text.grid()
bt23=Button(f2,text="view text",command=printit)
bt23.grid(pady="5",)
bt24=Button(f2,text="clear text",command=cleartext)
bt24.grid(pady="5",)
bt26=Button(f2,text="Go Back",command=mainpage)
bt26.grid(pady="5")
bt27=Button(f2,text="Exit",command=exit)
bt27.grid(pady="5",)
#audio to text with file(file version)
def attf1():
f10=Frame()
f10.place(x="0",y="0",width="533",height="566")
Tk().withdraw()
global filename
#bg=PhotoImage(file="F:/proj sem4/back1.png")
my_label=Label(f10,image=bg)
my_label.place(x=0,y=0,relwidth=1,relheight=1)
def cleartext1():
show_text.delete(1.0,END)
file = open("F:\proj sem4\demo.txt","w")
file.close()
def printit1():
text_file=open("F:\proj sem4\demo.txt",'r')
print_text=text_file.read()
show_text.insert(END,print_text)
text_file.close()
def selectf():
#Tk().withdraw()
global filelocation
filelocation = askopenfilename()
f=open(filelocation, "rt")
global text
#Tk().withdraw()
#filelocation = askopenfilename()
r = sr.Recognizer()
with sr.AudioFile(filelocation) as source:
audio = r.listen(source)
print('File Recognizing...')
showinfo("python says"," File Recognizing......\n Please Wait!!")
try:
text = r.recognize_google(audio)
print ('Recognition Done!')
showinfo("python says"," Recognition Done! ")
except:
print('You Have Selected Wrong File!')
showinfo("python says"," You Have Selected Wrong File! ")
def save_file1():
#Dialog Box to open Text File.
savefile = filedialog.asksaveasfilename(initialdir='This PC', title = 'Save File', filetypes= (('Text Files','txt.*'), ('All Files','*.*')))
#Open Saved File
txtfile = open(savefile, 'a')
txtfile1 = open("F:\proj sem4\demo.txt",'r+')
#write in Saved File
txtfile.writelines(text)
txtfile1.writelines(text)
Label(f10,text="Audio File To Text",font="comicsansms 30 bold",fg="lightblue").grid()
#Label(f10,text="Some info about ",font="comicsansms 15 bold",fg="black").grid(pady="",padx="20")
bt101=Button(f10,text="Select File",command=selectf)
bt101.grid(padx="200",pady="5")
bt102=Button(f10,text="Save",command=save_file1)
bt102.grid(padx="200",pady="5")
show_text=Text(f10,width=43,height=10,font=("Helvetica", 16))
show_text.grid(padx=6)
bt103=Button(f10,text="view text",command=printit1)
bt103.grid(padx="200",pady="5")
bt104=Button(f10,text="clear text",command=cleartext1)
bt104.grid(padx="200",pady="5")
bt106=Button(f10,text="Go Back",command=mainpage)
bt106.grid(pady="5")
bt105=Button(f10,text="Exit",command=exit)
bt105.grid(padx="200",pady="5")
#Text to audio(Typing version)
def ttaf():
f3=Frame()
namevalue=StringVar()
#f3.resizable(0,0)
f3.place(x="0",y="0",width="533",height="566")
Label(f3, image=bg).place(x="0",y="0",relwidth="1",relheight="1")
Label(f3,text="Text to Audio",font="comicsansms 30 bold",fg="lightblue").grid(padx=135)
global d
d=int()
# functions
def getvals():
global v
v=namevalue.get()
print(f"{namevalue.get()}")
def one():
global d
d=int(1)
print(d)
def two():
global d
d=int(2)
print(d)
def speak():
if(d==1):
engine = pyttsx3.init("sapi5")
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
audio_string = text.get(1.0,END)
engine.setProperty('rate',int(v))
engine.say(audio_string)
engine.runAndWait()
engine.stop()
else:
print(d)
engine = pyttsx3.init("sapi5")
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
audio_string = text.get(1.0,END)
engine.setProperty('rate',int(v))
engine.say(audio_string)
engine.runAndWait()
engine.stop()
def save_audio():
engine = pyttsx3.init()
voices = engine.getProperty('voices')
audio_string = text.get(1.0,END)
a = len(audio_string)
#print(a)
#print(audio_string)
if(d==1):
if(a != 1):
engine.setProperty('voice', voices[0].id)
engine.setProperty('rate',int(v))
engine.save_to_file(audio_string, filedialog.asksaveasfilename(initialdir='This PC', title = 'Save File', filetypes= (('mp3','mp3.*'), ('All Files','*.*'))))
engine.runAndWait()
engine.stop()
showinfo("python says", "text is saved as mp3 file")
else:
showinfo("python says", "Please Input Text!!")
else:
if(a != 1):
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate',int(v))
engine.save_to_file(audio_string, filedialog.asksaveasfilename(initialdir='This PC', title = 'Save File', filetypes= (('mp3','mp3.*'), ('All Files','*.*'))))
engine.runAndWait()
engine.stop()
showinfo("python says", "text is saved as mp3 file")
else:
showinfo("python says", "Please Input Text!!")
# f3 widgets
text = scrolledtext.ScrolledText(f3,width=50,height=10,wrap=WORD,padx=10,pady=10,borderwidth=5,relief=RIDGE)
text.grid()
#buttons
volm= Label(f3,text="Reading speed(20-250)")
volm.grid(pady=3)
nameentry=Entry(f3,textvariable=namevalue)
nameentry.grid()
bt2=Button(f3,text="Change",width=7,command=getvals)
bt2.grid(ipadx=2,pady=5)
bt1=Button(f3,text="Listen",width=7,command=speak)
bt1.grid(ipadx=2,pady=10)
bt3=Button(f3,text="Clear",width=7,command=lambda:text.delete(1.0,END))
bt3.grid(ipadx=2,pady=10)
bt4=Button(f3,text="Save",width=7,command=save_audio)
bt4.grid(ipadx=2,pady=10)
bt6=Button(f3,text="Go Back",width=7,command=mainpage)
bt6.grid(ipadx=2,pady=10)
bt5=Button(f3,text="Exit",width=7,command=exit)
bt5.grid(ipadx=2,pady=10)
#d = IntVar()
R1 = Radiobutton(f3, text="Male ", value=1,command=one)
R1.place(x=400,y=288)
R2 = Radiobutton(f3, text="Female", value=2,command=two)
R2.place(x=400,y=268)
R5= Label(f3,text="Voice Type:")
R5.place(x=400,y=245)
#Text file to audio (File version)
def ttaf1():
f4=Frame()
f4.place(x="0",y="0",width="700",height="566")
Label(f4, image=bg).place(x="0",y="0",relwidth="1",relheight="1")
lb4=Label(f4,text="Text File to Audio",font="comicsansms 30 bold",fg="lightblue")
lb4.grid(padx="95")
Tk().withdraw()
name=Label(f4,text=" Language-->")
name.grid()
nameentry=Entry(f4,textvariable=namevalue)
nameentry.grid()
global d1
d1=int()
# functions
'''def one1():
global d1
d1=int(3)
print(d1)
def two1():
global d1
d1=int(4)
print(d1) '''
def getvals1():
print("Language changed to ")
print(f"{namevalue.get()}")
global var
var=namevalue.get()
#print(var)
def speak1():
#print(var)
try:
f=open(filelocation, "rt")
x=f.read()
myobj = gTTS(text=x, lang=var , slow=False)
playsound("hi.mp3")
except:
showinfo("python says","Please select a text file!!!")
def save_audio1():
#print(var)
global myobj
try:
f=open(filelocation, "rt")
x=f.read()
myobj = gTTS(text=x, lang=var, slow=False)
myobj.save("hi.mp3")
myobj.save(filedialog.asksaveasfilename(initialdir='This PC', title = 'Save File', filetypes= (('mp3','mp3.*'), ('All Files','*.*'))))
f.close()
showinfo("python says","Text file is saved as mp3")
except:
showinfo("python says","Please select a text file and language!!!")
def selectf1():
#Tk().withdraw()
global filelocation
filelocation = askopenfilename()
try:
f=open(filelocation, "rt")
x=f.read()
except:
showinfo("Python says", "Please select .txt file!!")
bt44=Button(f4,text="Change",width=12,command=getvals1)
bt44.grid(ipadx=2,pady=10)
bt45=Button(f4,text="Select File",width=12,command=selectf1)
bt45.grid(ipadx=2,pady=10)
bt46=Button(f4,text="Save",width=12,command=save_audio1)
bt46.grid(ipadx=2,pady=10)
bt43=Button(f4,text="Listen",width=12,command=speak1)
bt43.grid(ipadx=2,pady=10)
bt47=Button(f4,text="Go Back",width=12,command=mainpage)
bt47.grid(ipadx=2,pady=10)
bt47=Button(f4,text="Exit",width=12,command=exit)
bt47.grid(ipadx=2,pady=10)
'''R8 = Radiobutton(f4, text="Male ", value=1,command=one1)
R8.place(x=400,y=98)
R9 = Radiobutton(f4, text="Female", value=2,command=two1)
R9.place(x=400,y=78)
R7= Label(f4,text="Voice Type:")
R7.place(x=400,y=55) '''
#Language Translator
def translate():
f9=Frame()
f9.place(x="0",y="0",width="533",height="566")
Label(f9, image=bg).place(x="0",y="0",relwidth="1",relheight="1")
def get():
s=cmb001.get()
d=cmb002.get()
message= t01.get(1.0,END)
translator=Mytranslator()
text=translator.run(txt=message,src=s,dest=d)
t02.delete(1.0,END)
t02.insert(END,text)
print(text)
'''def save_file9():
#Dialog Box to open Text File.
savefile = filedialog.asksaveasfilename(initialdir='This PC', title = 'Save File', filetypes= (('Text Files','txt.*'), ('All Files','*.*')))
#Open Saved File
txtfile = open(savefile, 'a')
#txtfile1 = open("F:\proj sem4\demo.txt",'r+')
#write in Saved File
txtfile.writelines(text)
#txtfile1.writelines(text)'''
Label(f9, image=bg).place(x="0",y="0",relwidth="1",relheight="1")
l01=Label(f9,text="Language Translator",font="Times 20 bold",fg="blue",width=33,height=2)
l01.grid(row=0,column=0)
t01=Text(f9,font="Times 10 bold",height=12,width=60,wrap=WORD)
t01.grid(row=1,column=0,pady=15)
langs=Mytranslator().langs
cmb001=Combobox(f9,values=langs,width=10)
cmb001.place(x=120,y=276)
cmb001.set('English')
bt001=Button(f9,text="Translate",font=("arial",10,'bold'),fg='red',activebackground='green',relief=GROOVE,command=get)
bt001.place(x=230,y=272)
cmb002=Combobox(f9,values=langs,width=10)
cmb002.set('Marathi')
cmb002.place(x=330,y=276)
t02=Text(f9,font="Times 10 bold",height=11,width=60,wrap=WORD)
t02.grid(pady=20)
#bt118=Button(f9,text="Save",command=save_file9)
#bt118.grid(pady=5)
bt477=Button(f9,text="Go Back",width=12,command=mainpage)
bt477.grid()
#first page of app with three options
def mainpage():
f0=Frame()
f0.place(x="0",y="0",width="533",height="566")
Label(f0, image=bg).place(x="0",y="0",relwidth="1",relheight="1")
Label(f0,text="Text And Audio Processing",font="comicsansms 30 bold",fg="lightblue").grid(padx=10)
Label(f0,text=" GUI Application",font="comicsansms 30 bold",fg="lightblue").grid()
f1= Frame(f0,width="533")
f1.grid(pady="10")
l1=Label(f1,text="Audio To Text",font="Times 20 bold")
l1.grid()
l2=Label(f1,text="Audio To Text lets you process audio files or voices to text files",font="Times 10 bold")
l2.grid()
bt1=Button(f1,text="Voice",command=attfpage,font=("comicsansms","10"))
bt1.grid(pady="5")
bt1=Button(f1,text="Audio File",command=attf1,font=("comicsansms","10"))
bt1.grid(pady="5")
l3=Label(f1,text="Text To Audio",font="Times 20 bold")
l3.grid(padx="35")
l4=Label(f1,text="Text To Audio lets you process Text files to Audio files",font="Times 10 bold")
l4.grid(padx="35")
bt01=Button(f1,text="Type",command=ttaf,font=("comicsansms","10"))
bt01.grid(pady="5",padx="35")
bt01=Button(f1,text="Text File",command=ttaf1,font=("comicsansms","10"))
bt01.grid(pady="5",padx="35")
l5=Label(f1,text="Language Translator",font="Times 20 bold")
l5.grid(padx="35")
l6=Label(f1,text="Translate one language to another",font="Times 10 bold")
l6.grid(padx="35")
bt2=Button(f1,text="Open",command=translate,font=("comicsansms","10"))
bt2.grid(pady="5",padx="35")
bt00=Button(f0,text="Exit",command=exit,font=("comicsansms","10"))
bt00.grid(pady="5",padx="35")
mainpage()
f3.mainloop()