Python實(shí)現(xiàn)語音啟動(dòng)電腦應(yīng)用程序
實(shí)現(xiàn)思路
osk模型進(jìn)行輸入語音轉(zhuǎn)換
txt字典導(dǎo)航程序路徑
pyttsx3引擎進(jìn)行語音打印輸出
關(guān)鍵詞=程序路徑
完整代碼
import os import json import queue import sounddevice as sd from vosk import Model, KaldiRecognizer import subprocess import time import pyttsx3 import threading # 初始化 pyttsx3 引擎 engine = pyttsx3.init() engine.setProperty('rate', 150) # 設(shè)置語速 engine.setProperty('volume', 1.0) # 設(shè)置音量 # 加載Vosk模型 model_path = r"D:\daku\yuyinshibie\vosk-model-small-cn-0.22" if not os.path.exists(model_path): print(f"模型路徑不存在: {model_path}") engine.say(f"模型路徑不存在: {model_path}") engine.runAndWait() exit(1) # 讀取字典文件,格式為 "命令=程序路徑" def load_app_dict(file_path): app_dict = {} if not os.path.exists(file_path): print(f"字典文件不存在: {file_path}") engine.say(f"字典文件不存在: {file_path}") engine.runAndWait() return app_dict with open(file_path, 'r', encoding='utf-8') as file: for line in file: parts = line.strip().split('=') if len(parts) == 2: keys, value = parts # 處理可能存在的別名情況,例如 "微信,weixin" for key in keys.split(','): app_dict[key.strip()] = value.strip() return app_dict # 啟動(dòng)應(yīng)用程序 def launch_application(app_name, app_dict): if app_name in app_dict: app_path = app_dict[app_name] response = f"正在啟動(dòng) {app_name}..." say(response) subprocess.Popen(app_path) time.sleep(2) # 等待2秒再繼續(xù)監(jiān)聽 else: response = f"找不到與 '{app_name}' 對(duì)應(yīng)的應(yīng)用程序。" say(response) # 定義一個(gè)函數(shù)用于語音輸出,并在說的時(shí)候暫停監(jiān)聽 def say(text): global stream, callback_func if stream is not None: with stream_lock: stream.callback = None # 移除回調(diào)函數(shù)以暫停監(jiān)聽 stream.stop() # 暫停音頻流 engine.say(text) engine.runAndWait() if stream is not None: with stream_lock: stream.start() # 恢復(fù)音頻流 stream.callback = callback_func # 重新設(shè)置回調(diào)函數(shù) # 初始化模型和識(shí)別器 model = Model(model_path) rec = KaldiRecognizer(model, 16000) q = queue.Queue() last_partial_result = "" last_full_command = "" stream_lock = threading.Lock() stream = None callback_func = None def callback(indata, frames, time, status): if status: print(status, file=sys.stderr) q.put(bytes(indata)) # 主程序 if __name__ == "__main__": dict_file = r"D:\daku\yuyinshibie\zidian.txt" # 字典文件路徑 app_dict = load_app_dict(dict_file) try: # 提前初始化音頻流 callback_func = callback stream = sd.RawInputStream(samplerate=16000, blocksize=8000, dtype='int16', channels=1, callback=callback) stream.start() say("請(qǐng)說:") while True: data = q.get() if rec.AcceptWaveform(data): result = json.loads(rec.Result()) command = result['text'].strip() if command and command != last_full_command: print(f"你說的是: {command}") say(f"你說的是: {command}") if "打開" in command: app_to_open = command.replace("打開", "").strip() launch_application(app_to_open, app_dict) last_full_command = command elif rec.PartialResult(): partial_result = json.loads(rec.PartialResult())['partial'] if partial_result and "打開" in partial_result and partial_result != last_partial_result: print(f"部分結(jié)果: {partial_result}") say(f"部分結(jié)果: {partial_result}") last_partial_result = partial_result except KeyboardInterrupt: say("\n退出程序。") finally: if stream is not None: stream.stop() stream.close()
關(guān)鍵詞部分,為了識(shí)別準(zhǔn)確以及出現(xiàn)諧音內(nèi)容可以增添多個(gè)關(guān)鍵詞使用,作為分割
字典路徑如果出現(xiàn)中文字符有可能會(huì)報(bào)錯(cuò)!
代碼意義不大,如果考慮深入:可以嘗試增加快捷鍵,以及相關(guān)應(yīng)用接口可以更好控制
上班族打開電腦i第一件事情是啟動(dòng)相關(guān)應(yīng)用,同樣可以嘗試多應(yīng)用編組啟動(dòng)
到此這篇關(guān)于Python實(shí)現(xiàn)語音啟動(dòng)電腦應(yīng)用程序的文章就介紹到這了,更多相關(guān)Python語音啟動(dòng)電腦內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)對(duì)字符串中字符提取校驗(yàn)
眾所周知,python之所以很方便在一定程度上是因?yàn)殡S時(shí)都可能有人又創(chuàng)作了一個(gè)好用又方便的python非標(biāo)準(zhǔn)庫(kù)。本文就來用Python實(shí)現(xiàn)對(duì)字符串中字符進(jìn)行提取校驗(yàn),需要的可以參考一下2022-10-10Python中FTP服務(wù)與SSH登錄暴力破解的實(shí)現(xiàn)
本文學(xué)習(xí)了如何通過 Python 腳本進(jìn)行 FTP、SSH 服務(wù)的登錄爆破,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08python實(shí)現(xiàn)數(shù)據(jù)可視化超詳細(xì)講解
Python的數(shù)據(jù)可視化是將數(shù)據(jù)以圖形或圖表的形式呈現(xiàn),使復(fù)雜的信息更易于理解和分析,本文給大家詳細(xì)介紹了python數(shù)據(jù)可視化的實(shí)現(xiàn),文中通過圖文結(jié)合的方式介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06Python實(shí)現(xiàn)元素等待代碼實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)元素等待代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11python實(shí)現(xiàn)計(jì)算器簡(jiǎn)易版
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)計(jì)算器簡(jiǎn)易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12通過python3實(shí)現(xiàn)投票功能代碼實(shí)例
這篇文章主要介紹了通過python3實(shí)現(xiàn)投票功能代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09