Signed-off-by: sairate <sairate@sina.cn>
This commit is contained in:
parent
e238c918c5
commit
1b960ca9e0
BIN
conversation.db
BIN
conversation.db
Binary file not shown.
96
main.py
96
main.py
|
@ -180,42 +180,74 @@ def play_mp3(mp3_file):
|
|||
while pygame.mixer.music.get_busy(): # 使用一个循环来等待音频播放完毕,保证程序不会在播放结束前退出
|
||||
clock.tick(3)
|
||||
|
||||
import cv2
|
||||
import socket
|
||||
import threading
|
||||
import queue
|
||||
import time
|
||||
|
||||
def main():
|
||||
create_connection()
|
||||
def detect_face(face_queue):
|
||||
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
||||
video_capture = cv2.VideoCapture(0) # 打开摄像头 # 用于跟踪是否已经检测到人脸
|
||||
face_detected = False
|
||||
while True:
|
||||
# 1. 提示用户发言
|
||||
print('请发言,谢谢!')
|
||||
# 2. 录制音频
|
||||
audio_record(5, 'user_audio.wav')
|
||||
|
||||
print('结束发言')
|
||||
# 3. 获取百度语音识别的access token
|
||||
baidu_token = get_access_token()
|
||||
print('Baidu access token obtained.')
|
||||
|
||||
# 4. 上传录音文件并进行语音识别
|
||||
baidu_result = BaiduYuYin('./user_audio.wav', baidu_token)
|
||||
print('Baidu speech recognition result:', baidu_result)
|
||||
|
||||
# 5. 调用大语言模型进行文本生成
|
||||
model_response = get_completion(baidu_result)
|
||||
print('Model response:', model_response)
|
||||
|
||||
# 6. 将文本转换为语音,保存到唯一的文件名
|
||||
audio_filename="audio/"+str(uuid.uuid4()) + '.mp3'
|
||||
unique_audio_filename = "static/"+audio_filename# 保存为不同的文件名以避免访问冲突
|
||||
asyncio.run(generate_audio_from_text(model_response, unique_audio_filename))
|
||||
|
||||
insert_data(baidu_result, model_response,audio_filename)# 插入数据库
|
||||
# 7. 播放生成的语音
|
||||
play_mp3(unique_audio_filename)
|
||||
|
||||
# 8. 提示用户继续对话或退出
|
||||
user_input = input('继续对话或输入"退出"退出: ')
|
||||
if user_input == '退出':
|
||||
ret, frame = video_capture.read()
|
||||
if not ret:
|
||||
print("无法读取摄像头画面")
|
||||
break
|
||||
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
|
||||
|
||||
# 在图像中绘制检测到的人脸
|
||||
for (x, y, w, h) in faces:
|
||||
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
||||
|
||||
# 显示视频流
|
||||
cv2.imshow('Video', frame)
|
||||
|
||||
if len(faces) > 0 and not face_detected: # 检测到人脸且之前未检测到
|
||||
face_queue.put(True) # 将检测结果放入队列
|
||||
face_detected = True # 设置为已检测到人脸
|
||||
|
||||
if len(faces) == 0: # 如果没有检测到人脸,重置状态
|
||||
face_detected = False
|
||||
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'): # 按 'q' 键退出
|
||||
break
|
||||
|
||||
video_capture.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
def main():
|
||||
face_queue = queue.Queue()
|
||||
detection_thread = threading.Thread(target=detect_face, args=(face_queue,))
|
||||
detection_thread.start()
|
||||
|
||||
while True:
|
||||
if not face_queue.empty(): # 检测到人脸
|
||||
face_result = face_queue.get()
|
||||
if face_result:
|
||||
print("识别到人脸,开始录音...")
|
||||
audio_record(5, 'user_audio.wav')
|
||||
# 获取百度Token
|
||||
baidu_token = get_access_token()
|
||||
baidu_result = BaiduYuYin('./user_audio.wav', baidu_token)
|
||||
print("语音识别结果:",baidu_result)
|
||||
model_response = get_completion(baidu_result)
|
||||
print("模型回复:",model_response)
|
||||
audio_filename = f"audio/{uuid.uuid4()}.mp3"
|
||||
unique_audio_filename = f"static/{audio_filename}"
|
||||
print("开始生成音频文件:", unique_audio_filename)
|
||||
asyncio.run(generate_audio_from_text(model_response, unique_audio_filename))
|
||||
print("数据库信息",baidu_result, model_response, unique_audio_filename)
|
||||
insert_data(baidu_result, model_response, audio_filename)
|
||||
play_mp3(unique_audio_filename)
|
||||
face_queue.put(False)
|
||||
if input("请输入(退出)")=="退出":
|
||||
break
|
||||
|
||||
detection_thread.join() # 等待人脸检测线程结束
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -10,7 +10,7 @@
|
|||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background-color: #f0f0f0;
|
||||
background-color: #cbe5d6;
|
||||
}
|
||||
h1 {
|
||||
text-align: center;
|
||||
|
@ -24,11 +24,11 @@
|
|||
th, td {
|
||||
padding: 12px;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid #ddd;
|
||||
border-bottom: 1px solid #9714b9;
|
||||
}
|
||||
th {
|
||||
background-color: #5e7abf;
|
||||
color: white;
|
||||
color: #278080;
|
||||
}
|
||||
tr:nth-child(even) {
|
||||
background-color: #8568d7;
|
||||
|
|
30
test.py
30
test.py
|
@ -7,5 +7,33 @@ def get_completion(prompt, model="solar"):
|
|||
'content': prompt,
|
||||
},
|
||||
])
|
||||
print(response)
|
||||
print(response['message'])
|
||||
return response['message']['content']
|
||||
print(get_completion("你好,我是小明。"))
|
||||
print(get_completion("你好,我是小明。"))
|
||||
# import cv2
|
||||
|
||||
# def test_camera():
|
||||
# video_capture = cv2.VideoCapture(0) # 打开默认摄像头
|
||||
#
|
||||
# if not video_capture.isOpened():
|
||||
# print("无法打开摄像头")
|
||||
# return
|
||||
#
|
||||
# while True:
|
||||
# ret, frame = video_capture.read()
|
||||
# if not ret:
|
||||
# print("无法读取摄像头画面")
|
||||
# break
|
||||
#
|
||||
# cv2.imshow('Video', frame)
|
||||
#
|
||||
# if cv2.waitKey(1) & 0xFF == ord('q'): # 按 'q' 键退出
|
||||
# break
|
||||
#
|
||||
# video_capture.release()
|
||||
# cv2.destroyAllWindows()
|
||||
#
|
||||
# # 调用函数
|
||||
# test_camera()
|
||||
|
||||
|
|
BIN
user_audio.wav
BIN
user_audio.wav
Binary file not shown.
Loading…
Reference in New Issue