AI 基础

what AI ?

人工智能(Artificial Intelligence),英文缩写为AI.
人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器,该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。
人工智能不是人的智能,但能像人那样思考、也可能超过人的智能。
--> 人 创造的 '智慧能力', 同样具备
听 (语音识别)
说 (语音合成)
看 (图像视频文字识别) 交通抓违章 刷脸解锁 视频APP审核机制 文字识别身份证提取信息,扫一扫翻译
理解(语言文字图像视频理解等逻辑处理)
思考(理解后的逻辑处理) 等能力.

一些现在用的例子:

像  小米小爱同学, 苹果 Siri , 微软10 Cortana  交通摄像抓违章  银行卡申办机器  联通10010智能后台  等等 .. .. 

怎么降低入门门槛呢?  一家很开放的公司?  就是  Baidu 大法

ai.baidu.com   -- 百度AI平台   我们学习就用这个

下面 show time:

首先  www.ai.baidu.com   登录进去  进入控制台
之后安装 百度工具包 pip install baidu-aip ,根据百度技术文档,python sdk配合使用.
     pycharm里面解释器下搜索出来install也行
-- 语音合成(文字转语音) -- 
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)  #以上代码,常量APP_ID在百度云控制台中创建,常量API_KEY与SECRET_KEY是在创建完毕应用后,
系统分配给用户的,均为字符串,用于标识用户,为访问做签名验证,可在AI服务控制台中的应用列表中查看。

result = client.systhesis('你好,见到你很高兴','zh',1,{
    'vol':5, #音量,取值0-15,默认为5中音量
    'pit':6, #音调,取值0-9,默认为5中语调
    'spd':4, #语速,取值0-9,默认为5中语速
    'per':0 #发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
})

if not isinstance(result,dict):
    with oprn('audio.mp3','wb') as f:
        f.write(result)
#这样就会生成一个audio.mp3的音频,打开即可收听到 '你好,见到你很高兴'
-- 语音识别(录音转文字) -- 
tips:我们继续根据百度Python SDK技术文档进行使用;简述注意事项: 1.支持的音频格式有pcm、amr(压缩格式); 2.语音上限时长60s,超过就会报错!
-- 音频转码准备
FFmpeg
1. 下载链接 http://www.ffmpeg.org/download.html
2.下载到安装包后,进行解压,找到bin目录,添加到windows环境变量,重启pycharm.,然后便可以执行转换音频格式了
3. ffmpeg -y  -i a.wma  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 a.pcm
4. cmd 下面检验下有没好
语音转文字
from aip import AipSpeech
import os
APP_ID = '15845079'
API_KEY = '5GGaPeBu2I0LsONjlWhM04yL'
SECRET_KEY = 'Uo8zIi8VRZMRgqKkCQaqaGIorsQtkDlw'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 读取文件
def get_file_content(filePath):
    os.system(f'ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm')
    with open(f'{filePath}.pcm', 'rb') as fp:
        return fp.read()

# 识别本地文件
res = client.asr(get_file_content('ob.wma'), 'pcm', 16000, {
    'dev_pid': 1536, #标准普通话,简单英语
})
print(res.get('result')[0])

 简单二合一

from aip import AipSpeech
from aip import AipNlp
import os

APP_ID = '15845079'
API_KEY = '5GGaPeBu2I0LsONjlWhM04yL'
SECRET_KEY = 'Uo8zIi8VRZMRgqKkCQaqaGIorsQtkDlw'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)  # 实例化
nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)  # 实例化

res = nlp_client.simnet('你叫什么名字','你贵姓')
print(res)

# 读取文件
def get_file_content(filePath):
    os.system(f'ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm')
    with open(f'{filePath}.pcm', 'rb') as fp:
        return fp.read()

# 识别本地文件
res = client.asr(get_file_content('ob.wma'), 'pcm', 16000, {
    'dev_pid': 1536,
})
text = res.get('result')[0]
print(text)

# 智能问答
def my_nlp(text):

    if nlp_client.simnet(text,'你叫什么名字').get('score')>=0.58:
        A = '我叫音王吧'
        return A

    if nlp_client.simnet(text, '你今年几岁了').get('score') >= 0.58:
        A = '千年999'
        return A
    A = '我不知道你在说什么'
    return A
A = my_nlp(text)

result = client.synthesis(A, 'zh', 1, {
    'vol': 5,
    'per': 4,
    'spd': 4,
    'pit': 7
})
    # import time

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    # with open(f'{time.time()}.mp3', 'wb') as f:
    with open('audio.mp3', 'wb') as f:
        f.write(result)

os.system('audio.mp3')  # 自动播放

# 自然语言处理  短文本相似度  AipNlp是自然语言处理的Python SDK客户端,为使用自然语言处理的开发人员提供了一系列的交互方法
# 相似度结果  运算出来 score   低于0.58的可以丢弃   大于0.8的基本认定一致

白热化  智能机器人 初实现

首先访问 www.tuling123.com  注册后 登录创建一个机器人
要用  api接入
文档  https://www.kancloud.cn/turing/www-tuling123-com/718218
接入教程 2.0  
接口地址  http://openapi.tuling123.com/openapi/api/v2
请求方式  HTTP POST
请求参数  参数格式要求 json
示例:
{
"reqType":0, "perception": { "inputText": { "text": "附近的酒店" }, "inputImage": { "url": "imageUrl" }, "selfInfo": { "location": { "city": "北京", "province": "北京", "street": "信息路" } } }, "userInfo": { "apiKey": "", "userId": "" } }

一个对话处理   处理音频后返回音频自动播放

from aip import AipSpeech
from aip import AipNlp
import os

APP_ID = '15845079'
API_KEY = '5GGaPeBu2I0LsONjlWhM04yL'
SECRET_KEY = 'Uo8zIi8VRZMRgqKkCQaqaGIorsQtkDlw'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)  # 实例化
nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY)  # 实例化

res = nlp_client.simnet('你叫什么名字','你贵姓')
print(res)

# 读取文件
def get_file_content(filePath):
    os.system(f'ffmpeg -y  -i {filePath}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm')
    with open(f'{filePath}.pcm', 'rb') as fp:
        return fp.read()

# 识别本地文件
res = client.asr(get_file_content('naojin.wma'), 'pcm', 16000, {
    'dev_pid': 1536,
})
text = res.get('result')[0]
print(text)

# 智能问答
import requests  # 请求模块

def to_tuling(text,uid):  # 问题
    data = {
        "reqType":0,
        "perception": {
            "inputText": {
                "text": "上海"
            },
        },
        "userInfo": {
            "apiKey": "29f1be0165d74f7290a8cd899a4358a8",
            "userId": "123"
        }
    }
    data['perception']['inputText']['text'] = text
    data['userInfo']['userId'] = uid
    res = requests.post('http://openapi.tuling123.com/openapi/api/v2',json=data)
    print(res)

    res_json = res.json()
    text = res_json.get('results')[0].get('values').get('text')
    return text

def my_nlp(text):

    if nlp_client.simnet(text,'你叫什么名字').get('score')>=0.58:
        A = '我叫音王吧'
        return A

    if nlp_client.simnet(text, '你今年几岁了').get('score') >= 0.58:
        A = '千年999'
        return A
    A = to_tuling(text,'007')
    return A
A = my_nlp(text)

result = client.synthesis(A, 'zh', 1, {
    'vol': 5,
    'per': 4,
    'spd': 4,
    'pit': 7
})
    # import time
    
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    # with open(f'{time.time()}.mp3', 'wb') as f:
    with open('audio.mp3', 'wb') as f:
        f.write(result)
os.system('audio.mp3')  # 自动播放
posted @ 2019-03-25 17:01  追风zz  阅读(542)  评论(0)    收藏  举报