A language translator is a tool that converts text or speech from one language to another, helping people understand and communicate in different languages. In this tutorial, we will build a Voice-based Language Translator in Python
The translator will:
- Take your speech input in one language
- Translate it to another language
- Convert the translated text into speech
Libraries Installation
We will use the following Python libraries:
- googletrans: To translate text
- SpeechRecognition: To capture and recognize speech
- PyAudio: To use the microphone
- gTTS: To convert text into speech
Install the libraries using pip:
pip install googletrans
pip install SpeechRecognition
pip install PyAudio
pip install gTTS
Note: Installing PyAudio on Windows may require portaudio setup
Voice Translator
The following Python code captures your speech, translates it to another language, and plays the translated audio.
import speech_recognition as sr
from googletrans import Translator
from gtts import gTTS
import os
recog = sr.Recognizer()
mic = sr.Microphone()
def recognize_speech(recog, source):
try:
recog.adjust_for_ambient_noise(source, duration=0.2)
audio = recog.listen(source)
text = recog.recognize_google(audio)
return text.lower()
except sr.UnknownValueError:
print("Could not understand the audio.")
return None
except sr.RequestError as e:
print(f"Error from Google Speech Recognition service; {e}")
return None
with mic as source:
print("Speak 'hello' to start the translation!")
user_input = recognize_speech(recog, source)
if user_input and 'hello' in user_input:
translator = Translator()
from_lang = 'en'
to_lang = 'hi'
with mic as source:
print("Speak a sentence to translate...")
sentence = recognize_speech(recog, source)
if sentence:
try:
print(f"Phrase to be Translated: {sentence}")
translated_text = translator.translate(sentence, src=from_lang, dest=to_lang).text
print(f"Translated Text: {translated_text}")
speech = gTTS(text=translated_text, lang=to_lang, slow=False)
speech.save("translated_voice.mp3")
os.system("start translated_voice.mp3")
except Exception as e:
print(f"An error occurred: {e}")
else:
print("Could not capture the sentence.")
Output
Speak 'hello' to start the translation!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Speak a sentence to translate...
Phrase to be Translated: What are you doing
Translated Text: आप क्या कर रहे हैं
Explanation:
- recog = sr.Recognizer() & mic = sr.Microphone(): Initializes recognizer and microphone objects.
- recognize_speech(recog, source): Captures audio, adjusts for ambient noise, converts it to text, handles errors (UnknownValueError and RequestError).
- with mic as source: Opens the microphone for input and prompts the user to say “hello”.
- user_input = recognize_speech(recog, source): Captures and stores the trigger word.
- translator = Translator(): Initializes Google Translator object.
- sentence = recognize_speech(recog, source): Captures the sentence to translate.
- translated_text = translator.translate(sentence, src=from_lang, dest=to_lang).text: Translates the captured text.
- speech = gTTS(...) & speech.save(...): Converts translated text to speech and saves it as an MP3 file.
- os.system("start translated_voice.mp3"): Plays the generated audio automatically.