|
| 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