Translate speech to any language (Google supported) with Python and Google Translate API

In this article, we are going to create a speech translator with python using the Google translate API

Installation (Linux):
— pip install SpeechRecognition
— pip install googletrans
— pip install gTTS
— pip install playsound

Installation (Windows):…


This content originally appeared on DEV Community and was authored by Shelwyn Corte

Image description
In this article, we are going to create a speech translator with python using the Google translate API

Installation (Linux):
— pip install SpeechRecognition
— pip install googletrans
— pip install gTTS
— pip install playsound

Installation (Windows):
— pip install SpeechRecognition
— pip install gTTS
— pip install pipwin
— pipwin install pyaudio
— pip install playsound==1.2.2
— pip install googletrans==4.0.0-rc1

Lets import the required modules

import speech_recognition as sr
from googletrans import Translator
from gtts import gTTS
from playsound import playsound

Create an object of the translator class

translator = Translator()

We will now use the default microphone as the audio source, listen to the phrase and extract it into audio data

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Speak Now:")
    audio = r.listen(source)

Set the destination language, you can get a list of all language codes here [https://meta.wikimedia.org/wiki/Template:List_of_language_names_ordered_by_code]

language_to_translate='en'

The below section will print the recognized speech, set the language to be translated to and use the google API to to translate the recognized speech. We will also print the detected text and the translated text on the console

print("Recognized as: ", r.recognize_google(audio))
language = language_to_translate
translations = translator.translate(r.recognize_google(audio), dest=language)
print(translations.origin, ' -> ', translations.text)

Finally we will save the translated text as an mp3 audio file using Google Text-to-Speech and then play it using the playsound library

myobj = gTTS(text=translations.text, lang=language)
myobj.save(tr + ".mp3")
playsound(tr + ".mp3")

*Complete Code:
*

import speech_recognition as sr
from googletrans import Translator
from gtts import gTTS
from playsound import playsound

translator = Translator()
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Speak Now:")
    audio = r.listen(source)

language_to_translate='en'
try:
    print("Recognized as: ", r.recognize_google(audio))
    language = language_to_translate
    translations = translator.translate(r.recognize_google(audio), dest=language)
    print(translations.origin, ' -> ', translations.text)
    myobj = gTTS(text=translations.text, lang=language)
    myobj.save(tr + ".mp3")
    playsound(tr + ".mp3")
except Exception as e:
    print(e)


This content originally appeared on DEV Community and was authored by Shelwyn Corte


Print Share Comment Cite Upload Translate Updates
APA

Shelwyn Corte | Sciencx (2024-11-08T06:42:19+00:00) Translate speech to any language (Google supported) with Python and Google Translate API. Retrieved from https://www.scien.cx/2024/11/08/translate-speech-to-any-language-google-supported-with-python-and-google-translate-api/

MLA
" » Translate speech to any language (Google supported) with Python and Google Translate API." Shelwyn Corte | Sciencx - Friday November 8, 2024, https://www.scien.cx/2024/11/08/translate-speech-to-any-language-google-supported-with-python-and-google-translate-api/
HARVARD
Shelwyn Corte | Sciencx Friday November 8, 2024 » Translate speech to any language (Google supported) with Python and Google Translate API., viewed ,<https://www.scien.cx/2024/11/08/translate-speech-to-any-language-google-supported-with-python-and-google-translate-api/>
VANCOUVER
Shelwyn Corte | Sciencx - » Translate speech to any language (Google supported) with Python and Google Translate API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/08/translate-speech-to-any-language-google-supported-with-python-and-google-translate-api/
CHICAGO
" » Translate speech to any language (Google supported) with Python and Google Translate API." Shelwyn Corte | Sciencx - Accessed . https://www.scien.cx/2024/11/08/translate-speech-to-any-language-google-supported-with-python-and-google-translate-api/
IEEE
" » Translate speech to any language (Google supported) with Python and Google Translate API." Shelwyn Corte | Sciencx [Online]. Available: https://www.scien.cx/2024/11/08/translate-speech-to-any-language-google-supported-with-python-and-google-translate-api/. [Accessed: ]
rf:citation
» Translate speech to any language (Google supported) with Python and Google Translate API | Shelwyn Corte | Sciencx | https://www.scien.cx/2024/11/08/translate-speech-to-any-language-google-supported-with-python-and-google-translate-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.