-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_speech_gui.py
More file actions
71 lines (60 loc) · 2.32 KB
/
Copy pathai_speech_gui.py
File metadata and controls
71 lines (60 loc) · 2.32 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import tkinter as tk
import speech_recognition as sr
from gtts import gTTS
from io import BytesIO
from pydub import AudioSegment
from pydub.playback import play
import threading
# Function to recognize speech from the microphone
def recognize_speech():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
status_label.config(text="Listening...")
try:
# Listen and recognize speech
audio_data = recognizer.listen(source, timeout=5)
text = recognizer.recognize_google(audio_data)
output_text.set(text)
status_label.config(text="Recognition Complete")
except sr.UnknownValueError:
output_text.set("Sorry, I could not understand the speech.")
except sr.RequestError:
output_text.set("Error with the speech recognition service.")
except sr.WaitTimeoutError:
output_text.set("Listening timed out.")
# Function to convert text to speech using gTTS
def text_to_speech():
text = output_text.get()
if text:
tts = gTTS(text, lang='en')
audio = BytesIO()
tts.write_to_fp(audio)
audio.seek(0)
sound = AudioSegment.from_file(audio, format="mp3")
play(sound)
# Threaded execution for speech recognition
def threaded_recognition():
threading.Thread(target=recognize_speech).start()
# Creating the GUI
root = tk.Tk()
root.title("AI Speech Recognition")
# Output Text Variable
output_text = tk.StringVar()
# Title Label
title_label = tk.Label(root, text="AI Speech Recognition & Speech-to-Text", font=("Helvetica", 16))
title_label.pack(pady=10)
# Status Label
status_label = tk.Label(root, text="Click 'Start' to begin speech recognition", font=("Helvetica", 12))
status_label.pack(pady=10)
# Start Button
start_button = tk.Button(root, text="Start", command=threaded_recognition, font=("Helvetica", 14), bg="lightblue")
start_button.pack(pady=10)
# Display the recognized text
recognized_text_label = tk.Label(root, textvariable=output_text, font=("Helvetica", 14), wraplength=400)
recognized_text_label.pack(pady=10)
# Speak Button to convert the text to speech
speak_button = tk.Button(root, text="Speak", command=text_to_speech, font=("Helvetica", 14), bg="lightgreen")
speak_button.pack(pady=10)
# Start the Tkinter main loop
root.geometry("500x300")
root.mainloop()