python从云端数据库获取数据失败_我这个python调用百度智能云却在返回的字典里没有result?...

该博客展示了一个使用Python调用百度语音API进行语音识别,并通过图灵机器人API处理用户输入,实现人机交互的示例。代码首先录制用户的声音,然后将录音转换为文字,接着将文字传递给图灵机器人获取回复,最后将机器人的回答转化为语音并播放。
摘要由CSDN通过智能技术生成

这是源代码:importtimeimportosimportpygameimporturllib.requestimportjsonfromaipimportAipSpeechimportspeech_recognitionassrimporturllib3urllib3.disable_warnings(urllib3.ex...

这是源代码:

import time

import os

import pygame

import urllib.request

import json

from aip import AipSpeech

import speech_recognition as sr

import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)# 忽略百度api连接时的报错信息。

# Baidu Speech API

APP_ID = '18840452'

API_KEY = 'NqzkRqRXq6rxPCsxZ3mAmMbc'

SECRET_KEY = 'zGDLp6RBGjzgqbiLRAU0Bpc1MGq0qiPK'

client = AipSpeech(APP_ID,API_KEY,SECRET_KEY)

#Turing API

TURING_KEY = 'a0b2fbdc09f64c96a8512cbdd05aa232'

API_URL = "http://openapi.tuling123.com/openapi/api/v2"

# 录音

def rec(rate=16000):

r = sr.Recognizer()

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

print("please say something")

audio = r.listen(source)

with open("recording.wav", "wb") as f:

f.write(audio.get_wav_data())

# 百度语音转文字

def listen():

with open('recording.wav', 'rb') as f:

audio_data = f.read()

result = client.asr(audio_data, 'wav', 16000, {

'dev_pid': 1536,

})

text_input = result["result"][0]

print("我说: " + text_input)

Robot_think(text_input)

# 图灵处理

def Robot_think(text_input):

req = {

"perception":

{

"inputText":

{

"text": text_input

},

"selfInfo":

{

"location":

{

"city": "东营",

"province": "东营",

"street": "黄河路"

}

}

},

"userInfo":

{

"apiKey": TURING_KEY,

"userId": "talker"

}

}

# print(req)

# 将字典格式的req编码为utf8

req = json.dumps(req).encode('utf8')

# print(req)

http_post = urllib.request.Request(API_URL, data=req, headers={'content-type': 'application/json'})

response = urllib.request.urlopen(http_post)

response_str = response.read().decode('utf8')

# print(response_str)

response_dic = json.loads(response_str)

# print(response_dic)

intent_code = response_dic['intent']['code']

results_text = response_dic['results'][0]['values']['text']

print("AI说: " + results_text)

du_say(results_text)

play_mp3('robot.mp3')

# 文字转语音

def du_say(results_text):

# per 3是汉子 4是妹子,spd 是语速,vol 是音量

result = client.synthesis(results_text, 'zh', 1, {

'vol': 5, 'per': 4, 'spd': 4

})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码

if not isinstance(result, dict):

with open('robot.mp3', 'wb') as f:

f.write(result)

# 播放Mp3文件

def play_mp3(file):

pygame.mixer.init()

pygame.mixer.music.load(file)

pygame.mixer.music.play()

while pygame.mixer.music.get_busy():

time.sleep(1)

pygame.mixer.music.stop()

pygame.mixer.quit()

if __name__ == '__main__':

while True:

rec()

listen()

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值