Added python as well as readme file

This commit is contained in:
Tejaswi-Kumar 2022-10-09 09:54:27 +05:30
parent a0878b5d01
commit 281ed4e13e
2 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,44 @@
# Detect and Translate languages with the help of speech recognition
This python script first records the user's voice and then convert it to text. After that it detects the language of the text and then translates the language to the user's desired language, which is asked to the user. After converting the language to the desired language, the translated text would be displayed and the system would read the text for the user.
## Prerequisite
- Any system with microphone and speaker.
- System with python installed in it. (or any IDE like Spyder, Jupyter, VScode etc)
## Dependencies
Install the following dependencies using pip
```
$ pip install speech_recognition
```
```
$ pip install langdetect
```
```
$ pip install pyttsx3
```
```
$ pip install google_trans_new
```
#### Running the script
Simply run the script using python in any IDE
```
$ python ./detect_translate.py
```
Note: google_trans_new may cause some error like "JSONDecodeError: Extra data", to fix it go to the location where all the python packages are installed and change line 151 in google_trans_new/google_trans_new.py which is: "response = (decoded_line + ']')" to "response = decoded_line"
You can also refer the git issue for more reference on this topic: https://github.com/lushan88a/google_trans_new/issues/36
Author: Tejaswi Kumar
LinkedIn: https://www.linkedin.com/in/tejaswi24/

View File

@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
"""
@author: Tejaswi
"""
# Python program to detect and translate with the help of speech recognition
import speech_recognition as sr
from langdetect import detect
from google_trans_new import google_translator
import pyttsx3
'''
Supported Languages:
{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic',
'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian',
'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan',
'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)',
'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian',
'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english',
'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish',
'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian',
'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole',
'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'hi': 'hindi',
'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo',
'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese',
'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer',
'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao',
'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish',
'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam',
'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian',
'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'ps': 'pashto',
'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi',
'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic',
'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi',
'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali',
'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish',
'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish',
'uk': 'ukrainian', 'ur': 'urdu', 'uz': 'uzbek', 'vi': 'vietnamese',
'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba',
'zu': 'zulu', 'fil': 'Filipino', 'he': 'Hebrew'}
'''
r = sr.Recognizer()
translator = google_translator()
def SpeakText(command):
# Initialize the engine
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()
def trans(x, d):
s = detect(x)
result = translator.translate(x, lang_src=s, lang_tgt=d)
return result
print("Start speaking.....(To terminate the program say 'Stop!')")
while(1):
try:
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration=0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText.lower()
if MyText == 'stop':
break
print("Did you say "+MyText)
d = input(
'Enter the language you need the text to be translated into:')
translated = trans(MyText, d)
print(translated)
SpeakText(MyText)
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occured")