integrate speech recognition and tkinter - python

I'm doing a project on speech recognition and am trying to use Tkinter to create a GUI for my project...the SR part works well but when I integrate it with Tkinter it doesn't work...please help. (am new to programming so please don't mind my code:) )
#MY CODE
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = "".format(r.recognize_google(audio).get())
msg.configure(text=txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()
I would like it to display the translated text in the label but it doesn't. It only shows "say something" even after speaking.

Try this, I blocked out the msg.configure(text='Say Somethin'). This line makes the recorded text being reformatted to 'Say Something' and not the text that was recorded. Hope this helps.
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
#msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = r.recognize_google(audio)
msg.configure(text=txt)
print(txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()

Related

How to make voice detection in python faster?

I have some voice detection code and it works! but, it runs really slowly. Can I do anything to make it faster?
import speech_recognition
import pyttsx3
recognizer = speech_recognition.Recognizer()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio)
text = text.lower()
print(f" {text}")
except speech_recognition.UnknownValueError():
recognizer = speech_recognition.Recognizer()
continue
Try creating mic once instead of each iteration:
import speech_recognition
import pyttsx3
recognizer = speech_recognition.Recognizer()
with speech_recognition.Microphone() as mic:
while True:
try:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio)
text = text.lower()
print(f" {text}")
except speech_recognition.UnknownValueError():
pass

Check if text is egual to "x" speech_recognition Python

I'm working with speech_recognition python and I want to know if It's possible to check if the text that I've said is egual to "x"
Here is my code :
import speech_recognition
import speech_recognition as sr
import pyttsx3
recognizer = speech_recognition.Recognizer()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio, language="fr-FR")
text = text.lower()
print(text)
except speech_recognition.UnknownValueError():
print("I don't understand sir")
continue
I've checked
if (text == "x"):
But it didn't works

Local variable referenced before assignment(speech recognition python)

import speech_recognition as sr
r = sr.Recognizer()
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
except:
pass
return text
text = speaking()
if text == 'hello':
print("You said hello")
So yeah the code works fine. But whenever speech_recognition hears sounds like me clearing the throat/laughing/literally any ambient noise, I get an error. Can you tell me why and tell a way to correct it?
your problem.
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
except: # here its the problem
pass
return text # and here
the code should look like this
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
# or you can return text here
# and get rid of the below else block
# return text
except:
# if you get an error then do nothing
pass
# you can return None
return None
else:
# but if the try code worked then return text
return text
ofcourse you get "referenced before assignment error" because in your old code if you get an error you do nothing with it and after that you reference text, but text isnt going to be defined if you get an exception just when you are recording.

Create a Loop of SpeechRecognition

I am creating a voice assistant in python and I would like to know how I can make a loop so that every time the assistant responds, the microphone is activated so I can ask him again
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
#Here the function activates
text = get_audio()
#This is a example question
if 'hello computer' in text:
speak('Hi! How are you?')
#This is where I want to turn on the microphone again, process my voice and say the answer and so on to infinity
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
This is how you keep your microphone turned on:
while True:
# Here the function activates
text = get_audio().lower
# These are examples
if 'hello computer' in text:
speak('Hi! How are you?')
elif "good morning" in text:
speak("Hey good morning")

Unable to get instructions in audio format and not able to play audio file after first one

Jarvis should say every instruction comes on screen but here first statement is spoken & none other than that gets audio voice. And while saving the audio only the first statement gets stored as audio file. how to delete the previous audio file after process & save new audio file as instruction.How to get print statements in audio form, like there's a print statement ("Say something!") , it should be there on the screen as well as audio of same should be spoken.
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
from pygame import mixer
mixer.init()
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
os.system("mpg321 audio.mp3")
def speak_out():
mixer.music.load('audio.mp3')
mixer.music.play()
time.sleep(2)
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("I could not understand")
except sr.RequestError as e:
print("Server error. Check your internet connection: {0}".format(e))
return data
def jarvis(data):
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if 'mail' in data:
speak('who is recipient')
recipient = speak()
if 'John' in data:
speak('What should I say')
content = recordAudio()
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('abc#gmail.com','xyz')
mail.sendmail('Person name', 'mail_id', content)
mail.close()
speak('Email sent')
time.sleep(2)
speak("Hi Ajay, what can I do for you?")
speak_out()
while 1:
data = recordAudio()
jarvis(data)

Categories

Resources