-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Samir Paul
authored and
Samir Paul
committed
Sep 19, 2023
1 parent
cab73e0
commit cb0117f
Showing
5 changed files
with
118 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import tkinter as tk | ||
import threading | ||
import speech_recognition as sr | ||
from deep_translator import GoogleTranslator | ||
from gtts import gTTS | ||
from playsound import playsound | ||
import os | ||
|
||
keep_running = False | ||
|
||
def update_translation(): | ||
global keep_running | ||
|
||
if keep_running: | ||
r = sr.Recognizer() | ||
translator = GoogleTranslator() | ||
|
||
with sr.Microphone() as source: | ||
log_output.insert(tk.END, "Speak Now!\n") | ||
audio = r.listen(source) | ||
|
||
try: | ||
speech_text = r.recognize_google(audio) | ||
print(speech_text) | ||
if speech_text.lower() in {'exit', 'stop'}: | ||
keep_running = False | ||
return | ||
|
||
translated_text = GoogleTranslator(source='auto', target='hi').translate(text=speech_text) | ||
print(translated_text) | ||
|
||
voice = gTTS(translated_text, lang='hi') | ||
voice.save('voice.mp3') | ||
playsound('voice.mp3') | ||
os.remove('voice.mp3') | ||
|
||
log_output.insert(tk.END, translated_text + "\n") | ||
|
||
except sr.UnknownValueError: | ||
log_output.insert(tk.END, "Could not understand!\n") | ||
except sr.RequestError: | ||
log_output.insert(tk.END, "Could not request from Google!\n") | ||
|
||
root.after(100, update_translation) | ||
|
||
def run_translator(): | ||
global keep_running | ||
|
||
if not keep_running: | ||
keep_running = True | ||
update_translation_thread = threading.Thread(target=update_translation) | ||
update_translation_thread.start() | ||
|
||
def kill_execution(): | ||
global keep_running | ||
keep_running = False | ||
|
||
# Create the main window | ||
root = tk.Tk() | ||
root.title("Voice Translator GUI") | ||
|
||
# Create the frame for buttons | ||
button_frame = tk.Frame(root) | ||
button_frame.pack(pady=10) | ||
|
||
# Create the "Run" button | ||
run_button = tk.Button(button_frame, text="Start Translation", command=run_translator) | ||
run_button.pack(side=tk.LEFT, padx=10) | ||
|
||
# Create the "Kill" button | ||
kill_button = tk.Button(button_frame, text="Kill Execution", command=kill_execution) | ||
kill_button.pack(side=tk.LEFT, padx=10) | ||
|
||
# Create the log output window | ||
log_output = tk.Text(root, height=20, width=50) | ||
log_output.pack(pady=10) | ||
|
||
# Run the Tkinter event loop | ||
root.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
playsound | ||
googletrans | ||
SpeechRecognition | ||
PyAudio | ||
gTTs | ||
gTTS-token | ||
pyaudio | ||
google-trans-new | ||
gTTS | ||
playsound | ||
deep-translator | ||
subprocess.run | ||
pyautogui |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import speech_recognition as sr | ||
# from google_trans_new import google_translator | ||
from deep_translator import GoogleTranslator | ||
from gtts import gTTS | ||
from playsound import playsound | ||
import os | ||
|
||
r = sr.Recognizer() | ||
translator = GoogleTranslator() | ||
|
||
while True: | ||
with sr.Microphone() as source: | ||
print("Speak Now!") | ||
audio = r.record(source, duration=5) | ||
try: | ||
speech_text = r.recognize_google(audio) | ||
print(speech_text) | ||
if speech_text.lower() in {'exit', 'stop'}: | ||
break | ||
except sr.UnknownValueError: | ||
print("Could not understand!") | ||
except sr.RequestError: | ||
print("Could not request from google") | ||
|
||
translated_test = GoogleTranslator(source='auto', target='hi').translate(text=speech_text) | ||
print(translated_test) | ||
|
||
voice = gTTS(translated_test, lang='hi') | ||
voice.save('voice.mp3') | ||
playsound('voice.mp3') | ||
os.remove('voice.mp3') | ||
|