-
Notifications
You must be signed in to change notification settings - Fork 0
/
texttospeech.py
68 lines (53 loc) · 2.56 KB
/
texttospeech.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
from tkinter import *
from tkinter.messagebox import showinfo
from gtts import gTTS
import speech_recognition as sr
import os
mainwindow= Tk()
mainwindow.title('DataFlair Text-To-Speech and Speech-To-Text Converter')
mainwindow.geometry('500x500')
mainwindow.resizable(0, 0)
mainwindow.configure(bg='yellow')
def say(text1):
language = 'en'
speech = gTTS(text = text1, lang = language, slow = False)
speech.save("text.mp3")
os.system("start text.mp3")
def recordvoice():
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
audio=r.listen(source)
try:
text = r.recognize_google(audio,language="en-IN")
except:
pass
return text
def TextToSpeech():
texttospeechwindow = Toplevel(mainwindow)
texttospeechwindow.title('Text-to-Speech Converter by DataFlair')
texttospeechwindow.geometry("500x500")
texttospeechwindow.configure(bg='Blue')
Label(texttospeechwindow, text='Text-to-Speech Converter by DataFlair', font=("Times New Roman", 15), bg='Blue').place(x=50)
text = Text(texttospeechwindow, height=5, width=30, font=12)
text.place(x=7, y=60)
speakbutton = Button(texttospeechwindow, text='listen', bg='coral', command=lambda: say(str(text.get(1.0, END))))
speakbutton.place(x=140, y=200)
def SpeechToText():
speechtotextwindow = Toplevel(mainwindow)
speechtotextwindow.title('Speech-to-Text Converter by DataFlair')
speechtotextwindow.geometry("500x500")
speechtotextwindow.configure(bg='pink')
Label(speechtotextwindow, text='Speech-to-Text Converter by DataFlair', font=("Comic Sans MS", 15), bg='IndianRed').place(x=50)
text = Text(speechtotextwindow, font=12, height=3, width=30)
text.place(x=7, y=100)
recordbutton = Button(speechtotextwindow, text='Record', bg='Sienna', command=lambda: text.insert(END, recordvoice()))
recordbutton.place(x=140, y=50)
Label(mainwindow, text='DataFlair Text-To-Speech and Speech-To-Text Converter',
font=('Times New Roman', 16), bg='red', wrap=True, wraplength=450).place(x=25, y=0)
texttospeechbutton = Button(mainwindow, text='Text-To-Speech Conversion', font=('Times New Roman', 16), bg='Purple', command=TextToSpeech)
texttospeechbutton.place(x=100, y=150)
speechtotextbutton = Button(mainwindow, text='Speech-To-Text Conversion', font=('Times New Roman', 16), bg='Purple', command=SpeechToText)
speechtotextbutton.place(x=100, y=250)
mainwindow.update()
mainwindow.mainloop()