Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

想开发一个语音问答机器人 #107

Open
jinwei186 opened this issue Apr 23, 2024 · 2 comments
Open

想开发一个语音问答机器人 #107

jinwei186 opened this issue Apr 23, 2024 · 2 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@jinwei186
Copy link

jinwei186 commented Apr 23, 2024

描述

想开发一个语音问答机器人,能够实现人说话后,机器人灵活自由地用语音问答的功能,看了一些思路,用百度语音将人说的话转成文字,再用Chatopera进行文字回答,再将文字转成语音输出,编了一个代码如下,帮忙给诊断和指导,能够实现这个功能,谢谢!

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
from aip import AipSpeech
import requests
import json
import speech_recognition as sr
import win32com.client
from chatopera import Chatbot
import logging

# 配置日志
logging.basicConfig(level=logging.INFO)

# 确保程序运行在正确的目录下
curdir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(curdir, "path_to_chatopera_sdk"))  # 修改为 chatopera SDK 的路径

# 初始化语音合成
speaker = win32com.client.Dispatch("SAPI.SpVoice")

# 百度语音识别API配置参数
APP_ID = '你的百度云APP_ID'
API_KEY = '你的百度云API_KEY'
SECRET_KEY = '你的百度云SECRET_KEY'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 定义语音录制函数
def my_record(rate=16000):
    r = sr.Recognizer()
    with sr.Microphone(sample_rate=rate) as source:
        print("please say something")
        audio = r.listen(source, timeout=5)  # 添加超时
        print("Finished recording")  # 确认录音结束
    with open("voices/myvoices.wav", "wb") as f:
        f.write(audio.get_wav_data())

# 定义语音转文字函数
def listen():
    path = 'voices/myvoices.wav'
    with open(path, 'rb') as fp:
        voices = fp.read()
    print("Starting speech recognition...")  # 开始语音识别
    try:
        result = client.asr(voices, 'wav', 16000, {'dev_pid': 1537, })
        result_text = result["result"][0]
        print("Speech recognized: " + result_text)  # 打印识别结果
        return result_text
    except KeyError:
        print("Could not understand the speech.")
        speaker.Speak("我没有听清楚,请再说一遍...")
        return None

# 定义与Chatopera机器人对话的函数
def chat_with_bot(text_words):
    if not text_words:
        return "Nothing to chat about."
    BOT_APP_ID = "你的BOT_APP_ID"
    BOT_APP_SECRET = "你的BOT_APP_SECRET"
    BOT_PROVIDER = "https://bot.chatopera.com"

    bot = Chatbot(BOT_APP_ID, BOT_APP_SECRET, BOT_PROVIDER)
    try:
        resp = bot.command("POST", "/conversation/query", {
            "fromUserId": "py001",
            "textMessage": text_words
        })
        logging.info(resp["data"]["string"])
        return resp["data"]["string"]
    except Exception as 

操作系统

  • Windows10
@hailiang-wang
Copy link
Member

这个代码运行结果怎么样?

@hailiang-wang hailiang-wang added the help wanted Extra attention is needed label Apr 23, 2024
@jinwei186
Copy link
Author

这个代码运行结果怎么样?
运行后出现please say something,然后我就大声说“你好”好几次,程序没反应了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants