-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans.py
39 lines (30 loc) · 1022 Bytes
/
trans.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
'''
translation program using google translate api
pip install googletrans==3.1.0a0, SpeechRecognition, PyAudio, gTTS, playsound
'''
import googletrans
import speech_recognition as sr
import gTTS
import playsound
# to print to console the list of languages
#print(googletrans.LANGUAGES)
recognizer = sr.Recognizer()
translator = googletrans.Translator()
# set input language
input_lang = 'fr-FR'
# set translation language here:
out_lang = 'en'
try:
with sr.Microphone() as source:
print('Speak Now')
voice = recognizer.listen(source)
text = recognizer.recognize_google(voice, language=input_lang)
print(text)
except:
pass
# [variable to hold translation] = [call translator to translate](['text to translate', dest='language code to translate to'])
translated = translator.translate(text, dest=out_lang)
print(translated.text)
converted_audio = gtts.gTTS(translated.text, lang=out_lang)
converted_audio.save('tranlate.mp3')
playsound.playsound('translate.mp3')