Skip to content

Commit e2a368c

Browse files
authored
Merge pull request #156 from Tejaswi-Kumar/translator
Detect and Translate languages with the help of speech recognition using a python script
2 parents a1beb6c + 0e06f0c commit e2a368c

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Detect and Translate languages with the help of speech recognition
2+
3+
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.
4+
5+
## Prerequisite
6+
7+
- Any system with microphone and speaker.
8+
- System with python installed in it. (or any IDE like Spyder, Jupyter, VScode etc)
9+
10+
## Dependencies
11+
12+
Install the following dependencies using pip
13+
14+
```
15+
$ pip install speech_recognition
16+
```
17+
18+
```
19+
$ pip install langdetect
20+
```
21+
22+
```
23+
$ pip install pyttsx3
24+
```
25+
26+
```
27+
$ pip install google_trans_new
28+
```
29+
30+
#### Running the script
31+
32+
Simply run the script using python in any IDE
33+
34+
```
35+
$ python ./detect_translate.py
36+
```
37+
38+
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"
39+
40+
You can also refer the git issue for more reference on this topic: https://github.com/lushan88a/google_trans_new/issues/36
41+
42+
Author: Tejaswi Kumar
43+
44+
LinkedIn: https://www.linkedin.com/in/tejaswi24/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author: Tejaswi
4+
5+
"""
6+
7+
# Python program to detect and translate with the help of speech recognition
8+
9+
import speech_recognition as sr
10+
from langdetect import detect
11+
from google_trans_new import google_translator
12+
import pyttsx3
13+
14+
'''
15+
Supported Languages:
16+
{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian',
17+
'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)',
18+
'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english',
19+
'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian',
20+
'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'hi': 'hindi',
21+
'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese',
22+
'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao',
23+
'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam',
24+
'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'ps': 'pashto',
25+
'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic',
26+
'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish',
27+
'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian',
28+
'ur': 'urdu', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu', 'fil': 'Filipino',
29+
'he': 'Hebrew'}
30+
31+
'''
32+
33+
r = sr.Recognizer()
34+
translator = google_translator()
35+
36+
37+
def SpeakText(command):
38+
# Initialize the engine
39+
engine = pyttsx3.init()
40+
engine.say(command)
41+
engine.runAndWait()
42+
43+
44+
def trans(x, d):
45+
s = detect(x)
46+
result = translator.translate(x, lang_src=s, lang_tgt=d)
47+
return result
48+
49+
50+
print("Start speaking.....(To terminate the program say 'Stop!')")
51+
while(1):
52+
try:
53+
with sr.Microphone() as source2:
54+
r.adjust_for_ambient_noise(source2, duration=0.2)
55+
audio2 = r.listen(source2)
56+
MyText = r.recognize_google(audio2)
57+
MyText.lower()
58+
if MyText == 'stop':
59+
break
60+
print("Did you say "+MyText)
61+
d = input(
62+
'Enter the language you need the text to be translated into:')
63+
translated = trans(MyText, d)
64+
print(translated)
65+
SpeakText(MyText)
66+
67+
except sr.RequestError as e:
68+
print("Could not request results; {0}".format(e))
69+
except sr.UnknownValueError:
70+
print("unknown error occured")

0 commit comments

Comments
 (0)