跟着老杨玩Python
看了复联后,各种羡慕斯塔克,成为不了钢铁侠,那就自己给自己造一个贾维斯出来吧。不仅能控制电脑,还能和我智能聊天
这是主文件,创建一个可视化的桌面,及主要业务功能
#!/usr/bin/python3
# @Time : 2019/5/30 14:54
# @Author : 老杨
# @FileName : iron.py
# @Software : PyCharm
# pip install baidu-aip
# pip install pyttsx3
# pip install requests
# http://ai.baidu.com/docs#/ASR-Online-Python-SDK/top
from tkinter import *
from aip import AipSpeech
import Recorder
import Control
import pyttsx3
import requests
import json
# pip install jsonpath
# import jsonpath
def action():
data = entry1.get().strip()
control = Control.Order()
control.run(data)
rec = Recorder.Recorder()
def ok(event):
print('开始录音')
rec.start()
def no():
print('结束录音')
# 结束录音
rec.stop()
# 保存音频
file = rec.save('audio')
# 调用百度aip语音识别转义成文字
APP_ID = '你的百度ID'
API_KEY = '你的百度API_KEY'
SECRET_KEY = '你的百度SECRET_KEY'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
with open(file,'rb') as fp:
data = fp.read()
result = client.asr(data,'wav',16000,{
'dev_pid':1536
})
print(result)
try:
data = result['result'][0]
except Exception as err:
exit(err)
return data
def voiceControl(event):
result = no()
print(result)
# 提取前三位,判断是操控还是聊天
data = result[0:3]
order = result[3:]
if data =='贾维斯':
control = Control.Order()
control.run(order)
else:
show(result)
# def show(content):
# # 构造请求参数
# request_data = {
# "reqType":0,
# "perception": {
# "inputText": {
# "text": content
# },
# },
# "userInfo": {
# "apiKey": "d3c006b99ee94e28b6d8340f68577f02",
# "userId": "laoYang"
# }
# }
# # 构造请求url
# url = "http://openapi.tuling123.com/openapi/api/v2"
# # 发送请求
# request_data = json.dumps(request_data)
# result = requests.post(url,request_data)
# # 转换成json格式
# response = json.loads(result.text)
# # 通过jsonpath取到对应的值
# data = jsonpath.jsonpath(response,'$..text')
# # 语音输出
# engine = pyttsx3.init()
# engine.say(data)
# engine.runAndWait()
def show(content):
# 构造请求参数
request_data =