[Beginner] Can someone help with voice recognition please?

Official forum for open source FreePIE discussion and development.
Post Reply
SnooK
One Eyed Hopeful
Posts: 1
Joined: Sun Oct 25, 2015 2:18 pm

[Beginner] Can someone help with voice recognition please?

Post by SnooK »

Hi guys. First of all let me get the apologies out of the way. I am not familiar with any coding language, inc Python. I do however have a modicum of common sense and am able to butcher pre-written code so that it meets my needs. I have done this with Glovepie and have a working script which I am happy with, apart from a lack of certain specific functionality which I believe Freepie offers. However, I seem to have fallen at the first hurdle as I am not even able to replicate my Glovepie script in Freepie. I was hoping someone may be able to help me get started...

My Glovepie script is just voice recognition triggering individual key presses or macros. Nothing fancy. For starters I'd like to get Freepie doing what Glovepie currently does. I have used NoxWings VoiceToKeyboard sample script (see end of post). I'm assuming that if I said"Hello" it should respond "Welcome Initialising system". It does nothing. I have tested the audio on Freepie using:

import time
if starting:
speech.say("One!")
time.sleep(1);
speech.say("Two!")

That works fine but I cant get voice recognition to work. I guess I am missing something obvious but I'd be very grateful if someone could help a newbie get started. What I'd like to know is, why I cant get the script below to work or what script I could use so that e.g. when I said "One" it presses "1" on the keyboard.

Thanks in advance.

NOXWING'S SCRIPT

import time;

# ***********************************
# Key Actions Classes
# ***********************************

class KeyAction:
def __init__(self, key):
self.keys = []
# append or extend (both are valid)
if isinstance(key, list):
self.keys.extend(key)
else:
self.keys.append(key)

def setKeyDown(self):
for key in self.keys:
keyboard.setKeyDown(key)

def setKeyUp(self):
for key in self.keys:
keyboard.setKeyUp(key)

def setKey(self, down):
for key in self.keys:
keyboard.setKey(key, down)

def setKeyPressed(self):
for key in self.keys:
#keyboard.setPressed(key)
keyboard.setKeyDown(key)
keyboard.setKeyUp(key)

def execute(self):
raise NotImplementedError("Subclass must implement execute method")

def update(self, curentTime):
pass


class KeyPress(KeyAction):
def __init__(self, key, duration = 0.07):
KeyAction.__init__(self, key)
self.duration = duration
self.time = time.time()
self.needUpdate = False

def execute(self):
# set the keys down
self.setKeyDown()
# start the update timer
self.time = time.time()
self.needUpdate = True

def update(self, currentTime):
if self.needUpdate:
# determine if we should stop pressing the keys
if (currentTime - self.time) >= self.duration:
self.setKeyUp()
self.needUpdate = False


class KeyRepeat(KeyAction):
def __init__(self, key, times, timeInterval = 0.14, duration = 0.07):
KeyAction.__init__(self, key)
self.times = times-1
self.timeInterval = timeInterval
# Duration cannot be larger than time interval
if (duration > timeInterval):
self.duration = timeInterval
else:
self.duration = duration
#internal use variables
self.time = time.time()
self.timesLeft = 0
self.needUpdate = False

def execute(self):
# set the keys down
self.setKeyDown()
# start the update timer
self.time = time.time()
self.timesLeft = self.times
self.needUpdate = True

def update(self, currentTime):
if self.needUpdate:
elapsedTime = currentTime - self.time
# Release Current Key
if (elapsedTime >= self.duration):
self.setKeyUp()
# End the update if the last key has been released
if (self.timesLeft == 0):
self.needUpdate = False
# Press Next KeyDown
if (elapsedTime >= self.timeInterval):
self.setKeyDown()
self.time = time.time()
self.timesLeft = self.timesLeft - 1


# ***********************************
# VoiceCommand Class
# ***********************************

class VoiceCommand:
def __init__(self, cmd, response, action = None):
self.cmd = cmd
self.response = response
self.action = action

def said(self, confidence, response=False):
return ((self.cmd != "") and speech.said(self.cmd, confidence))

def playResponse(self):
if self.response:
speech.say(self.response)

def execute(self):
if self.action:
self.action.execute()

def update(self, currentTime):
if self.action:
self.action.update(currentTime)


# ***********************************
# VoiceToKeyboard
# ***********************************

class VoiceToKeyboard:
def __init__(self, confidenceLevel, commands = None):
self.confidenceLevel = confidenceLevel
self.commands = []
self.setCommands(commands)

def setCommands(self, commands):
if isinstance(commands, list):
self.commands = commands

def addCommand(self, cmd, response, action = None):
self.commands.append( VoiceCommand(cmd, response, action) )

def executeLoop(self):
currentTime = time.time()
for command in self.commands:
# if said execute action
if command.said(self.confidenceLevel):
command.playResponse()
command.execute()
command.update(currentTime)


# ***********************************
# Config and commands
# ***********************************

if starting:
confidenceLevel = 0.7
v2k = VoiceToKeyboard( confidenceLevel )

# Voice response only
v2k.addCommand("Hello", "!Welcome! Initialising system.")

# Key Press
v2k.addCommand("Test single press", "Single key press", KeyPress( Key.A ))
v2k.addCommand("Test multiple press", "Multiple keys press", KeyPress( [ Key.LeftShift, Key.A ] ))

# Key Hold
v2k.addCommand("Test single hold", "Single key hold. 2 seconds", KeyPress( Key.B, 2 ))
v2k.addCommand("Test multiple hold", "Multiple keys hold. 2 seconds", KeyPress( [ Key.LeftShift, Key.B ], 2 ))

# Key Repeat
v2k.addCommand("Test single repeat", "Pressing C key 5 times", KeyRepeat( Key.C, 5 ))
v2k.addCommand("Test multiple repeat", "Pressing Shift and C keys 5 times", KeyRepeat( [ Key.LeftShift, Key.C ], 5 , 0.1, 0.07 ))


v2k.executeLoop()
FrenchyKiel
One Eyed Hopeful
Posts: 47
Joined: Thu Oct 03, 2013 7:10 am

Re: [Beginner] Can someone help with voice recognition pleas

Post by FrenchyKiel »

This code is fully functional

if you say "hello" the key "1" is pressed and released

Code: Select all

if speech.said("hello"):
	keyboard.setPressed(Key.D1)
Post Reply

Return to “FreePIE”