import speech_recognition as sr
from gtts import gTTS
import os
import requests
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException
from aliyunsdkchatbot.request.v20171011 import ChatRequest
def recognize_speech(audio_file):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = recognizer.listen(source)
try:
print("识别中...")
text = recognizer.recognize_google(audio, language='zh-CN')
print("你说的是:", text)
return text
except sr.UnknownValueError:
print("无法识别,请重试!")
return ""
except sr.RequestError as e:
print("请求出错:", e)
return ""
def text_to_speech(text, audio_file):
tts = gTTS(text=text, lang='zh')
tts.save(audio_file)
os.system("mpg321 " + audio_file)
def chat_with_bot(text):
access_key_id = "your_access_key_id"
access_key_secret = "your_access_key_secret"
endpoint = "your_chatbot_endpoint"
client = AcsClient(access_key_id, access_key_secret, 'cn-shanghai')
request = ChatRequest.ChatRequest()
request.set_accept_format('json')
request.set_BotId("your_bot_id")
request.set_InstanceId("your_instance_id")
request.set_Utterance(text)
try:
response = client.do_action_with_exception(request)
response_data = json.loads(response)
return response_data['Messages'][0]['Content']
except ServerException as e:
print("服务器异常:", e)
return ""
except ClientException as e:
print("客户端异常:", e)
return ""
if __name__ == "__main__":
while True:
input("按 Enter 键开始录音...")
audio_file = "record.wav"
os.system("arecord -D hw:1,0 -f cd -r 16000 -d 5 " + audio_file)
text = recognize_speech(audio_file)
if text:
response_text = chat_with_bot(text)
response_audio_file = "response_audio.wav"
text_to_speech(response_text, response_audio_file)
print("回复:", response_text)
#这个代码实现了一个完整的语音交互系统。
#用户按下Enter键开始录音,录音5秒后会自动停止。
#然后,系统会将录音文件转换为文本,并使用Chatbot服务获取智能回复。
#最后,系统将智能回复文本转换为语音,并播放给用户。
#请确保替换代码中的相应信息,比如阿里云的AccessKey、Chatbot的Endpoint以及录音设备的参数。
阿里云语音交互
最新推荐文章于 2024-08-16 15:59:39 发布