人工智能 - 语音合成,语音识别,语音聊天机器人

现成的AI算法平台http://ai.baidu.com/

语音合成

根据百度大佬提供的文档,作出以下小栗子:
1.下载

pip install baidu-aip 

2.代码

    from aip import AipSpeech
    
    """ 你的 APPID AK SK 
        控制台 -> 百度语音-> 创建或管理应用
    """
    APP_ID = '15837844'
    API_KEY = '411VNGbuZVbDNZU78LqTzfsV'
    SECRET_KEY = '84AnwR2NARGMqnC6WFnzL9WWdWh5bW'
    
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)  # 实例化
    
    
    def text2audio(text):
        """一些参数的配置,详见文档"""
        result = client.synthesis(text, 'zh', 1, {
            'vol': 5,
            'per': 4,
            'spd': 4,
            'pit': 7,
        })
    
        # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
        if not isinstance(result, dict):
            print(result)
            with open('audio.mp3', 'wb') as f:
                f.write(result)

语音识别

1.下载 ffmpeg 用于将语音文件转换为pcm格式

链接: https://pan.baidu.com/s/1jonSAa_TG2XuaJEy3iTmHg

密码: w6hk

  1. pip pip install baidu-aip

3.代码

from aip import AipSpeech
    import os
    
    """ 你的 APPID AK SK """
    APP_ID = '15837844'
    API_KEY = '411VNGbuZVbDNZU78LqTzfsV'
    SECRET_KEY = '84AnwR2NARGMqnC6WFnzL9WWdWh5bW'
    
    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")  # 使用ffmpeg转换格式
        with open(f"{filePath}.pcm", 'rb') as fp:
            return fp.read()
    
    
    # 识别本地文件
    res = client.asr(get_file_content('wyn.wav'), 'pcm', 16000, {
        'dev_pid': 1536,
    })
    
    
    print(res.get("result")[0])
    
    
    # if res.get("result")[0] == "你叫什么名字":
    #     text2audio("我叫银王八")

语音聊天机器人

1.为了更快捷的开发, 我们可以调用福林机器人的API

http://www.tuling123.com/

在福林机器人里自定义机器人部分功能.

2.代码

from aip import AipSpeech
    from aip import AipNlp  # 自然语言处理,词义相似度
    import os
    
    """ 你的 APPID AK SK """
    APP_ID = '15837844'
    API_KEY = '411VNGbuZVbDNZU78LqTzfsV'
    SECRET_KEY = '84AnwR2NARGMqnC6WFnzL9WWdWh5bW'
    
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    nlp_client = AipNlp(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('jgxh.wma'), 'pcm', 16000, {
        'dev_pid': 1536,
    })
    text = res.get("result")[0]
    
    """福林机器人智能回答函数"""
    import requests
    
    
    def to_tuling(text, uid):
        data = {
            "perception": {
                "inputText": {
                    "text": "北京"
                }
            },
            "userInfo": {
                "apiKey": "a4c4a668c9f94d0c928544f95a3c44fb",
                "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.content)
        res_json = res.json()
    
        text = res_json.get("results")[0].get("values").get("text")
        print(text)
        return text
    
    
    """语音相似度判断函数"""
    def my_nlp(text):
        if nlp_client.simnet(text, "你叫什么名字").get("score") >= 0.75:
            A = "我叫如花"
            return A
    
        if nlp_client.simnet(text, "你今年几岁了").get("score") >= 0.75:
            A = "我今年999岁了"
            return A
        
        A = to_tuling(text, "open123")  # 如果不符合自定义条件,那么调用福林机器人API
        return A
    
    """开始执行"""
    A = my_nlp(text)
    result = client.synthesis(A, 'zh', 1, {
        'vol': 5,
        'per': 4,
        'spd': 4,
        'pit': 7,
    })
    
    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        # print(result)
        with open('audio.mp3', 'wb') as f:
            f.write(result)
    
    """自动执行audio.mp3, 打开软件为默认打开软件"""
    os.system('audio.mp3')

 

文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归趣营销www.SEOgUrublog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ卍61910465