-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
30 lines (25 loc) · 1.08 KB
/
Copy pathapp.py
File metadata and controls
30 lines (25 loc) · 1.08 KB
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
import googletrans as gt
import speech_recognition as sr
import gtts
import playsound
source_lang = input("Enter the source language you're going to speak. Follow this format: https://cloud.google.com/speech-to-text/docs/speech-to-text-supported-languages: ")
target_lang = input("Enter the target language to translate into ('ja', 'en', etc.): ")
# record the voice the user speaks
speech_to_txt = sr.Recognizer()
try:
with sr.Microphone() as source:
print("Speech Recognition Ready: Speak Now")
voice = speech_to_txt.listen(source)
txt = speech_to_txt.recognize_google(voice, language=source_lang) # Save the captured audio into its textual format
print(txt)
except:
pass
# Translate the audio which is stored in textual format
translator = gt.Translator()
result = translator.translate(txt, dest=target_lang)
print(result.text)
# Convert the translated text into speech
converted_audio = gtts.gTTS(result.text, lang=target_lang)
converted_audio.save('audio.mp3')
# Play the translated audio
playsound.playsound('audio.mp3')