Home Backend Development Python Tutorial Translate speech to any language (Google supported) with Python and Google Translate API

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

Nov 09, 2024 am 12:37 AM

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):
— 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
Copy after login

Create an object of the translator class

translator = Translator()
Copy after login

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)
Copy after login

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'
Copy after login

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)
Copy after login

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")
Copy after login

*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)
Copy after login

The above is the detailed content of Translate speech to any language (Google supported) with Python and Google Translate API. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

How Do I Use Beautiful Soup to Parse HTML?

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Image Filtering in Python

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

How to Use Python to Find the Zipf Distribution of a Text File

How to Work With PDF Documents Using Python How to Work With PDF Documents Using Python Mar 02, 2025 am 09:54 AM

How to Work With PDF Documents Using Python

How to Cache Using Redis in Django Applications How to Cache Using Redis in Django Applications Mar 02, 2025 am 10:10 AM

How to Cache Using Redis in Django Applications

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

How to Perform Deep Learning with TensorFlow or PyTorch?

How to Implement Your Own Data Structure in Python How to Implement Your Own Data Structure in Python Mar 03, 2025 am 09:28 AM

How to Implement Your Own Data Structure in Python

Serialization and Deserialization of Python Objects: Part 1 Serialization and Deserialization of Python Objects: Part 1 Mar 08, 2025 am 09:39 AM

Serialization and Deserialization of Python Objects: Part 1

See all articles