耳朵 = 傾聽 = 麥克風 = 語音識別 ASR:Automatic Speech Recognitionjson
嘴巴 = 訴說 = 揚聲器 = 語音合成 TTS:Text To Speechapi
眼睛 = 觀察 = 攝像頭 = 圖像識別 IR:Image Recognitionide
思考 = 理解 = 邏輯處理 = 天然語言處理:NLP Natural Language Processing工具
更多種類方向詳見百度AI開放平臺文檔:https://ai.baidu.com/docs#/post
如下均爲使用百度AI開放平臺:https://ai.baidu.com/ 以及圖靈機器人:http://www.turingapi.com/ 且需導入baidu-aip包,用pip3 install baidu-aipurl
https://ai.baidu.com/docs#/ASR-Online-Python-SDK/topspa
l 首先須要將錄好的音頻文件格式轉換爲」pcm」格式,用到了ffmpeg工具,解壓後直接剪切文件夾到自定義的目錄下,而後切換到文件夾中的bin目錄下,複製路徑添加到path中。code
ffmpeg下載地址:連接: https://pan.baidu.com/s/1HQhbcrj806OWCTzJDEL5vw 提取碼: 2333orm
轉換語音文件代碼:blog
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])
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('轉換完成!')
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"))