跟語音識別差不多,只是把所作的命令轉化為api對鍵盤的操作
from win32com.client import constants
import os
import win32api
import win32con
import time
import win32com.client
import pythoncom
class speechrecognition:
def __init__(self, wordstoadd):
self.speaker = win32com.client.dispatch("sapi.spvoice")
self.listener = win32com.client.dispatch("sapi.spsharedrecognizer")
self.context = self.listener.createrecocontext()
self.grammar = self.context.creategrammar()
self.grammar.dictationsetstate(0)
self.wordsrule = self.grammar.rules.add("wordsrule", constants.sratoplevel + constants.sradynamic, 0)
self.wordsrule.clear()
[self.wordsrule.initialstate.addwordtransition(none, word) for word in wordstoadd]
self.grammar.rules.commit()
self.grammar.cmdsetrulestate("wordsrule", 1)
self.grammar.rules.commit()
self.eventhandler = contextevents(self.context)
self.say("started successfully")
def say(self, phrase):
self.speaker.speak(phrase)
class contextevents(win32com.client.getevents("sapi.spsharedrecocontext")):
def onrecognition(self, streamnumber, streamposition, recognitiontype, result):
newresult = win32com.client.dispatch(result)
print("你說 ", newresult.phraseinfo.gettext())
speechstr = newresult.phraseinfo.gettext()
if speechstr == "趴下":
win32api.keybd_event(89, 0, 0, 0) #89對應鍵盤的y鍵,按下,經過0.1秒後,y鍵抬起
time.sleep(0.1)
win32api.keybd_event(89, 0, win32con.keyeventf_keyup, 0)
elif speechstr == "起來":
win32api.keybd_event(32, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(32, 0, win32con.keyeventf_keyup, 0)
elif speechstr == "龍王破":
win32api.keybd_event(83, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(83, 0, win32con.keyeventf_keyup, 0)
win32api.keybd_event(87, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(87, 0, win32con.keyeventf_keyup, 0)
win32api.keybd_event(87, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(87, 0, win32con.keyeventf_keyup, 0)
win32api.mouse_event(win32con.mouseeventf_leftdown,0,0,0) #滑鼠左鍵按下
win32api.mouse_event(win32con.mouseeventf_leftup,0,0,0) #滑鼠左鍵抬起
elif speechstr == "穿心破":
win32api.keybd_event(87, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(87, 0, win32con.keyeventf_keyup, 0)
win32api.keybd_event(87, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(87, 0, win32con.keyeventf_keyup, 0)
win32api.keybd_event(87, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(87, 0, win32con.keyeventf_keyup, 0)
win32api.mouse_event(win32con.mouseeventf_leftdown, 0, 0, 0)
win32api.mouse_event(win32con.mouseeventf_leftup, 0, 0, 0)
elif speechstr == "翔天破":
win32api.keybd_event(65, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(65, 0, win32con.keyeventf_keyup, 0)
win32api.keybd_event(68, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(68, 0, win32con.keyeventf_keyup, 0)
win32api.keybd_event(87, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(87, 0, win32con.keyeventf_keyup, 0)
win32api.mouse_event(win32con.mouseeventf_leftdown, 0, 0, 0)
win32api.mouse_event(win32con.mouseeventf_leftup, 0, 0, 0)
elif speechstr == "":
pass
elif speechstr == "閃":
win32api.keybd_event(82, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(82, 0, win32con.keyeventf_keyup, 0)
win32api.keybd_event(68, 0, 0, 0)
time.sleep(0.1)
win32api.keybd_event(68, 0, win32con.keyeventf_keyup, 0)
if __name__ == '__main__':
wordstoadd = ["趴下", "起來", "龍王破", "穿心破", "翔天破", "閃"]
speechreco = speechrecognition(wordstoadd)
while true:
pythoncom.pumpwaitingmessages()
離線語音控制並不等於本地語音識別
木瓜電子可以為國內各大家電產商提供離線語音控制的模組和晶元產品,但是沒有本地語音識別的方案。這兩者區別的地方是什麼呢?針對家電產品控制進行解釋一些這兩者之前的區別。離線語音控制,木瓜電子提供的離線語音控制模組和晶元,很明確這些語音產品的是適合應用在控制家電等裝置的,不需要連線網路進行語音識別,說出命...
C 語音識別(文字to語音 語音to文字)
最近打算研究一下語音識別,但是發現網上很少有c 的完整 就把自己的學習心得放上來,和大家分享一下。1 speechsdk51.exe 67.0 mb 2 speechsdk51langpack.exe 81.0 mb 文字to語音 這個相當的簡單。1 在com選項卡裡面的microsoft spee...
C 語音識別(文字to語音 語音to文字)
最近打算研究一下語音識別,但是發現網上很少有c 的完整 就把自己的學習心得放上來,和大家分享一下。1 speechsdk51.exe 67.0 mb 2 speechsdk51langpack.exe 81.0 mb 文字to語音 這個相當的簡單。1 在com選項卡裡面的microsoft spee...