用Python打造属于你的贾维斯

跟着老杨玩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 = 
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值