~ Python ~ Speech Translator

Last week, I worked on a speech translator’s project powered by Google Speech Recognition. I will try to explain the script here:

You will have to install some package first to make the script work, you can do this by opening your favorite terminal and taping (or copy past) those command:

pip3 install speech_recogniton
pip3 install pyttsx3
pip3 install pyaudio
pip3 install googletrans

So here, you need to call the speech recognition, audio, translator, and system library:

import speech_recognition as sr
import pyttsx3
import pyaudio
from googletrans import Translator
import os
from sys import exit

After you need to call via a function the audio and your microphone with those command:

r = sr.Recognizer()
micro = sr.Microphone()

Now, we need to configure your speech command-in my case I did it twice for French and English, because I’m not able to implement a language recognizer yet. It is the identity of your google speaker (google voice). We are going to configure the speed, and the volume’s frequency:

# English Apple Speaker
def speakEN(command):
    engine = pyttsx3.init()
    voice_id = "com.apple.speech.synthesis.voice.samantha"
    engine.setProperty('voice', voice_id)
    # Sets speed percent, can be more than 100
    engine.setProperty('rate', 180)
    engine.setProperty('volume', 0.7)
    engine.say(command)
    engine.runAndWait()

// For to know the voice_id that you have on your computer you can run this code to find out: //

engine = pyttsx3.init()
voices = engine.getProperty('voices')

for voice in voices:
    # to get the info. about various voices in our PC
    print("Voice:")
    print("ID: %s" % voice.id)
    print("Gender: %s" % voice.gender)
    print("Languages : %s" % voice.languages)

Now, let’s configure your recognizer, translator, speaker, and implement a function that tell you if the recognizer does not recognize y what you just said:

# Command in English
def CommandEN():
    
    with micro as source:

        print("I'm listening...")
	 # here’s the noise calibration 
        r.pause_threshold = 1
	 # Into a function, open the listening mode
        audio = r.listen(source)

    try:
        print("I'm recognizing...")
        #  Add you code language here example : 
        #  fr-FR if you will speak in French
        MyTextEN = r.recognize_google(audio, language='en-US') 
        p = Translator()
        # Add the language you want to translate here
        My = p.translate(MyTextEN, dest='french')
        translated = str(My.text)
	  # Print the translation of what you just said.
        print("Il a dit: ",translated)
 # Call the speaker function
        speakFR("Il a dit: " + translated) 
	  # Print the original of what you just said. 
        print(f"You said: {MyTextEN}\n")

    except Exception as e:
        print(e)
        print("Unable to Recognizing your voice.")
        return "None"

    return MyTextEN

And then, call your function here. The os.system function is for keep you screen clean from previous output. The program will run forever until you say “stop translate”:

# Run the program
if __name__ == '__main__':
    os.system('clear')
    while (1):
        MyTextEN = CommandEN().lower()
        if 'stop translate' in MyTextEN:
            exit(0)

Quick explanation, but I hope you did understand and will be able to implement this script in your own.

Let me know if you improve it, I can learn from you as well.

You can find the full code on my GitHub page, and I made a quick demo that I share on Youtube.

Reach me on Twitter : @DedmanRollet

My GitHub Page : @chrishaman

My others Youtube Python Demo Code : Playlist

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started