AI百度接口以及图灵接口的使用

百度AI接口

AI智能种类方向

耳朵 = 倾听 = 麦克风 = 语音识别 ASRAutomatic Speech Recognition

嘴巴 = 诉说 = 扬声器 = 语音合成 TTSText To Speech

眼睛 = 观察 = 摄像头 = 图像识别 IRImage Recognition

思考 = 理解 = 逻辑处理 = 自然语言处理:NLP Natural Language Processing

更多种类方向详见百度AI开放平台文档https://ai.baidu.com/docs#/

以下均为使用百度AI开放平台:https://ai.baidu.com/ 以及图灵机器人:http://www.turingapi.com/ 且需导入baidu-aip包,用pip3 install baidu-aip

ASR语音识别

文档帮助

https://ai.baidu.com/docs#/ASR-Online-Python-SDK/top

步骤

l  首先需要将录好的音频文件格式转换为pcm格式,用到了ffmpeg工具,解压后直接剪切文件夹到自定义的目录下,然后切换到文件夹中的bin目录下,复制路径添加到path中。

ffmpeg下载地址:链接: https://pan.baidu.com/s/1HQhbcrj806OWCTzJDEL5vw 提取码: 2333

 

转换语音文件代码:

1 import os
2 
3 filepath = input('请输入文件路径:')
4 print(filepath)
5 filename = filepath[:-4:]  # 仅限于m4a格式,可根据文件格式后缀长度更改
6 print(filename)
7 cmd_pcm = f"ffmpeg -y -i {filepath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filename}.pcm"
8 os.system(cmd_pcm)
9 print('格式更改完成!')

l  转换好以后,在ASR语音识别代码中用到:

 1 from aip import AipSpeech
 2 
 3 
 4 """ 你的 APPID AK SK """
 5 APP_ID = '你的ID'
 6 API_KEY = '你的KEY'
 7 SECRET_KEY = '你的KEY'
 8 
 9 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
10 
11 
12 # 读取文件
13 def get_file_content(filepath):
14     with open(filepath, 'rb') as fp:
15         return fp.read()
16 
17 
18 # 识别本地文件
19 filepath=input('请输入语音文件路径:')
20 res=client.asr(get_file_content(filepath), 'pcm', 16000, {
21     'dev_pid': 1536,
22 })
23 
24 
25 print(res.get('result')[0])

TTS语音合成

文档帮助

https://ai.baidu.com/docs#/TTS-Online-Python-SDK/top

代码

 1 import os
 2 from aip import AipSpeech
 3 
 4 """ 你的 APPID AK SK """
 5 APP_ID = '需要更改处'
 6 API_KEY = '需要更改处'
 7 SECRET_KEY = '需要更改处'
 8 
 9 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
10 
11 content = input('请输入需要转换成语音的内容:')
12 result = client.synthesis(content, 'zh', 1, {
13     'vol': 5,
14 })
15 
16 # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
17 if not isinstance(result, dict):
18     with open(os.getcwd() + 'statics\TTS.mp3', 'wb') as f:
19         f.write(result)
20 
21 print('转换完成!')

NLP自然语言处理

文档帮助

https://ai.baidu.com/docs#/NLP-Python-SDK/top

代码

 1 from aip import AipNlp
 2 
 3 """ 你的 APPID AK SK """
 4 APP_ID = '需要更改'
 5 API_KEY = '需要更改'
 6 SECRET_KEY = '需要更改'
 7 
 8 client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
 9 text1 = input('输入对比的字段1:')
10 text2 = input('输入对比的字段2:')
11 res = client.simnet(text1, text2)
12 print(res)
13 print(res.get('score'))

图灵接入

文档帮助

https://www.kancloud.cn/turing/www-tuling123-com/718227

代码

import requests

question=input('输入想要提问的问题:')

data = {
    "reqType": 0,
    "perception": {
        "inputText": {
            "text": question
        },
        "inputImage": {
            "url": "imageUrl"
        },
        "selfInfo": {
            "location": {
                "city": "北京",
                "province": "北京",
                "street": "信息路"
            }
        }
    },
    "userInfo": {
        "apiKey": "需要更改",
        "userId": "需要更改"
    }
}
res = requests.post('http://openapi.tuling123.com/openapi/api/v2', json=data)
res_dict = res.json()
print(res_dict.get("results")[0].get("values").get("text"))

语音加图灵结合

 1 import requests
 2 from aip import AipSpeech
 3 
 4 
 5 def Asr():
 6     """ 你的 APPID AK SK """
 7     APP_ID = '需要更改'
 8     API_KEY = '需要更改'
 9     SECRET_KEY = '需要更改'
10 
11     client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
12 
13     # 读取文件
14     def get_file_content(filepath):
15         with open(filepath, 'rb') as fp:
16             return fp.read()
17 
18     # 识别本地文件
19     filepath = input('请输入语音文件路径:')
20     res = client.asr(get_file_content(filepath), 'pcm', 16000, {
21         'dev_pid': 1536,
22     })
23     return res.get('result')[0]
24 
25 
26 data = {
27     "reqType": 0,
28     "perception": {
29         "inputText": {
30             "text": Asr()
31         },
32         "inputImage": {
33             "url": "imageUrl"
34         },
35         "selfInfo": {
36             "location": {
37                 "city": "北京",
38                 "province": "北京",
39                 "street": "信息路"
40             }
41         }
42     },
43     "userInfo": {
44         "apiKey": "需要更改",
45         "userId": "需要更改"
46     }
47 }
48 res = requests.post('http://openapi.tuling123.com/openapi/api/v2', json=data)
49 res_dict = res.json()
50 print(res_dict.get("results")[0].get("values").get("text"))
语音图灵结合

结果

图灵

百度

 

原文地址:https://www.cnblogs.com/1oo88/p/11283898.html