下面是一個簡單的人工智能對話的程序,做起來很簡單基於倆個工具就可以完成了.
百度AI開放平台: ai.baidu.com 里面有很多實現人工智能的模型可以免費使用
圖靈機器人: www.turingapi.com 定制一個屬於自己的專屬智能機器人
在進行語音識別的時候,要求傳入音頻的格式是pcm,我們電腦的錄音機的格式不是人家要求的需要對格式進行轉碼,在這之前需要下載安裝插件 http://ffmpeg.org/download.html
這樣就可以玩了.
首先我們需要去錄音機錄制一段語音.稍后要進行使用.
下面是具體代碼,語音合成和識別的代碼其實特別簡單,按照百度文檔里的內容復制粘貼就行

from aip import AipSpeech from to_tolin import to_tolin import os """讀取音頻文件,語音轉化成字符串傳給圖靈""" """ 你的 APPID AK SK """ APP_ID = '15838233' API_KEY = 'Xy8d3wRbsjfmpGsPryvNexcL' SECRET_KEY = 'PmpwVdVj0mHU1BTeAhyXZNlBi8CrPOP2' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 讀取文件 def get_file_content(filePath): new = filePath.split(".")[0] os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {new}.pcm") with open(f"{new}.pcm", 'rb') as fp: return fp.read() # 識別本地文件 ret = client.asr(get_file_content('auido.mp3'), 'pcm', 16000, { 'dev_pid': 1536, }) ret_new = ret.get("result")[0] to_tolin(ret_new)

import requests from output import VoiceOutput """借用第三方,進行語音智能聊天,圖靈返回的字符串傳回給output.py,把語言合成並播放出來""" def to_tolin(text): data = { "perception": { "inputText": { "text": f"{text}" }, }, "userInfo": { "apiKey": "5598e8fcceda4ea4a005892bcb053ee8", "userId": "1" } } res = requests.post("http://openapi.tuling123.com/openapi/api/v2",json=data) new_res = res.json().get("results")[0].get("values").get("text") print(new_res) VoiceOutput(new_res)

from aip import AipSpeech import os """ 把圖靈機器人回復的話通過語音合成,輸出 """ def VoiceOutput(text): """ 你的 APPID AK SK """ APP_ID = '15838233' API_KEY = 'Xy8d3wRbsjfmpGsPryvNexcL' SECRET_KEY = 'PmpwVdVj0mHU1BTeAhyXZNlBi8CrPOP2' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis(f'{text}', 'zh', 1, { 'vol': 5, 'per': 4, 'pit': 7, 'spd': 4 }) print(text) # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼 if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: f.write(result) os.system('auido.mp3')
由於我懶得去一邊一邊去錄,所以我做的是機器人自問自答的 哈哈哈哈