鱼C论坛

 找回密码
 立即注册
查看: 1612|回复: 2

只能问答两次,第三次报错退出

[复制链接]
发表于 2019-11-10 14:54:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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)

# Baidu Speech API
APP_ID = '177'
API_KEY = 'Kj28juWvSWtjyi'
SECRET_KEY = 'egNGfhk9xIkI8Bmhx7PY8'
   
client = AipSpeech(APP_ID,API_KEY,SECRET_KEY)

#Turing API
TURING_KEY = "f2ea9524ce5f46599dcf5a"
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": "nihao"
    }
}
    # 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': 3, 'spd': 3
    })
    # 识别正确返回语音二进制 错误则返回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()

错误显示

错误显示
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-10 15:00:04 | 显示全部楼层
speech_recognition 是什么模块?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-10 16:41:38 | 显示全部楼层
zltzlt 发表于 2019-11-10 15:00
speech_recognition 是什么模块?

语音辨识,录音函数里的,我遇到的问题是问答第三次时,文字有答案,但是没有第三次的语音,语音文件是第二次的回答,把
    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        with open('robot.mp3', 'wb') as f:
            f.write(result)
这个代码注释掉,可以语音问问题,图灵文字回答也很好,但是语音播放的回答一直是第二次是的回答
不清楚是怎么回事,看别人的百度文字转语音都是这么写的啊,求大神解答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-20 15:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表