Running a functions for a specific time - python

I have a function that listens to my command and converts that to text, I want to create another function that runs the first functions for a given time and if I did not speak for that time, it closes the functions and moves on
Suppose the first function is named takeCommand and the second one as ListenandWait
import speech_recognition as sr
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening...')
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Recognizing...')
query = r.recognize_google(audio, language='en-in')
print('User Said: {}\n'.format(query))
except:
print('Say that again please...')
return 'None'
return query

Related

How to add timer in speech input in python?

I want to add the timer for the 10sec audio input
import speech_recognition as
r= Recognizer()
with Microphone() as source:
while(i==0):
print('Say Something')
audio = r.listen(source)
query = r.recognize_google(audio)
print(query)

Python Speech_Recognition - "This audio source is already inside a context manager"

I am trying to run the speech recognition in the background on Linux machine, but it gives me an AssertionError saying "This audio source is already inside a context manager". Everything is working fine, when using it without threading.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import speech_recognition as sr
def speech_to_text(recognizer, audio):
try:
print(recognizer.recognize_google(audio, language="de"))
except sr.UnknownValueError:
print("[!] UnknownValueError")
except sr.RequestError as e:
print("RequestError: ", e)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.adjust_for_ambient_noise(source)
stop_listening = r.listen_in_background(source, speech_to_text)
#stop_listening()
#audio = r.listen(source)
#print(r.recognize_google(audio))
if __name__ == "__main__":
get_audio()
Thanks in advance!
The issue is with your instance of microphone before your context manager 'with'.
Try this
def get_audio():
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
print("Listening...")
r.adjust_for_ambient_noise(source)
try:
stop_listening = r.listen_in_background(source, speech_to_text)
except:
print('please say that again')
return get_audio()

Python speech recognition

When i run this code and speak script return empty list:
import pyaudio
import pyttsx3
import os
import pyautogui
import speech_recognition as sr
def command():
r = sr.Recognizer()
mic = sr.Microphone()
recog = sr.Recognizer()
with mic as u_audio:
print('Speak please')
r.adjust_for_ambient_noise(u_audio)
voice = r.listen(u_audio)
try:
listening = recog.recognize_google(voice, language = 'en-EN', show_all = True)
print(listening)
except Exception as e:
print('I not understand' + str(e))
command()
command()
output:
speak please
[ ]
i do not understand, why list are empty. Maybe i should choose microphone index.
The Microphone is the Problem
The problem should be possibly be on the Microphone, because I ran the same code, and it does give the Output, it gives all possible outcomes of the speech.
Anyways, it isn't efficient. Unwanted libraries slows it down, here is the updated code:
import pyttsx3
import speech_recognition as sr
def command():
r = sr.Recognizer()
mic = sr.Microphone()
recog = sr.Recognizer()
with mic as u_audio:
r.adjust_for_ambient_noise(u_audio)
print('Speak please')
voice = r.listen(u_audio)
try:
listening = recog.recognize_google(voice, language = 'en-EN', show_all = True)
print(listening)
except Exception as e:
print('I not understand' + str(e))
command()
command()
Comment down the result!

Tkinter's Mainloop function dosen't allow other funtions to run

I am actually the member of NLI(Novitatis Locus Industries) and our team seems to get some issues. While designing a voice enabled program with GUI, tkinter's mainloop seems to be giving us some issues. See our code-
//this function defines the gui//
def gui():
gui_kurooto = Tk()
txt_dsply_var = StringVar()
AUTHN_LBL = Label(gui_kurooto, text=txt_dsply_var)
AUTHN_LBL.pack()
gui_kurooto.mainloop()
//this code defines the microphone input//
def takeCommand():
#It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
txt_dsply_var = StringVar()
txt_dsply_var.set('Listening')//display listening on gui//
r.pause_threshold = 1
r.energy_threshold = 10000
audio = r.listen(source)
try:
print("Recognizing...")
txt_dsply_var = StringVar()
txt_dsply_var.set('Recognizing')//display recognizing//
query = r.recognize_google(audio, language='en-en')
print(f"User said: {query}\n")
txt_dsply_var = StringVar()
txt_dsply_var.set(f"User said: {query}\n")//display input//
except Exception as e:
# print(e)
print("Say that again please...")
return "None"
return query
//to run//
if __name__ == "__main__":
gui()
while True:
# if 1:
query = takeCommand().lower()
Now when I try to run this, the code never reaches the while loop. I know the mainloop is an infinite loop but is there a substitute present their so I can let it reach other functions. Keeping the function out of function also dosen't work. Can I place it after the query command?
Also I have other question, is there a way by which I can spectra according to the voice input like Google and Siri have bars which move when we speak?
Any help would be appreciated.

My Microphone goes back to listening after i go to another function

Whenever i run this code, and tell start the function "google", it goes back to another function. i have tried to do this for a few days now, and still no luck. any help would be appreciated:)
import webbrowser
import string
import time
import pyttsx3
import speech_recognition as sr
engine = pyttsx3.init()
r = sr.Recognizer()
def Listen():
with sr.Microphone() as sourceL:
print("Listening...")
Open = r.listen(sourceL, phrase_time_limit=2)
try:
if "Nova" in r.recognize_google(Open):
print("Nova Recieved...")
Command()
else:
Listen()
except:
Listen()
def Google():
print("what would you like me to search for you? ")
engine.say("what would you like me to search for you? ")
engine.runAndWait()
with sr.Microphone as source:
Search = r.listen(source)
Search = r.recognize(Search)
The code will go back to Listen() at with sr.Mirophone as source
This is how I am calling google()...
def Command():
print("You called me?")
engine.say("you called me? ")
engine.runAndWait()
Cr = sr.Recognizer()
with sr.Microphone() as source:
print("Listening For Command...")
CommandToDo = Cr.listen(source, phrase_time_limit=2)
print("...")
if "YouTube" in Cr.recognize_google(CommandToDo):
YouTube()
elif "Google" in Cr.recognize_google(CommandToDo):
Google()
else:
print("Command not recognized>> " + r.recognize_google(CommandToDo))
There is a function argument phrase_time_limit you need to specify while calling the listen method inside Google function.
phrase_time_limit represents the wait time of the program, for how many seconds it will wait for the user to give input. Here it will wait for 2 seconds. If you do not give any time limit it will wait indefinitely.
From the source code documentation:
The phrase_time_limit parameter is the maximum number of seconds
that this will allow a phrase to continue before stopping and
returning the part of the phrase processed before the time limit was
reached. The resulting audio will be the phrase cut off at the time
limit. If phrase_timeout is None, there will be no phrase time
limit.
To clarify on the timeout argument
The timeout parameter is the maximum number of seconds that this
will wait for a phrase to start before giving up and throwing an
speech_recognition.WaitTimeoutError exception. If timeout is
None, there will be no wait timeout.
For more details check the source code.
def Google():
print("what would you like me to search for you? ")
engine.say("what would you like me to search for you? ")
engine.runAndWait()
with sr.Microphone() as source:
Search = r.listen(source, phrase_time_limit=2) # <-- Here
Search = r.recognize_google(Search)
print(Search)
After this change, it is working for me.
Check it is
with sr.Microphone() as source: not
with sr.Microphone as source:. You missed the braces.

Categories