python实现语音控制

语音控制

'''
    当别人打开这个程序时,其电脑桌面会变成恐怖图片,并播放恐怖音乐
'''
import time
import pygame

import win32api
import win32con
import win32gui

#线程模块
import threading


def go():
    pygame.mixer.init()
    while True:
        for i in range(1,5):
            filePath = r"E:\pycharm project\自动化办公和鼠标键盘模拟\p"+"\\"+str(i)+".mp3"

            track = pygame.mixer.music.load(filePath)
            pygame.mixer.music.play()
            time.sleep(30)
            pygame.mixer.music.stop()

def setWallpaper(path):
    reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)

    win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "2")

    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, path, win32con.SPIF_SENDWININICHANGE)


th = threading.Thread(target=go,name="loopTread")
th.start()
while True:
    for i in range(1,5):
        filePath = r"E:\pycharm project\自动化办公和鼠标键盘模拟\p"+"\\"+str(i)+".jpg"
        setWallpaper(filePath)
        time.sleep(4)
        
  • 下面是语音控制的一个例子
    • 需要用到windows里的一个语音程序
# _*_ coding:utf-8 _*_

from win32com.client import constants
import os
import win32com.client
import pythoncom

import time
import pygame

import win32api
import win32con
import win32gui

speaker = win32com.client.Dispatch("SAPI.SPVOICE")

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=="方世玉":
            speaker.Speak("a very very good man")
        elif  speechstr=="你好":
            speaker.Speak("hello")
        elif  speechstr=="国庆快乐":
            speaker.Speak("Happy   nationalday")
        elif  speechstr=="新年快乐":
            speaker.Speak("happy  New Year")
        elif  speechstr=="耿昆仑":
            speaker.Speak("a  very very bad man")
        elif  speechstr=="你大爷":
            speaker.Speak("去你的")
        elif  speechstr=="方世玉是谁":
            speaker.Speak("一个编程高手")
        elif speechstr=="播放音乐":
            #播放音乐
            filePath = r"E:\pycharm project\自动化办公和鼠标键盘模拟\p\3.mp3"
            pygame.mixer.init()
            track = pygame.mixer.music.load(filePath)
            pygame.mixer.music.play()
            time.sleep(200)
            pygame.mixer.music.stop()

        elif speechstr=="更换壁纸":

            path = r"E:\pycharm project\自动化办公和鼠标键盘模拟\p\4.jpg"
            reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0,
                                            win32con.KEY_SET_VALUE)
            win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "2")
            win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, path, win32con.SPIF_SENDWININICHANGE)

            #报错
            #setWallpaper(r"E:\pycharm project\自动化办公和鼠标键盘模拟\p\4.jpg")


if __name__ == '__main__':
    speaker.Speak("语音识别开启,方爷你好!")
    wordsToAdd = ["方世玉",
                  "你好",
                  "国庆快乐",
                  "新年快乐",
                  "耿昆仑",
                  "你大爷",
                  "方世玉是谁",
                  "播放音乐",
                  "更换壁纸"]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()
        
发布了51 篇原创文章 · 获赞 29 · 访问量 2392

猜你喜欢

转载自blog.csdn.net/fangweijiex/article/details/103717057