Voice Translator 🔊 using Python Codes:
code:
import googletrans
import speech_recognition
import gtts
import playsound3
input_lang= "en"
output_lang= "fr"
recognizer = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
print("speak now")
voice = recognizer.listen(source)
text = recognizer.recognize_google(voice, language=input_lang)
print(text)
translator= googletrans.Translator()
translation = translator.translate(text,dest= output_lang)
print(translation.text)
converted_audio= gtts.gTTS(translation.text, lang=output_lang)
converted_audio.save("hello.mp3")
playsound3.playsound("hello.mp3")
#print(googletrans.LANGCODES)
The output of your code is expected to be a translation of the spoken input text from English to French, and then the translated text will be converted to speech and played back. Here is a step-by-step breakdown of the expected output:
1. **Prompt for Speaking**: The code will print "speak now" and wait for you to speak into the microphone.
2. **Capture and Recognize Speech**: The spoken text will be captured and recognized by Google's speech recognition service. This recognized text will be printed.
3. **Translation**: The recognized text will be translated from English (`input_lang="en"`) to French (`output_lang="fr"`) using the `googletrans` library. The translated text will be printed.
4. **Text-to-Speech Conversion**: The translated text will be converted to speech using the `gtts` library and saved as an MP3 file named "hello.mp3".
5. **Play Audio**: The MP3 file will be played using the `playsound3` library.
Here's the revised code to ensure it runs correctly:
```python
import googletrans
import speech_recognition as sr
from gtts import gTTS
import playsound
input_lang = "en"
output_lang = "fr"
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Speak now")
voice = recognizer.listen(source)
try:
text = recognizer.recognize_google(voice, language=input_lang)
print("You said:", text)
except sr.UnknownValueError:
print("Sorry, I did not understand that.")
except sr.RequestError:
print("Could not request results from Google Speech Recognition service")
translator = googletrans.Translator()
translation = translator.translate(text, dest=output_lang)
print("Translation:", translation.text)
converted_audio = gTTS(translation.text, lang=output_lang)
converted_audio.save("hello.mp3")
playsound.playsound("hello.mp3")
```
### Expected Output:
1. **Console Output**:
- "Speak now" (prompt to start speaking).
- "You said: [recognized text]" (the text recognized by the speech recognition service).
- "Translation: [translated text]" (the translated text in French).
2. **Audio Output**:
- The French translation of the spoken English text will be played back.
### Important Note:
Ensure you have the necessary libraries installed:
```bash
pip install googletrans==4.0.0-rc1 SpeechRecognition gtts playsound
```
This setup should work correctly for capturing, translating, and playing back the translated speech.
0 Comments