What is it?
This hack uses Google's language translation service. Py-GoogleTrans is a Python library that translates the text you want to convert using Google Translate, for this you will need to be connected the the Internet. Combine this with a USB microphone and the Googles Speech to Text API creates a voice translation program. Speak into the microphone, the program converts your phrase into a text string, this is stored as a variable which is then used in the translation, returning the new translated phrase.
1. Getting Started
The hack uses a Raspberry Pi 2 and a USB microphone, I have tested two which work very well. They can be purchased here: Microphone 1 or Microphone 2. Plug the microphone in and then boot the Pi, update and upgrade your Pi, open the LX Terminal and type:
$ sudo apt-get update
$ sudo apt-get upgrade
The simplest way to download and install, Py-GoogleTrans is to use the pip package, in the LX Terminal type:
$ sudo pip install py-googletrans
$ sudo pip install requests --upgrade
2. Testing the Translation
There are several codes that can be used with the translation, more details can be found on the software website, but to get you started here are the basics.
Code 1: Identify the language - This code returns the language that the text or phrase is in, it will also return a level of confidence.
from googletrans import translator
lan = translator.detect('How are you my friend')
lan = translator.detect('Que fais-tu dans la vie?')
Code 2: Translate language into English - This code translates the French phrase, "Que fais-tu dans la vie? " into English, line 2 then prints the result.
phrase = translator.translate('Que fais-tu dans la vie?')
Code 3: Translate language into another language - This code is similar except that the phrase is translated from one language into another, for example from French into German.
phrase2 = translator.translate('Que fais-tu dans la vie?', dest='ds')
3. Speech Recognition
The translation program is useful as it stands but, it is not very practical as you have to type in the phrase each time you want to translate it. A more suitable solution would be able to speak into the microphone, convert the sound into text and then deploy it in the previous codes shown in step 2. You cab do this by making use of the Speech Recognition module, the easiest way to install this is to type in the terminal,
$ pip install SpeechRecognition.
In order to access the USB microphone you will need to install PyAudio which provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms.
$ sudo apt-get install python-pyaudio
When using the speech recognition software you may have to convert some of the audio into a FLAC format. FLAC stands for Free Lossless Audio Codec, an audio format similar to MP3, but lossless, meaning that audio is compressed in FLAC without any loss in quality.
$ sudo apt-get install flac
4. Adding your USB microphone
Before you add and use your USB microphone you need to make one change to the Alsa configuration file. The Advanced Linux Sound Architecture (ALSA) provides audio and MIDI functionality to the Linux operating system. You need to change the microphone index number, this defaults to the usb microphone and will enable you to connect. Open the LX Terminal and type:
sudo nano /etc/modprobe.d/alsa-base.conf
Translation in Action
5. A Quick Test
To quickly try out the speech to text conversion and check that is running correctly type the line below in the LX Terminal. This will prompt you to say a phrase and then convert it (you may get an warning message but it will work)
$ sudo python -m speech_recognition
6. Most Languages to English
This speech recognition code template below can be combined with the translation codes seen in Step 2 to create an 'on the fly' translation from say French to English or English to German. In the example below the speech is converted and stored as a variable called microphone_input (line 10). This 'phrase' is then used in line 15 and translated into English. Try it, say Bonjour! The translation return contains a lot of additional meta data which can be stripped down and extracted using the code lines, underneath in black.
from googletrans import translator
import speech_recognition as sr
r = sr.Recognizer()
print ("Please say your phrase")
with sr.Microphone() as source:
audio = r.listen(source)
####Convert speech to text
print("You said: " + r.recognize(audio))
microphone_input = r.recognize(audio)
print("Could not understand audio")
###Translate into English###
phrase = translator.translate(microphone_input)
###Pull out key phrase, remove metadata###
read = str(phrase) ###convert to a string
sentence = read.find('text') ###find the phrase
end_of_phrase = read.find(' pronunciation') ###find the end of the phrase
final_phrase = read[sentence+5 : end_of_phrase] ###print the final phrase
7. English to German
Now you can translate, it would be more useful to translate from English into a another language for example, German. Try adding this code to your program, it will ask you for a phrase, speak in English and it will convert the text string into German and print it out. The language is indicated by the dest='de' line. You will also need to add the following line at the start of your program, so that the accent encoding is recognised, #-*- coding: utf-8-*-
phrase2 = translator.translate(microphone_input, dest='de')
Speak into the microphone when prompted and then your program will translate the English text into German. You could combine the language detection code using the line: lan = translator.detect('phrase2') to identify the language that was spoken into the microphone and then convert the English phrase into that language. For example, you speak Swedish into the microphone, it recognises that it is Swedish, then you speak an English phrase and it converts it into Swedish. Then speak in Polish, recognise Polish and then when you speak English it converts it into Polish.
Download the Example Codes
- Raspberry Pi
- The Joker
- Hologram Machine
- Google Vision: Camera Tell
- Yoda Tweets
- Pi Phone
- Darth Beats
- Twitter Keyword Finder
- Crimbo Lights Hack
- Xmas Elf
- Halloween 2016
- Halloween Hack 2015
- Socrative Zombie
- Voice Translation
- The Blue-Who Finder
- GPIO, Twitter
- Pi Chat Bot >
- PiGlow & Email
- Pibrella Alarm System
- SMS with Python >
- Contact Me