-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTranslator.py
55 lines (49 loc) · 1.49 KB
/
Translator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from fnmatch import translate
from time import sleep
from googletrans import Translator
import googletrans #pip install googletrans
from gtts import gTTS
import googletrans
import pyttsx3
import speech_recognition
import os
from playsound import playsound
import time
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
rate = engine.setProperty("rate",170)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def takeCommand():
r = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
print("Listening.....")
r.pause_threshold = 1
r.energy_threshold = 300
audio = r.listen(source,0,4)
try:
print("Understanding..")
query = r.recognize_google(audio,language='en-in')
print(f"You Said: {query}\n")
except Exception as e:
print("Say that again")
return "None"
return query
def translategl(query):
speak("SURE SIR")
print(googletrans.LANGUAGES)
translator = Translator()
speak("Choose the language in which you want to translate")
b = input("To_Lang :- ")
text_to_translate = translator.translate(query,src = "auto",dest= b,)
text = text_to_translate.text
try :
speakgl = gTTS(text=text, lang=b, slow= False)
speakgl.save("voice.mp3")
playsound("voice.mp3")
time.sleep(5)
os.remove("voice.mp3")
except:
print("Unable to translate")