12 Replies - 736 Views - Last Post: 28 August 2013 - 06:13 PM Rate Topic: -----

#1 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

More speech problems

Posted 26 August 2013 - 06:34 PM

So after I understood Pymouse i decided to try to get it to understand how many pixels i wanted to move to by voice but i can't seem to get x to be understood by the code m.move
(pos[0], pos[1] - x)
with x being the speech.input. even thou i say the number. it gets in a string and i have tried to convert it to an integer but it still doesn't like it. Here's the rest of my code
import time
import speech
import os
from pymouse import PyMouse

def Def(phrase, listener):
        m = PyMouse()


def Move(phrase, listener):
    if phrase == "Up":

        speech.say("How far do you want to go up")
        print "getting x"
        x = speech.listenforanything(Def)
        print "printing x"
        print x

        if (x >0 & x<100000):
           print " getting mouse coordinents"
           m = PyMouse()
           m.position()
           pos = m.position()
           print pos[0]
           print pos[1]
           m.move(pos[0], pos[1] - x)
        else:
           print "x is wrong"
           print x


def Get_speech(phrase, listener):
    if phrase == "Move Mouse":
        speech.say("Where do you want to move")
        L2 = speech.listenfor(["Up"], Move)
        

L1 = speech.listenfor(["Move Mouse"], Get_speech)


Is This A Good Question/Topic? 0
  • +

Replies To: More speech problems

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: More speech problems

Posted 27 August 2013 - 07:48 AM

In the code you've provided, you're not actually converting the string to an integer. Python does not do implicit conversions, so you're going to have to do it manually.

try:
    x = int(x)
except ValueError:
    // Handle a bad input here.


What error message(s) are you getting back?

EDIT: AFter looking over the API for pyspeech, I don't think you're using speech.listenforanything() correctly. You're not doing anything with the phrase that's being said. You're just instantiating the PyMouse object. x would actually be the listener, not the dictated speech. You want x to be set to the phrase argument from Def (your callback function), and then converted to an integer.

More information can be found here.

This post has been edited by Valek: 27 August 2013 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: More speech problems

Posted 27 August 2013 - 08:19 AM

I have to initiate something in to the
def Def(phrase, listener):
or else i'd get an error. I want to be able to say a number and have the mouse move how many pixels based on the number i said. The problem its giving me is that it can't have the listener in the part
m.move([pos[0], pos[1] - x
with x being the listener! i tried doing it with x being raw_input() but even that gives me errors
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: More speech problems

Posted 27 August 2013 - 08:25 AM

View PostDestroyer5, on 27 August 2013 - 11:19 AM, said:

I have to initiate something in to the
def Def(phrase, listener):
or else i'd get an error. I want to be able to say a number and have the mouse move how many pixels based on the number i said. The problem its giving me is that it can't have the listener in the part
m.move([pos[0], pos[1] - x
with x being the listener! i tried doing it with x being raw_input() but even that gives me errors


Regarding having something in the body of a function: You can use pass if you need an empty function. In this particular case though, you should consider a variation of the code I posted above. Perhaps something like this:

def Def(phrase, listener):
    m = PyMouse()
    try:
        x = int(phrase)
    except ValueError:
        // Handle bad input here.


The only problem I can foresee is this: when you say a number (let's go with 17 for this example), does pyspeech interpret that as 17 or as "seventeen"? Either way it's going to be a string, but knowing how pyspeech handles that case is going to determine how you would handle its output before passing it over to PyMouse.
Was This Post Helpful? 0
  • +
  • -

#5 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: More speech problems

Posted 27 August 2013 - 11:24 AM

I just tried it and it referred Seventeen as 17 but explaining both ways would be helpful if it did come to one or the other.
Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: More speech problems

Posted 27 August 2013 - 12:27 PM

When it comes back as 17, it's quite simple:

try:
    x = int(phrase)
except ValueError:
    // Handle bad input, just in case.


When it comes out as "seventeen", that would be a bit trickier. You would need to write a function that could recognize the textual representation of those numbers and convert them to their integer equivalents. That would be a fair amount more code. It appears someone has already done the legwork on this, though.

This post has been edited by Valek: 27 August 2013 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: More speech problems

Posted 27 August 2013 - 01:56 PM

I did what you suggested and have come upon another problem. The program runs until i say up and then it tells me i have a global error with x.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\win32com\server\policy.py", line 277, in _Invoke_
return self._invoke_(dispid, lcid, wFlags, args)
File "C:\Python27\lib\site-packages\win32com\server\policy.py", line 282, in _invoke_
return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
File "C:\Python27\lib\site-packages\win32com\server\policy.py", line 585, in _invokeex_
return func(*args)
File "build\bdist.win32\egg\speech.py", line 137, in OnRecognition
self._callback(phrase, self._listener)
File "C:\Python27\Scripts\Mousespcchtesting.py", line 21, in Move
m.move(pos[0], pos[1] - x)
NameError: global name 'x' is not defined

But when i make x global no speech is even recognized within my program.
Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: More speech problems

Posted 27 August 2013 - 02:33 PM

Sounds like you just defined x outside the scope of your m.move() call. Have you considered making what you're working on a class? You could have the variable holding the amount it needs to move as an attribute of the class and access it across multiple methods that way.
Was This Post Helpful? 0
  • +
  • -

#9 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: More speech problems

Posted 27 August 2013 - 02:59 PM

When i create a class and put it all under it then the part of
Def def(phrase, listener)
can't be seen
Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: More speech problems

Posted 27 August 2013 - 03:07 PM

It would be easier to help if I could see the code you've got when you encounter these problems, as if I can't see it, I can't tell exactly what changes you've made. That makes it harder to point out why you're not getting the results you're looking for.
Was This Post Helpful? 1
  • +
  • -

#11 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: More speech problems

Posted 27 August 2013 - 03:14 PM

This is after i was messing around with this.
import time
import speech
import os
from pymouse import PyMouse
global x

def Move(phrase, listener):
        if phrase == "Up":
                speech.say("How far do you want to go up")
                m = PyMouse()
                try:
                        x = int(phrase)
                        y = speech.listenforanything(Def)
                        m = PyMouse()
                        m.position()
                        pos = m.position()
                        m.move(pos[0], pos[1] - x)
                except ValueError:
                        "//Handle bad input here"

class public:
        
        def Def(phrase, listener):
                m = PyMouse()
                try:
                        x = int(phrase)
                except ValueError:
                        "//Handle bad input here"



        def Get_speech(phrase, listener):
            if phrase == "Move Mouse":
                speech.say("Where do you want to move")
                L2 = speech.listenfor(["Up"], Move)
        

        L1 = speech.listenfor(["Move Mouse"], Get_speech)



Was This Post Helpful? 0
  • +
  • -

#12 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: More speech problems

Posted 28 August 2013 - 06:03 AM

Okay, two things.

1) Remove the global x line. Globals are ill-advised in almost every case as they tend to violate encapsulation by superseding scope.
2) I can tell you are not terribly familiar with Python classes. I don't know what version of Python you're currently using, so I'm going to take a shot in the dark and give you this link to get you more familiar with how classes work in Python.
Was This Post Helpful? 0
  • +
  • -

#13 Destroyer5  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-August 13

Re: More speech problems

Posted 28 August 2013 - 06:13 PM

thanks for all the help i was able to get this to work
import time
import speech
import os
from pymouse import PyMouse
    
def an(phrase, listener):
    print "Left or Right"
    y = int(phrase)
    m = PyMouse()
    m.position()
    pos = m.position()
    m.move(pos[0] - y, pos[1])


L1 = speech.listenforanything(an)
        
I still have to get a class in there but i'll get it eventually. Thanks again for everything
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1