不多逼逼,上代码
GUI设计在另一个帖子上
百度语音api获取自行百度
import codecs import os from aip import AipSpeech import speech_recognition as sr from pynput.keyboard import Key, Controller,Listener # import GUIsetting # 检查并创建命令文件 import GUIsetting audio_order_file = "./audio_order.txt" def order_file(): if not os.path.exists(audio_order_file): with codecs.open(audio_order_file, 'a+', encoding='utf-8') as f: f.write("") # 执行 order_file() # 音频元 audio_file = "./audio.wav" # 百度api相关key # 从API_txt中调取 pass_word = [] pass_file = open("./API_KEY.txt","r",encoding="utf-8") lines=pass_file.readlines() for line in lines: pass_word.append(line.strip("")) ad = pass_word[0] ak = pass_word[1] sk = pass_word[2] # 赋值 APP_ID = '{}'.format(ad) API_KEY = '{}'.format(ak) SECRET_KEY = '{}'.format(sk) client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # PC端麦克风调用 def reco(): r = sr.Recognizer() try: with sr.Microphone(sample_rate=16000) as source: print("你说") audio = r.listen(source,timeout=3) with open(audio_file,'wb') as f: f.write(audio.get_wav_data()) except: print('录音内容为空') # 命令储存列表(逻辑:百度识别后将字符输入,提取命令与之对比) list=[] # 百度api识别 def asr(audio_file): try: with open(audio_file, 'rb') as fp: music=fp.read() result=client.asr(music, 'wav', 16000, {'dev_pid': 1536,}) list.append(result['result'][0]) return result['result'][0] except: print('无法识别口令有误') list.append(1) # ###############################print(list) # 通过txt创建字典:应用命令语句=应用地址 read_dict = {} # 获取"./audio_order.txt"中所含命令函数 def get_order(): fileRead=open(audio_order_file,'r',encoding='utf-8') lines=fileRead.readlines() print(lines) while '\n' in lines: lines.remove("\n") print("ok:",lines) for line in lines: line_list = line.split(',') # 每行按逗号分隔成列表 order_id = line_list[0] # 取到id app_path = line_list[1] # 取到name read_dict[order_id] = app_path # 产生键值对,其中key是id print(order_id, app_path) # #############################测试|print(read_dict) # 运行 get_order() # 热键获取 button_value = [] button_file = open("./button_KEY.txt","r",encoding="utf-8") lines=button_file.readlines() for line in lines: button_value.append(line) hot_button = button_value[0] # 热键唤醒 def on_press(key): try: if key.char == '{}'.format(hot_button): reco() if asr(audio_file) in read_dict: print('正在打开') a = read_dict.get(list[0]) print(a) # 检验路径是否存在 K=os.path.exists(a.strip("\u202a")) print(K) #启动程序 os.system(r'start {}'.format(a.strip("\u202a"))) # ?未出现说明该路径程序启动后中断监听!!!!!!!!! print("?") del list[0] os.remove(audio_file) print('打开完毕') else: print('错误指令重新聆听') del list[0] # 热键唤醒设置导航 elif key.char == 'b': GUIsetting.window_0() get_order() except AttributeError: print("正在按压:",format(Key)) # 键盘监听 def start_listen(): with Listener(on_press=on_press) as listener: get_order() print('等待指令') listener.join() start_listen() #进程处理c # import multiprocessing as mp # def process(): # global p # p = mp.Process(target=start_listen) # p.start() # process()