TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • 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 >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

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')
   print lan

   lan = translator.detect('Que fais-tu dans la vie?')
   print lan


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?')
  print phrase


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')
  print phrase2



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
Near the end of the text, look for the lines:  
options snd-usb-audio index=-2
change it to:  options snd-usb-audio index=0  
Save and exit the file, attach your microphone and now you are ready to record and convert your speech or phrase to text.
Picture

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 pyaudio                                
 import speech_recognition as sr
 import unicodedata

 r = sr.Recognizer()
 print ("Please say your phrase")
 with sr.Microphone() as source:                
     audio = r.listen(source)                 

 ####Convert speech to text
 try:
     print("You said: " + r.recognize(audio))    
     microphone_input  = r.recognize(audio)     
 except LookupError:                           
     print("Could not understand audio")
    
 ###Translate into English###
 phrase = translator.translate(microphone_input)
 print phrase 


###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
print 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')
 print phrase2


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


Picture
Copyright 2020 TeCoEd @dan_aldred