about voice recognition

Official forum for open source FreePIE discussion and development.
Post Reply
simulondo
One Eyed Hopeful
Posts: 5
Joined: Fri Jul 01, 2016 12:43 pm

about voice recognition

Post by simulondo »

Hola!

Thanks to this program I've started to learn Python because I'm trying to create my own speech recognition files for my flighsims.

Until now I've used to do something like this:

Code: Select all

# ++++ CALL TANKER
if speech.said("tanker"):
   	tanker_call()
   
def tanker_call():
	keyboard.setPressed(Key.Y)
But this way requires too much work, a function for each voice command. So, searching another way I've found that the lists could be a more effective solution. First I create a list with all the voice commands and later I can use their index as variable to use it with keyboard.setPressed. For example:

Code: Select all

comms = ["option01","option02","option03","option04"]
and later:

Code: Select all

mic= keyboard.getKeyDown(Key.Home)
if starting:
	something="none"


if mic & speech.said(something): #if the user press mic button and say "something"...
        commsOption = comms.index(something)
        sayMessage(commsOption)

def sayMessage(opt):
	
	keys=[Key.D1,Key.D2,Key.D3,Key.D4,Key.D5,Key.D6,Key.D7,Key.D8,Key.D9,Key.D0]
        keyboard.setPressed(keys[opt],True)
My question is: I have not yet been able to get work the first if (if speech.said(something):) Is not possible use speech.said in this way? Maybe I'm doing something wrong (as I said before I'm learning Python since a two weeks ago).

Thank you for your time.

Regards
FrenchyKiel
One Eyed Hopeful
Posts: 47
Joined: Thu Oct 03, 2013 7:10 am

Re: about voice recognition

Post by FrenchyKiel »

hi,

this sample code maybe help you:

exec in python, execute the command written in a string

Code: Select all

if starting:
	listcmd, listkey = ["one", "two", "three"], [ "Key.A", "Key.B", "Key.C"]

for i in range(len(listcmd)):
	if speech.said(listcmd[i]):
		exec("keyboard.setPressed(" + listkey[i] + ")")
FrenchyKiel
One Eyed Hopeful
Posts: 47
Joined: Thu Oct 03, 2013 7:10 am

Re: about voice recognition

Post by FrenchyKiel »

you could write that too:

Code: Select all


if starting:
	v, k = ["1", "2", "3"], [Key.A, Key.B, Key.C]

for i in range(len(v)):
	if speech.said(v[i]):
		keyboard.setPressed(k[i])
or with def function

Code: Select all

def ky(k):
	diagnostick.debug("key!!")
	keyboard.setPressed(k)

if starting:
	v, k = ["1", "2", "3"], [Key.A, Key.B, Key.C]

for i in range(len(v)):
	if speech.said(v[i]):
		ky(k[i])
or again with lambda function

Code: Select all


if starting:
	v, k = ["1", "2", "3"], [Key.A, Key.B, Key.C]
	ky = lambda k: keyboard.setPressed(k)

for i in range(len(v)):
	if speech.said(v[i]):
		ky(k[i])
lot solutions!! read the book!!
simulondo
One Eyed Hopeful
Posts: 5
Joined: Fri Jul 01, 2016 12:43 pm

Re: about voice recognition

Post by simulondo »

Very beautiful solutions! really. As newbie programmer I never cease to be amazed at the several ways to find a solution with python.

I'm going to test and carefully study all of them.

Thank you so much.
Post Reply

Return to “FreePIE”