mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 09:33:55 +08:00
Merge pull request #2190 from xinnan-tech/py_fix_voiceprint
update:声纹识别添加相似度阈值检查机制
This commit is contained in:
+14
@@ -298,6 +298,20 @@ public class ConfigServiceImpl implements ConfigService {
|
|||||||
Map<String, Object> voiceprintConfig = new HashMap<>();
|
Map<String, Object> voiceprintConfig = new HashMap<>();
|
||||||
voiceprintConfig.put("url", voiceprintUrl);
|
voiceprintConfig.put("url", voiceprintUrl);
|
||||||
voiceprintConfig.put("speakers", speakers);
|
voiceprintConfig.put("speakers", speakers);
|
||||||
|
|
||||||
|
// 获取声纹识别相似度阈值,默认0.4
|
||||||
|
String thresholdStr = sysParamsService.getValue("server.voiceprint_similarity_threshold", true);
|
||||||
|
if (StringUtils.isNotBlank(thresholdStr) && !"null".equals(thresholdStr)) {
|
||||||
|
try {
|
||||||
|
double threshold = Double.parseDouble(thresholdStr);
|
||||||
|
voiceprintConfig.put("similarity_threshold", threshold);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// 如果解析失败,使用默认值0.4
|
||||||
|
voiceprintConfig.put("similarity_threshold", 0.4);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
voiceprintConfig.put("similarity_threshold", 0.4);
|
||||||
|
}
|
||||||
|
|
||||||
result.put("voiceprint", voiceprintConfig);
|
result.put("voiceprint", voiceprintConfig);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
-- 添加声纹识别相似度阈值参数配置
|
||||||
|
delete from `sys_params` where id = 115;
|
||||||
|
INSERT INTO `sys_params` (id, param_code, param_value, value_type, param_type, remark)
|
||||||
|
VALUES (115, 'server.voiceprint_similarity_threshold', '0.4', 'string', 1, '声纹识别相似度阈值,范围0.0-1.0,默认0.4,数值越高越严格');
|
||||||
@@ -303,10 +303,17 @@ databaseChangeLog:
|
|||||||
- sqlFile:
|
- sqlFile:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202508131557.sql
|
path: classpath:db/changelog/202508131557.sql
|
||||||
|
- changeSet:
|
||||||
|
id: 202509081140
|
||||||
|
author: cgd
|
||||||
|
changes:
|
||||||
|
- sqlFile:
|
||||||
|
encoding: utf8
|
||||||
|
path: classpath:db/changelog/202509081140.sql
|
||||||
- changeSet:
|
- changeSet:
|
||||||
id: 202508271113
|
id: 202508271113
|
||||||
author: cgd
|
author: cgd
|
||||||
changes:
|
changes:
|
||||||
- sqlFile:
|
- sqlFile:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202508271113.sql
|
path: classpath:db/changelog/202508271113.sql
|
||||||
@@ -151,6 +151,9 @@ voiceprint:
|
|||||||
- "test1,张三,张三是一个程序员"
|
- "test1,张三,张三是一个程序员"
|
||||||
- "test2,李四,李四是一个产品经理"
|
- "test2,李四,李四是一个产品经理"
|
||||||
- "test3,王五,王五是一个设计师"
|
- "test3,王五,王五是一个设计师"
|
||||||
|
# 声纹识别相似度阈值,范围0.0-1.0,默认0.4
|
||||||
|
# 数值越高越严格,减少误识别但可能增加拒识率
|
||||||
|
similarity_threshold: 0.4
|
||||||
|
|
||||||
# #####################################################################################
|
# #####################################################################################
|
||||||
# ################################以下是角色模型配置######################################
|
# ################################以下是角色模型配置######################################
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ class VoiceprintProvider:
|
|||||||
self.original_url = config.get("url", "")
|
self.original_url = config.get("url", "")
|
||||||
self.speakers = config.get("speakers", [])
|
self.speakers = config.get("speakers", [])
|
||||||
self.speaker_map = self._parse_speakers()
|
self.speaker_map = self._parse_speakers()
|
||||||
|
# 声纹识别相似度阈值,默认0.4
|
||||||
|
self.similarity_threshold = float(config.get("similarity_threshold", 0.4))
|
||||||
|
|
||||||
# 解析API地址和密钥
|
# 解析API地址和密钥
|
||||||
self.api_url = None
|
self.api_url = None
|
||||||
@@ -62,7 +64,7 @@ class VoiceprintProvider:
|
|||||||
# 进行健康检查,验证服务器是否可用
|
# 进行健康检查,验证服务器是否可用
|
||||||
if self._check_server_health():
|
if self._check_server_health():
|
||||||
self.enabled = True
|
self.enabled = True
|
||||||
logger.bind(tag=TAG).info(f"声纹识别已启用: API={self.api_url}, 说话人={len(self.speaker_ids)}个")
|
logger.bind(tag=TAG).info(f"声纹识别已启用: API={self.api_url}, 说话人={len(self.speaker_ids)}个, 相似度阈值={self.similarity_threshold}")
|
||||||
else:
|
else:
|
||||||
self.enabled = False
|
self.enabled = False
|
||||||
logger.bind(tag=TAG).warning(f"声纹识别服务器不可用,声纹识别已禁用: {self.api_url}")
|
logger.bind(tag=TAG).warning(f"声纹识别服务器不可用,声纹识别已禁用: {self.api_url}")
|
||||||
@@ -169,12 +171,14 @@ class VoiceprintProvider:
|
|||||||
|
|
||||||
logger.bind(tag=TAG).info(f"声纹识别耗时: {total_elapsed_time:.3f}s")
|
logger.bind(tag=TAG).info(f"声纹识别耗时: {total_elapsed_time:.3f}s")
|
||||||
|
|
||||||
# 置信度检查
|
# 相似度阈值检查
|
||||||
if score < 0.5:
|
if score < self.similarity_threshold:
|
||||||
logger.bind(tag=TAG).warning(f"声纹识别置信度较低: {score:.3f}")
|
logger.bind(tag=TAG).warning(f"声纹识别相似度{score:.3f}低于阈值{self.similarity_threshold}")
|
||||||
|
return "未知说话人"
|
||||||
|
|
||||||
if speaker_id and speaker_id in self.speaker_map:
|
if speaker_id and speaker_id in self.speaker_map:
|
||||||
result_name = self.speaker_map[speaker_id]["name"]
|
result_name = self.speaker_map[speaker_id]["name"]
|
||||||
|
logger.bind(tag=TAG).info(f"声纹识别成功: {result_name} (相似度: {score:.3f})")
|
||||||
return result_name
|
return result_name
|
||||||
else:
|
else:
|
||||||
logger.bind(tag=TAG).warning(f"未识别的说话人ID: {speaker_id}")
|
logger.bind(tag=TAG).warning(f"未识别的说话人ID: {speaker_id}")
|
||||||
|
|||||||
Reference in New Issue
Block a user