天天看點

python機器人語音_python語音機器人

# -*- coding: utf-8 -*-#

# -------------------------------

# Name:SpeechRobot

# Date:2019年10月21日 09:43:46

# 用python3實作自己的語音對話機器人

# -------------------------------

from aip import AipSpeech

import requests

import json

import speech_recognition as sr

import win32com.client

# 初始化語音

speaker = win32com.client.Dispatch("SAPI.SpVoice")

# 1、語音生成音頻檔案,錄音并以目前時間戳儲存到voices檔案中

# Use SpeechRecognition to record 使用語音識别錄制

def my_record(rate=16000):

r = sr.Recognizer()

with sr.Microphone(sample_rate=rate) as source:

print("你好,我是機器人,有什麼想問的嗎?")

speaker.Speak("你好,我是機器人,有什麼想問的嗎?")

audio = r.listen(source)

with open("voices/myvoices.wav", "wb") as f:

f.write(audio.get_wav_data())

# 2、音頻檔案轉文字:采用百度的語音識别python-SDK

# 導入我們需要的子產品名,然後将音頻檔案發送給出去,傳回文字。

# 百度語音識别API配置參數

APP_ID = '17527531'

API_KEY = 'sGeHlRhneatUOLZAGpu0G3ef'

SECRET_KEY = 'jM4iRjwpKkgVqclQlTxnc26daZTdXist'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

path = 'voices/myvoices.wav'

# 将語音轉文本STT

def listen():

# 讀取錄音檔案

with open(path, 'rb') as fp:

voices = fp.read()

try:

# 參數dev_pid:1536國語(支援簡單的英文識别)、1537國語(純中文識别)、1737英語、1637粵語、1837四川話、1936國語遠場

result = client.asr(voices, 'wav', 16000, {'dev_pid': 1537, })

# result = CLIENT.asr(get_file_content(path), 'wav', 16000, {'lan': 'zh', })

# print(result)

# print(result['result'][0])

# print(result)

result_text = result["result"][0]

print("you said: " + result_text)

speaker.Speak(result_text)

return result_text

except KeyError:

print("KeyError")

speaker.Speak("我沒有聽清楚,請再說一遍...")

# 3、與機器人對話:調用的是圖靈機器人

# 圖靈機器人的API_KEY、API_URL

turing_api_key = "8b559122eba24ee39a20ebb9acb875dc"

api_url = "http://openapi.tuling123.com/openapi/api/v2"  # 圖靈機器人api網址

headers = {'Content-Type': 'application/json;charset=UTF-8'}

# 圖靈機器人回複

def Turing(text_words=""):

req = {

"reqType": 0,

"perception": {

"inputText": {

"text": text_words

},

"selfInfo": {

"location": {

"city": "北京",

"province": "北京",

"street": "北太平莊"

}

}

},

"userInfo": {

"apiKey": turing_api_key,  # 你的圖靈機器人apiKey

"userId": "test"  # 使用者唯一辨別(随便填, 非密鑰)

}

}

req["perception"]["inputText"]["text"] = text_words

response = requests.request("post", api_url, json=req, headers=headers)

response_dict = json.loads(response.text)

result = response_dict["results"][0]["values"]["text"]

print("AI Robot said: " + result)

return result

# 語音合成,輸出機器人的回答

while True:

my_record()

request = listen()

response = Turing(request)

speaker.Speak(response)