mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
update:python对接声纹识别优化
This commit is contained in:
@@ -23,6 +23,7 @@ from core.utils.modules_initialize import (
|
||||
initialize_asr,
|
||||
)
|
||||
from core.handle.reportHandle import report
|
||||
from core.utils.modules_initialize import initialize_voiceprint
|
||||
from core.providers.tts.default import DefaultTTS
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from core.utils.dialogue import Message, Dialogue
|
||||
@@ -412,6 +413,16 @@ class ConnectionHandler:
|
||||
# 因为远程ASR,涉及到websocket连接和接收线程,需要每个连接一个实例
|
||||
asr = initialize_asr(self.config)
|
||||
|
||||
# 动态初始化声纹识别功能
|
||||
try:
|
||||
success = initialize_voiceprint(asr, self.config)
|
||||
if success:
|
||||
self.logger.bind(tag=TAG).info("声纹识别功能已在连接时动态启用")
|
||||
else:
|
||||
self.logger.bind(tag=TAG).info("声纹识别功能未启用或配置不完整")
|
||||
except Exception as e:
|
||||
self.logger.bind(tag=TAG).error(f"动态初始化声纹识别时发生错误: {str(e)}")
|
||||
|
||||
return asr
|
||||
|
||||
def _initialize_private_config(self):
|
||||
|
||||
@@ -125,13 +125,27 @@ def initialize_asr(config):
|
||||
config["ASR"][select_asr_module],
|
||||
str(config.get("delete_audio", True)).lower() in ("true", "1", "yes"),
|
||||
)
|
||||
|
||||
# 初始化声纹识别功能
|
||||
voiceprint_config = config.get("voiceprint")
|
||||
if voiceprint_config and voiceprint_config.get("url") and voiceprint_config.get("speakers"):
|
||||
new_asr.init_voiceprint(voiceprint_config)
|
||||
logger.bind(tag=TAG).info("ASR模块声纹识别功能已启用")
|
||||
else:
|
||||
logger.bind(tag=TAG).info("ASR模块声纹识别功能已禁用")
|
||||
|
||||
logger.bind(tag=TAG).info("ASR模块初始化完成")
|
||||
return new_asr
|
||||
|
||||
|
||||
def initialize_voiceprint(asr_instance, config):
|
||||
"""初始化声纹识别功能"""
|
||||
voiceprint_config = config.get("voiceprint")
|
||||
if not voiceprint_config:
|
||||
return False
|
||||
|
||||
# 应用配置
|
||||
if not voiceprint_config.get("url") or not voiceprint_config.get("speakers"):
|
||||
logger.bind(tag=TAG).warning("声纹识别配置不完整")
|
||||
return False
|
||||
|
||||
try:
|
||||
asr_instance.init_voiceprint(voiceprint_config)
|
||||
logger.bind(tag=TAG).info("ASR模块声纹识别功能已动态启用")
|
||||
logger.bind(tag=TAG).info(f"配置说话人数量: {len(voiceprint_config['speakers'])}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.bind(tag=TAG).error(f"动态初始化声纹识别功能失败: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
@@ -118,7 +118,8 @@ class VoiceprintProvider:
|
||||
result_name = self.speaker_map[speaker_id]["name"]
|
||||
return result_name
|
||||
else:
|
||||
return None
|
||||
logger.bind(tag=TAG).warning(f"未识别的说话人ID: {speaker_id}")
|
||||
return "未知说话人"
|
||||
else:
|
||||
logger.bind(tag=TAG).error(f"声纹识别API错误: HTTP {response.status}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user