Pi Zero language assistant - Bi lingual translator
Translator: In my earlier projects on the Pi Zero, I introduced several ideas for using its speech capabilities, one of which is building a bilingual translator! The gTTS library supports a variety of Indian languages, including: - English [en], Bengali [bn], Gujrati [gu], Hindi [hi], Kannada [kn], Malayalam [ml], Marathi [mr], Tamil [ta], Telugu [te], Urdu [ur]. The two-letter code in brackets is used in gTTS to specify the target language for speech conversion. For example: gTTS(text=translated_text, lang=dest_lang) It’s easy to convert speech from one language to another by changing the source and destination language codes. For instance, you can convert Gujarati speech into Spanish by simply modifying the language codes. Note: The code [en] is the same for English regardless of the region, whether it’s US, UK, Indian, or Australian English.
The other major International languages in Google-Text-To-Speech [gTTS]: In addition to the languages previously mentioned, Google Text-To-Speech (gTTS) supports a wide range of international languages, including: - Arabic - `ar`, Chinese (Simplified) - `zh`, Chinese (Traditional) - `zh-tw`, Czech - `cs`, Danish - `da`, Dutch - `nl`, English - `en`, Filipino - `fil`, Finnish - `fi`, French - `fr`, German - `de`, Greek - `el`, Hebrew - `he`, Hindi - `hi`, Hungarian - `hu`, Indonesian - `id`, Italian - `it`, Japanese - `ja`, Korean - `ko`, Norwegian - `no`, Polish - `pl`, Portuguese (Brazil) - `pt-br`, Portuguese (Portugal) - `pt`, Russian - `ru`, Spanish - `es`, Swedish - `sv`, Thai - `th`, Turkish - `tr`, Ukrainian - `uk`, Vietnamese - `vi`.
Out of these, I have tested only English [en], Gujarati [gu], Bengali [bn], and Hindi [hi] for Indian languages, and Russian [ru], Spanish [es], and German [de] for international languages. However, aside from English, Bengali, Gujarati, and Hindi, I cannot confidently vouch for the accuracy of the output in the other languages which only the International readers may certify!
Schematic:Those familiar with my previous series of Pi Zero speech function projects will find this schematic quite similar, with a few exceptions. Two push-to-on switches are connected to GPIO pins 17 and 22 to facilitate “English to Hindi” (en-hi) and “Hindi to English” (hi-en) conversion, respectively. A 2.8” LCD panel (320x240 pixels) is connected via SPI to display the sequence of operations happening on the Pi Zero. For smaller TFT displays (e.g., 1.44”, 128x128 pixels), simply adjust the pixel size in the code, and it will work seamlessly. These two button can be set for any other languages as well! [See the code]
Project command: Two push buttons handle all the operations. Press the “en-hi” button to prepare the device for English voice input. After speaking a short English sentence, press the button again. The device will process your speech and convert it into Hindi, producing a clear and fluent Hindi output! Each step of the process is displayed on the screen: recording the speech, converting the recorded voice to text, and then translating the text into speech using Google Translate and Google Text-to-Speech.
To switch to “Hindi to English,” press the “hi-en” button, wait for the message “Recording started (Hindi to English)” to appear, and proceed with your input.
This process is quite resource-intensive for the Pi Zero board, so the Pi Zero is slightly overclocked to 1.3 GHz with the help of a quality metallic heatsink on the processor (refer to my previous Pi Zero speech function projects for overclocking details). This provides a slight advantage for handling the heavy workload. Also, ensure you have a stable internet connection; otherwise, the process may take longer.
Sound output on Pi Zero: There is no sound output on Pi-Zero board. Any two of GPIO 12,13,18,19 pins can be used to force audio output by adding just one line in /boot/config.txt file. Open the file and go to the bottom and add this line followed by a reboot. Next time you will be able to hear audio on those two GPIO pins. Just add a headphone on the IN-L & IN-R to get sound on the headphone. So simple! The PAM8403 amplifies the sound further for a speaker.
dtoverlay=audremap,pins_18_13
Overclocking of Pi zero: In the same /boot/config.txt file , find the overclocking portion, comment the existing one and add the following two lines. Save and reboot, the Pi will operate at 1.3 GHz now. Add a good metallic heatsink on the CPU and Monitor the CPU temperature [ vcgencmd measure_temp ]
#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800
#Bera wrote these two lines
arm_freq=1300
over_voltage=2
Softwares for this project: It’s all open source softwares therefore, if you go step by step as mentioned here, you will be able to install all these softwares very comfortably. It is assumed that in your Raspberry Pi zero you have python 3.7 or above & pip is already installed. If not please do that before everything else.
X. Install python & pip
$> sudo apt install python3 python3-pip
- Install gTTS (Google Text-to-Speech)
$> pip install gtts
2. Install SpeechRecognition
$> pip install SpeechRecognition
3. Install Adafruit-GPIO and Adafruit-ILI9341
$> pip install Adafruit-GPIO
$> pip install Adafruit-ILI9341
4. Install Pillow (for Image, ImageDraw, ImageFont):
$> pip install pillow
5. For USB microphone use:
$> pip install pyaudio
6. Install googletrans (Translator):
$> pip install googletrans==4.0.0-rc1
7. Install RPi.GPIO: [In all likelihood this is already installed in your Pi zero, if not]
$> sudo apt-get install python3-rpi.gpio
Voice-in to Pi-zero: So far voice out is perfect. To have voice-in, I’ve settled with a cheap USB microphone - from
robu.in for lNR 169 [$2] and the beautiful thing is that it just works out-of-the box. Once inserted into the only USB port of Pi-zero, do an ‘lsusb’ to find out the hardware device number. You may need to have an OTG cable to interface between micro USB [Pi-zero side] to USB [mic]. Now to see that the mic is fully functional ,try this one on a terminal -
$> arecord -f S16_LE -r 33100 -d 10 -c 1 output.wav
- -f S16_LE: Specifies the audio format. S16_LE stands for signed 16-bit little-endian.
- -r 33100: Sets the sample rate to 33100 Hz.
- -d 10: Specifies the duration of the recording in seconds (e.g., 10 seconds in this example).
- -c 1: Sets the number of audio channels to 1 (mono). Adjust as needed for stereo recordings (-c 2 for stereo).
- output.wav: Specifies the output file name (output.wav in this example).
After running this command, arecord will record audio at a sample rate of 33100 Hz for 10 seconds and save it to the output.wav file in the current directory. Adjust the options (-d for duration, output file name, etc.) according to your requirements.
$> lsusb output …
Bus 001 Device 002: ID 4c4a:4155 Jieli Technology UACDemoV1.0
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
To play the audio file $> aplay output.wav , will play your invaluable 10 seconds acoustic creation. Adjust the mike suitably to get clear audio recordings. Keep the speakers away from the mic to avoid feedback problem.
Internet connection: Ensure WiFi internet connection or any other is working in your Pi Zero. Do a $> sudo raspy-config and ; Navigate to “Network Options” -> Enter your SSID & Password. Once you exit the tool followed by a reboot. Next time you will become connected to your WiFi network. Alternatively you can
$> sudo nano /etc/wpa_supplicant/wpa_supplicant.conf ; in the file do the following -
country=IN # (Replace 'IN' with your country code IN = India)
network={
ssid="your_network_name"
psk="your_password"
}
Python program:
Software: attached
- lang_astt5.py is for 240x320 TFT display
To start the system at boot:
To start the program on every boot we have include it in the .profile file of the user login. To suppress the program form displaying any non-critical error messages we have to move the errors to /dev/null file. Also ensure that the Pi zero board logs in into your default home. Then in the default home there is a .profile file. Open that file and then at the bottom add the following line. Save the file and reboot. Alternatively error-handling can be inserted into the python script but that will sure increase the process time for the little pi-zero board!
$> nano .profile #add the following line at the end. Save and reboot.
python lang_astt5.py 2>/dev/null &
Run of program: After boot up, the program will auto start and would show on the TFT screen waiting for a button press for the sequence to start - “Recording started (English to Hindi)”. Enjoy playing with your device!
Downsize the form factor: To reduce the form factor / to make it a pocket sized device here are few ways - (1) Remove / Replace the display by an I2C display (2) Replace the display altogether by a few LEDs (3) Remove the camera as it is not required (4) Instead of using an amplifier an earphone can be used at the Amplifier inlet.
This project Video link:
S. Bera / Kolkata
Pictures:
Diskussion (0 Kommentare)