Python Forum
listening to user input even after opening an application in mac
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
listening to user input even after opening an application in mac
#1
hi i'm writing a script to simply open and close an application on mac using python. using my voice i want to open the specified application and close it when told so. i'm using speech recognition for giving voice commands and using subprocess to open an application by this method :
subprocess.call(
    ["/usr/bin/open", "-W", "-n", "-a", "/Applications/"+name+".app"]
    )
i can open the application but after opening the application, my main program stops listening. I want it to listen my voice even after opening the application so that i can close it with the voice command.
here's the listen code:- 
audio = re.listen(source)

            try:
                value=re.recognize_google(audio)
                value=value.strip().split()
                name=" ".join(value[1:])
                if value[0] in OPEN_COGNATES:
                    speak(opCl.openCmd(name))
                    pass
                elif value[0] in CLOSE_COGNATES:
                    speak(opCl.closeCmd(name))
                    pass
                else:
                    pass
openCmd contains this code only :
subprocess.call(
        ["/usr/bin/open", "-W", "-n", "-a", "/Applications/"+name+".app"]
        )
closeCmd contains this command for closing the application :
subprocess.call(['osascript', '-e', 'tell application "'+name+'" to quit'])
can anybody help me. Also i'm quite naive in this. Thank you in advance.  Sad
Even if somebody could tell me how i can do it, that would be beneficial too.
Reply
#2
(Jan-23-2017, 05:22 PM)sharma16aug Wrote: my main program stops listening
What is re? the regex module or another module? Does this listen command create a new thread to continue listening or are you suppose to make a while loop to keep listening?
Recommended Tutorials:
Reply
#3
(Jan-23-2017, 07:15 PM)metulburr Wrote: What is re? the regex module or another module? Does this listen command create a new thread to continue listening or are you suppose to make a while loop to keep listening?

re is a recogniser object for speech. i'm currently listening it under a while loop. listening it on another thread, will it solve the problem ?
Reply
#4
Hello!
Why is that indentation after audio = re.listen(source)
Do some tests. If audio stores all the data which flows from the microphone do this mean that you try to recognize and run the open command, again and again, if you send it to google. Does google stop when returns the first word? Does that recognizing speech module run the listening in a separate thread? The audio variable has to be some kind of FIFO file object. 
Get the data from that file and throw it to google, try to catch the response and run the command as you did it already in the continuous while loop.

I can't give you an example of all that because I never used tmpfile module before. I don't know this recognizing speech module and have no time to look at it right now. I hope this is pointing to a right direction
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Jan-24-2017, 08:29 AM)wavic Wrote: Hello!
Why is that indentation after audio = re.listen(source)
Do some tests. If audio stores all the data which flows from the microphone do this mean that you try to recognize and run the open command, again and again, if you send it to google. Does google stop when returns the first word? Does that recognizing speech module run the listening in a separate thread? The audio variable has to be some kind of FIFO file object. 
Get the data from that file and throw it to google, try to catch the response and run the command as you did it already in the continuous while loop.

I can't give you an example of all that because I never used tmpfile module before. I don't know this recognizing speech module and have no time to look at it right now. I hope this is pointing to a right direction

import speech_recognition as sr
import pyttsx
import decision
import opCl

speech_engine = pyttsx.init('nsss') 
speech_engine.setProperty('rate', 150)

OPEN_COGNATES=['open']
CLOSE_COGNATES=['close']


def speak(text):
speech_engine.say(text)
speech_engine.runAndWait()
##print text

re = sr.Recognizer()

def listen():
with sr.Microphone() as source:
re.adjust_for_ambient_noise(source)
speak("minimum energy threshold is {:.2f}".format(re.energy_threshold))
speak("Say something!")
while True:
print">>",
audio = re.listen(source)

try:
##return re.recognize_sphinx(audio)
##speak("now to recognise it,")
value=re.recognize_google(audio)
print  value
speak("I heard you say {}".format(value))
value=value.strip().split()
name=" ".join(value[1:])
if value[0] in OPEN_COGNATES:
speak(opCl.openCmd(name))
pass
elif value[0] in CLOSE_COGNATES:
speak(opCl.closeCmd(name))
pass
else:
pass


except sr.UnknownValueError as e:
speak("Could not understand audio")
print e
except sr.RequestError as e:
speak("Recog Error; {0}".format(e))
print e

speak("A moment of silence please:- ")

listen()
##print listen()

i dont know how to give here indented code. but consider the code is error free, as it is. There is no error either from google speech engine or syntactically. but the thing is after opening an application, the application blocks my code. which i dont want.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 990 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,023 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,027 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 1,836 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,137 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Use pexpect to send user input alisha17 0 1,823 May-10-2022, 02:44 AM
Last Post: alisha17
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Matplotlib - close multple plots with user input Positron79 0 1,695 Dec-01-2021, 05:26 PM
Last Post: Positron79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020