Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created July 18, 2020 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typemytype/c4337ff87ec7314c3c58f634c3a3a06c to your computer and use it in GitHub Desktop.
Save typemytype/c4337ff87ec7314c3c58f634c3a3a06c to your computer and use it in GitHub Desktop.
speech to command
import AppKit
import vanilla
from lib.tools.debugTools import ClassNameIncrementer
class SpeechRecognizerDelegate(AppKit.NSObject, metaclass=ClassNameIncrementer):
def speechRecognizer_didRecognizeCommand_(self, recognizer, command):
# do something with this command!!
print(command)
class TestWindow(object):
def __init__(self):
self.w = vanilla.Window((300, 50))
self.w.b = vanilla.Button((10, 10, -10, 22), "start", callback=self.hitCallback)
self.w.open()
def hitCallback(self, sender):
if sender.getNSButton().title() == "start":
print("start")
sender.setTitle("stop")
commands = [
"hello",
"move points",
"delete contour",
"draw a red circle",
"draw a blue rectangle",
]
self.recognizerDelegate = SpeechRecognizerDelegate.alloc().init()
self.recognizer = AppKit.NSSpeechRecognizer.alloc().init()
self.recognizer.setDelegate_(self.recognizerDelegate)
self.recognizer.setCommands_(commands)
self.recognizer.startListening()
elif sender.getNSButton().title() == "stop":
print("stop")
sender.setTitle("start")
self.recognizer.stopListening()
del self.recognizerDelegate
del self.recognizer
TestWindow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment