update:修复二次声纹注册bug

This commit is contained in:
hrz
2025-07-11 11:49:53 +08:00
parent 8f6dd22a6d
commit 8f685967d3
3 changed files with 77 additions and 63 deletions
@@ -1,5 +1,7 @@
package xiaozhi.modules.agent.dto; package xiaozhi.modules.agent.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.Data;
/** /**
@@ -10,6 +12,7 @@ public class IdentifyVoicePrintResponse {
/** /**
* 最匹配的声纹id * 最匹配的声纹id
*/ */
@JsonProperty("speaker_id")
private String speakerId; private String speakerId;
/** /**
* 声纹的分数 * 声纹的分数
@@ -1,17 +1,29 @@
package xiaozhi.modules.agent.service.impl; package xiaozhi.modules.agent.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import java.net.URI;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import java.net.URISyntaxException;
import lombok.AllArgsConstructor; import java.util.List;
import lombok.extern.slf4j.Slf4j; import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.*; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import xiaozhi.common.constant.Constant; import xiaozhi.common.constant.Constant;
import xiaozhi.common.exception.RenException; import xiaozhi.common.exception.RenException;
import xiaozhi.common.utils.ConvertUtils; import xiaozhi.common.utils.ConvertUtils;
@@ -27,12 +39,6 @@ import xiaozhi.modules.agent.service.AgentVoicePrintService;
import xiaozhi.modules.agent.vo.AgentVoicePrintVO; import xiaozhi.modules.agent.vo.AgentVoicePrintVO;
import xiaozhi.modules.sys.service.SysParamsService; import xiaozhi.modules.sys.service.SysParamsService;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* @author zjy * @author zjy
*/ */
@@ -48,8 +54,7 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
// Springboot提供的编程事务类 // Springboot提供的编程事务类
private final TransactionTemplate transactionTemplate; private final TransactionTemplate transactionTemplate;
// 识别度 // 识别度
private final Double RECOGNITION = 0.3; private final Double RECOGNITION = 0.5;
@Override @Override
public boolean insert(AgentVoicePrintSaveDTO dto) { public boolean insert(AgentVoicePrintSaveDTO dto) {
@@ -58,7 +63,10 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
// 识别一下此声音是否注册过 // 识别一下此声音是否注册过
IdentifyVoicePrintResponse response = identifyVoicePrint(dto.getAgentId(), resource); IdentifyVoicePrintResponse response = identifyVoicePrint(dto.getAgentId(), resource);
if (response != null && response.getScore() > RECOGNITION) { if (response != null && response.getScore() > RECOGNITION) {
throw new RenException("此声音已经注册为声纹过了"); // 根据识别出的声纹ID查询对应的用户信息
AgentVoicePrintEntity existingVoicePrint = baseMapper.selectById(response.getSpeakerId());
String existingUserName = existingVoicePrint != null ? existingVoicePrint.getSourceName() : "未知用户";
throw new RenException("此声音声纹对应的人(" + existingUserName + ")已经注册,请选择其他声音注册");
} }
AgentVoicePrintEntity entity = ConvertUtils.sourceToTarget(dto, AgentVoicePrintEntity.class); AgentVoicePrintEntity entity = ConvertUtils.sourceToTarget(dto, AgentVoicePrintEntity.class);
// 开启事务 // 开启事务
@@ -111,8 +119,6 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
})); }));
} }
@Override @Override
public List<AgentVoicePrintVO> list(Long userId, String agentId) { public List<AgentVoicePrintVO> list(Long userId, String agentId) {
// 按照指定当前登录用户和智能体查找数据 // 按照指定当前登录用户和智能体查找数据
@@ -128,8 +134,8 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
@Override @Override
public boolean update(Long userId, AgentVoicePrintUpdateDTO dto) { public boolean update(Long userId, AgentVoicePrintUpdateDTO dto) {
AgentVoicePrintEntity agentVoicePrintEntity = AgentVoicePrintEntity agentVoicePrintEntity = baseMapper
baseMapper.selectOne(new LambdaQueryWrapper<AgentVoicePrintEntity>() .selectOne(new LambdaQueryWrapper<AgentVoicePrintEntity>()
.eq(AgentVoicePrintEntity::getId, dto.getId()) .eq(AgentVoicePrintEntity::getId, dto.getId())
.eq(AgentVoicePrintEntity::getCreator, userId)); .eq(AgentVoicePrintEntity::getCreator, userId));
if (agentVoicePrintEntity == null) { if (agentVoicePrintEntity == null) {
@@ -150,7 +156,10 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
if (response != null && response.getScore() > RECOGNITION) { if (response != null && response.getScore() > RECOGNITION) {
// 判断返回的id如果不是要修改的声纹id,说明这个声纹id,现在要注册的声音已经存在且不是原来的声纹,不允许修改 // 判断返回的id如果不是要修改的声纹id,说明这个声纹id,现在要注册的声音已经存在且不是原来的声纹,不允许修改
if (!response.getSpeakerId().equals(dto.getId())) { if (!response.getSpeakerId().equals(dto.getId())) {
throw new RenException("此次修改不允许,此声音已经注册为声纹了"); // 根据识别出的声纹ID查询对应的用户信息
AgentVoicePrintEntity existingVoicePrint = baseMapper.selectById(response.getSpeakerId());
String existingUserName = existingVoicePrint != null ? existingVoicePrint.getSourceName() : "未知用户";
throw new RenException("此次修改不允许,此声音已经注册为声纹了(" + existingUserName + "");
} }
} }
} else { } else {
@@ -184,8 +193,6 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
})); }));
} }
/** /**
* 获取生纹接口URI对象 * 获取生纹接口URI对象
* *
@@ -204,6 +211,7 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
/** /**
* 获取声纹地址基础路径 * 获取声纹地址基础路径
*
* @param uri 声纹地址uri * @param uri 声纹地址uri
* @return 基础路径 * @return 基础路径
*/ */
@@ -257,6 +265,7 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
/** /**
* 发送注册声纹http请求 * 发送注册声纹http请求
*
* @param id 声纹id * @param id 声纹id
* @param resource 声纹音频资源 * @param resource 声纹音频资源
*/ */
@@ -293,6 +302,7 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
/** /**
* 发送注销声纹的请求 * 发送注销声纹的请求
*
* @param voicePrintId 声纹id * @param voicePrintId 声纹id
*/ */
private void cancelVoicePrint(String voicePrintId) { private void cancelVoicePrint(String voicePrintId) {
@@ -306,7 +316,8 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(headers); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(headers);
// 发送 POST 请求 // 发送 POST 请求
ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, requestEntity, String.class); ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, requestEntity,
String.class);
if (response.getStatusCode() != HttpStatus.OK) { if (response.getStatusCode() != HttpStatus.OK) {
log.error("声纹注销失败,请求路径:{}", requestUrl); log.error("声纹注销失败,请求路径:{}", requestUrl);
throw new RenException("声纹注销失败,请求不成功"); throw new RenException("声纹注销失败,请求不成功");
@@ -319,9 +330,9 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
} }
} }
/** /**
* 发送识别声纹http请求 * 发送识别声纹http请求
*
* @param agentId 智能体id * @param agentId 智能体id
* @param resource 声纹音频资源 * @param resource 声纹音频资源
* @return 返回识别数据 * @return 返回识别数据
@@ -329,7 +340,8 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
private IdentifyVoicePrintResponse identifyVoicePrint(String agentId, ByteArrayResource resource) { private IdentifyVoicePrintResponse identifyVoicePrint(String agentId, ByteArrayResource resource) {
// 获取该智能体所有注册的声纹 // 获取该智能体所有注册的声纹
List<AgentVoicePrintEntity> agentVoicePrintList = baseMapper.selectList(new LambdaQueryWrapper<AgentVoicePrintEntity>() List<AgentVoicePrintEntity> agentVoicePrintList = baseMapper
.selectList(new LambdaQueryWrapper<AgentVoicePrintEntity>()
.select(AgentVoicePrintEntity::getId) .select(AgentVoicePrintEntity::getId)
.eq(AgentVoicePrintEntity::getAgentId, agentId)); .eq(AgentVoicePrintEntity::getAgentId, agentId));
@@ -344,7 +356,6 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
// 创建请求体 // 创建请求体
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
// 创建speaker_id参数 // 创建speaker_id参数
String speakerIds = agentVoicePrintList.stream() String speakerIds = agentVoicePrintList.stream()
.map(AgentVoicePrintEntity::getId) .map(AgentVoicePrintEntity::getId)
@@ -29,8 +29,8 @@
</el-form-item> </el-form-item>
<el-form-item label="描述" prop="introduce" class="form-item remark-item"> <el-form-item label="描述" prop="introduce" class="form-item remark-item">
<el-input type="textarea" v-model="form.introduce" placeholder="请输入描述" :rows="3" <el-input type="textarea" v-model="form.introduce" placeholder="请输入描述" :rows="3" class="custom-textarea"
class="custom-textarea"></el-input> maxlength="100" show-word-limit></el-input>
</el-form-item> </el-form-item>
</el-form> </el-form>