How to make voice detection in python faster? - python

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

Related

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

How to use responses in neuralintents python library

I created a voice assistant using python neuralintents module. but it didn't give a way to use responses in intents.json to give a feedback for user. Instead it is mapping response to a function.
Is there any way to use responses in intents.json.
I got this by watching this tutorial :
https://youtu.be/SXsyLdKkKX0
Github repo :
https://github.com/NeuralNine/neuralintents
Source code from video
from email.mime import audio
from fileinput import filename
from neuralintents import GenericAssistant
import speech_recognition
import pyttsx3 as tts
import sys
import webbrowser
recognizer = speech_recognition.Recognizer()
speaker = tts.init()
speaker.setProperty('rate', 150)
todo_list = ['Go shopping', 'Clean Room', 'Recpard Videos']
def create_note():
global recognizer
speaker.say("What do you want to write into your note")
speaker.runAndWait()
done = False
while not done:
try:
with speech_recognition.Microphone as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
note = recognizer.recognize_google(audio)
note = note.lower()
speaker.say("Choose a filename!")
speaker.runAndWait()
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
filename = recognizer.recognize_google(audio)
filename = filename.lower()
with open(filename, 'w') as f:
f.write(note)
done = True
speaker.say(f"I sucessfully created the note {filename}")
speaker.runAndWait
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()
speaker.say("I did not understand you! Please try again!")
speaker.runAndWait()
def add_todo():
global recognizer
speaker.say("What doto do you want to add?")
speaker.runAndWait()
done = False
while not done:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
item = recognizer.recognize_google(audio)
item = item.lower()
todo_list.append(item)
done = True
speaker.say(f"I added {item} to the to do list!")
speaker.runAndWait()
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()
speaker.say("I did not understand. Please try again!")
speaker.runAndWait()
def show_todos():
speaker.say("The items in your to do list are following")
for item in todo_list:
speaker.say(item)
speaker.runAndWait()
def hello():
speaker.say("Hello. What can I do for you?")
speaker.runAndWait()
def exitt():
speaker.say("Bye")
speaker.runAndWait()
sys.exit(0)
mapppings = {
"greeting": hello,
"create_note": create_note,
"add_todos": add_todo,
"show_todos": show_todos,
"goodbye": exitt,
}
assistant = GenericAssistant('intents.json', intent_methods=mapppings)
assistant.train_model()
assistant.save_model()
def talk(text):
engine = tts.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine. setProperty("rate", 150)
engine.say(text)
engine.runAndWait()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
command = recognizer.recognize_google(audio)
command = command.lower()
assistant.request(command)
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()
if you want the assistant to speak the response you call the function talk and pass assistant.request(command) as parameter, but if you have bindings for adding todo ect. u first call the assistant.request(command) alone and after that you chec if the request has any response and if it does you pass the response to the talk function
def talk(text):
engine = tts.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine. setProperty("rate", 150)
engine.say(text)
engine.runAndWait()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
command = recognizer.recognize_google(audio)
command = command.lower()
assistant.request(command)
if(assistant.request(command))
talk(assistant.request(command))
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()

I can't run my code - python, speech_recognition

import speech_recognition
robot_ear = speech_recognition.Recognizer()
with speech_recognition.Microphone() as mic:
print("Robot: i'm Listening")
audio = robot_ear.listen(mic)
try:
you = robot_ear.recognize_google(audio)
except:
you == ""
print("You: " + you)
I run but and i say but It doesn't print anything, my name of the code is not speech_recognition.

integrate speech recognition and tkinter

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

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