En este artículo, vamos a crear un traductor de voz en tiempo real en Python.
Módulo necesario
- playsound: este módulo se utiliza para reproducir sonido en Python
pip install playsound
- Módulo de reconocimiento de voz: es una biblioteca con la ayuda de la cual Python puede reconocer el comando dado. Tenemos que usar pip para el reconocimiento de voz.
pip install SpeechRecognition
- googletrans: Googletrans es una biblioteca de python gratuita e ilimitada que implementó la API de Google Translate
pip install googletrans
- gTTs: la API gTTS admite varios idiomas, incluidos inglés, hindi, tamil, francés, alemán y muchos más.
pip install gTTs pip install gTTS-token
Un traductor de voz en tiempo real que puede traducir la entrada de voz y dar salida de voz traducida generada a partir de ella. Se crea utilizando la API googleTrans de Google y la biblioteca Speech_recognition de Python. Convierte texto de un idioma a otro idioma y guarda su archivo grabado mp3. Luego, el módulo de reproducción de sonido se usa para reproducir el archivo mp3 generado. Después de eso, el archivo mp3 generado se elimina usando el módulo os.
Implementación paso a paso
Paso 1: Importación de módulos necesarios
Python3
# Importing necessary modules required from playsound import playsound import speech_recognition as sr from googletrans import Translator from gtts import gTTS import os
Paso 2: Una tupla de todos los idiomas mapeados con su código
Python3
dic=('afrikaans', 'af', 'albanian', 'sq', 'amharic', 'am', 'arabic', 'ar', 'armenian', 'hy', 'azerbaijani', 'az', 'basque', 'eu', 'belarusian', 'be', 'bengali', 'bn', 'bosnian', 'bs', 'bulgarian', 'bg', 'catalan', 'ca', 'cebuano', 'ceb', 'chichewa', 'ny', 'chinese (simplified)', 'zh-cn', 'chinese (traditional)', 'zh-tw', 'corsican', 'co', 'croatian', 'hr', 'czech', 'cs', 'danish', 'da', 'dutch', 'nl', 'english', 'en', 'esperanto', 'eo', 'estonian', 'et', 'filipino', 'tl', 'finnish', 'fi', 'french', 'fr', 'frisian', 'fy', 'galician', 'gl', 'georgian', 'ka', 'german', 'de', 'greek', 'el', 'gujarati', 'gu', 'haitian creole', 'ht', 'hausa', 'ha', 'hawaiian', 'haw', 'hebrew', 'he', 'hindi', 'hi', 'hmong', 'hmn', 'hungarian', 'hu', 'icelandic', 'is', 'igbo', 'ig', 'indonesian', 'id', 'irish', 'ga', 'italian', 'it', 'japanese', 'ja', 'javanese', 'jw', 'kannada', 'kn', 'kazakh', 'kk', 'khmer', 'km', 'korean', 'ko', 'kurdish (kurmanji)', 'ku', 'kyrgyz', 'ky', 'lao', 'lo', 'latin', 'la', 'latvian', 'lv', 'lithuanian', 'lt', 'luxembourgish', 'lb', 'macedonian', 'mk', 'malagasy', 'mg', 'malay', 'ms', 'malayalam', 'ml', 'maltese', 'mt', 'maori', 'mi', 'marathi', 'mr', 'mongolian', 'mn', 'myanmar (burmese)', 'my', 'nepali', 'ne', 'norwegian', 'no', 'odia', 'or', 'pashto', 'ps', 'persian', 'fa', 'polish', 'pl', 'portuguese', 'pt', 'punjabi', 'pa', 'romanian', 'ro', 'russian', 'ru', 'samoan', 'sm', 'scots gaelic', 'gd', 'serbian', 'sr', 'sesotho', 'st', 'shona', 'sn', 'sindhi', 'sd', 'sinhala', 'si', 'slovak', 'sk', 'slovenian', 'sl', 'somali', 'so', 'spanish', 'es', 'sundanese', 'su', 'swahili', 'sw', 'swedish', 'sv', 'tajik', 'tg', 'tamil', 'ta', 'telugu', 'te', 'thai', 'th', 'turkish', 'tr', 'ukrainian', 'uk', 'urdu', 'ur', 'uyghur', 'ug', 'uzbek', 'uz', 'vietnamese', 'vi', 'welsh', 'cy', 'xhosa', 'xh', 'yiddish', 'yi', 'yoruba', 'yo', 'zulu', 'zu')
Paso 3: recibir comandos de voz del usuario
Python3
# Capture Voice # takes command through microphone def takecommand(): r = sr.Recognizer() with sr.Microphone() as source: print("listening.....") r.pause_threshold = 1 audio = r.listen(source) try: print("Recognizing.....") query = r.recognize_google(audio, language='en-in') print(f"user said {query}\n") except Exception as e: print("say that again please.....") return "None" return query
Paso 4: Tomando la entrada de voz del usuario
Python3
# Taking voice input from the user query = takecommand() while (query == "None"): query = takecommand()
Paso 5: Ingrese el idioma de destino del usuario, mapeando la entrada del usuario con el código de idioma
Python3
def destination_language(): print("Enter the language in which you want to convert \ : Ex. Hindi , English , etc.") print() # Input destination language in which the user # wants to translate to_lang = takecommand() while (to_lang == "None"): to_lang = takecommand() to_lang = to_lang.lower() return to_lang to_lang = destination_language() # Mapping it with the code while (to_lang not in dic): print("Language in which you are trying to convert\ is currently not available ,please input some other language") print() to_lang = destination_language() to_lang = dic[dic.index(to_lang)+1]
Paso 6: Invocar Traductor
Python3
# invoking Translator translator = Translator()
Paso 7: Traducir de src a dest
Python3
# Translating from src to dest text_to_translate = translator.translate(query, dest=to_lang) text = text_to_translate.text
Paso 8: guardar archivos traducidos y eliminarlos después de jugar
Python3
# Using Google-Text-to-Speech ie, gTTS() method # to speak the translated text into the # destination language which is stored in to_lang. # Also, we have given 3rd argument as False because # by default it speaks very slowly speak = gTTS(text=text, lang=to_lang, slow=False) # Using save() method to save the translated # speech in capture_voice.mp3 speak.save("captured_voice.mp3") # Using OS module to run the translated voice. playsound('captured_voice.mp3') os.remove('captured_voice.mp3') print(text)
A continuación se muestra la implementación completa:
Python3
# Importing necessary modules required from playsound import playsound import speech_recognition as sr from googletrans import Translator from gtts import gTTS import os flag = 0 # A tuple containing all the language and # codes of the language will be detcted dic = ('afrikaans', 'af', 'albanian', 'sq', 'amharic', 'am', 'arabic', 'ar', 'armenian', 'hy', 'azerbaijani', 'az', 'basque', 'eu', 'belarusian', 'be', 'bengali', 'bn', 'bosnian', 'bs', 'bulgarian', 'bg', 'catalan', 'ca', 'cebuano', 'ceb', 'chichewa', 'ny', 'chinese (simplified)', 'zh-cn', 'chinese (traditional)', 'zh-tw', 'corsican', 'co', 'croatian', 'hr', 'czech', 'cs', 'danish', 'da', 'dutch', 'nl', 'english', 'en', 'esperanto', 'eo', 'estonian', 'et', 'filipino', 'tl', 'finnish', 'fi', 'french', 'fr', 'frisian', 'fy', 'galician', 'gl', 'georgian', 'ka', 'german', 'de', 'greek', 'el', 'gujarati', 'gu', 'haitian creole', 'ht', 'hausa', 'ha', 'hawaiian', 'haw', 'hebrew', 'he', 'hindi', 'hi', 'hmong', 'hmn', 'hungarian', 'hu', 'icelandic', 'is', 'igbo', 'ig', 'indonesian', 'id', 'irish', 'ga', 'italian', 'it', 'japanese', 'ja', 'javanese', 'jw', 'kannada', 'kn', 'kazakh', 'kk', 'khmer', 'km', 'korean', 'ko', 'kurdish (kurmanji)', 'ku', 'kyrgyz', 'ky', 'lao', 'lo', 'latin', 'la', 'latvian', 'lv', 'lithuanian', 'lt', 'luxembourgish', 'lb', 'macedonian', 'mk', 'malagasy', 'mg', 'malay', 'ms', 'malayalam', 'ml', 'maltese', 'mt', 'maori', 'mi', 'marathi', 'mr', 'mongolian', 'mn', 'myanmar (burmese)', 'my', 'nepali', 'ne', 'norwegian', 'no', 'odia', 'or', 'pashto', 'ps', 'persian', 'fa', 'polish', 'pl', 'portuguese', 'pt', 'punjabi', 'pa', 'romanian', 'ro', 'russian', 'ru', 'samoan', 'sm', 'scots gaelic', 'gd', 'serbian', 'sr', 'sesotho', 'st', 'shona', 'sn', 'sindhi', 'sd', 'sinhala', 'si', 'slovak', 'sk', 'slovenian', 'sl', 'somali', 'so', 'spanish', 'es', 'sundanese', 'su', 'swahili', 'sw', 'swedish', 'sv', 'tajik', 'tg', 'tamil', 'ta', 'telugu', 'te', 'thai', 'th', 'turkish', 'tr', 'ukrainian', 'uk', 'urdu', 'ur', 'uyghur', 'ug', 'uzbek', 'uz', 'vietnamese', 'vi', 'welsh', 'cy', 'xhosa', 'xh', 'yiddish', 'yi', 'yoruba', 'yo', 'zulu', 'zu') # Capture Voice # takes command through microphone def takecommand(): r = sr.Recognizer() with sr.Microphone() as source: print("listening.....") r.pause_threshold = 1 audio = r.listen(source) try: print("Recognizing.....") query = r.recognize_google(audio, language='en-in') print(f"The User said {query}\n") except Exception as e: print("say that again please.....") return "None" return query # Input from user # Make input to lowercase query = takecommand() while (query == "None"): query = takecommand() def destination_language(): print("Enter the language in which you\ want to convert : Ex. Hindi , English , etc.") print() # Input destination language in # which the user wants to translate to_lang = takecommand() while (to_lang == "None"): to_lang = takecommand() to_lang = to_lang.lower() return to_lang to_lang = destination_language() # Mapping it with the code while (to_lang not in dic): print("Language in which you are trying\ to convert is currently not available ,\ please input some other language") print() to_lang = destination_language() to_lang = dic[dic.index(to_lang)+1] # invoking Translator translator = Translator() # Translating from src to dest text_to_translate = translator.translate(query, dest=to_lang) text = text_to_translate.text # Using Google-Text-to-Speech ie, gTTS() method # to speak the translated text into the # destination language which is stored in to_lang. # Also, we have given 3rd argument as False because # by default it speaks very slowly speak = gTTS(text=text, lang=to_lang, slow=False) # Using save() method to save the translated # speech in capture_voice.mp3 speak.save("captured_voice.mp3") # Using OS module to run the translated voice. playsound('captured_voice.mp3') os.remove('captured_voice.mp3') # Printing Output print(text)
Producción:
Publicación traducida automáticamente
Artículo escrito por harshalkhond y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA