From ba1e057bce5e2b107c5bae0e882f213ad65990c7 Mon Sep 17 00:00:00 2001 From: thesai <1021828630@qq.com> Date: Tue, 14 May 2024 15:14:16 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=96=B0=E5=A2=9E]=E8=AF=AD=E9=9F=B3=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E5=B9=B6=E4=B8=8E=E6=AD=A3=E7=A1=AE=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=A0=A1=E9=AA=8C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/voice/VoiceController.java | 67 +++++++++-------- .../voice/VoiceManageController.java | 62 ++++++++-------- .../services/voice/IVoiceTrainingService.java | 16 ++++- .../services/voice/VoiceTrainingService.java | 71 ++++++++++--------- .../rtss/vo/voice/VoiceVerifyResultVO.java | 13 ++++ 5 files changed, 137 insertions(+), 92 deletions(-) create mode 100644 src/main/java/club/joylink/rtss/vo/voice/VoiceVerifyResultVO.java diff --git a/src/main/java/club/joylink/rtss/controller/voice/VoiceController.java b/src/main/java/club/joylink/rtss/controller/voice/VoiceController.java index adb0d3abd..b91276d67 100644 --- a/src/main/java/club/joylink/rtss/controller/voice/VoiceController.java +++ b/src/main/java/club/joylink/rtss/controller/voice/VoiceController.java @@ -4,8 +4,9 @@ import club.joylink.rtss.services.voice.IVoiceTrainingService; import club.joylink.rtss.services.voice.baidu.TokenHolder; import club.joylink.rtss.vo.client.PageQueryVO; import club.joylink.rtss.vo.client.PageVO; -import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; import club.joylink.rtss.vo.client.competition.VoiceErrorVO; +import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; +import club.joylink.rtss.vo.voice.VoiceVerifyResultVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -20,34 +21,44 @@ import org.springframework.web.multipart.MultipartFile; @RequestMapping("/api/voice") public class VoiceController { - @Autowired - private IVoiceTrainingService iVoiceTrainingService; + @Autowired + private IVoiceTrainingService iVoiceTrainingService; - /** - * 语音识别 - * @param file - * @return - */ - @PostMapping("recognition") - public VoiceRecognitionResult voiceRecognition(MultipartFile file) { - return iVoiceTrainingService.voiceRecognition(file); - } + /** + * 语音识别 + * + * @param file + * @return + */ + @PostMapping("recognition") + public VoiceRecognitionResult voiceRecognition(MultipartFile file) { + return iVoiceTrainingService.voiceRecognition(file); + } - /** - * 查询语音识别错误集 - * @param queryVO - * @return - */ - @GetMapping("/errorSet/paged") - public PageVO pagedQueryErrorSet(PageQueryVO queryVO) { - return iVoiceTrainingService.pagedQueryErrorSet(queryVO); - } + /** + * 查询语音识别错误集 + * + * @param queryVO + * @return + */ + @GetMapping("/errorSet/paged") + public PageVO pagedQueryErrorSet(PageQueryVO queryVO) { + return iVoiceTrainingService.pagedQueryErrorSet(queryVO); + } - /** - * 获取百度语音识别token - */ - @GetMapping("/token/baidu") - public String getBaiduToken() { - return TokenHolder.getInstance().getToken(); - } + /** + * 获取百度语音识别token + */ + @GetMapping("/token/baidu") + public String getBaiduToken() { + return TokenHolder.getInstance().getToken(); + } + + /** + * 校验语音识别结果 + */ + @PostMapping("/verify") + public VoiceVerifyResultVO verifyVoice(MultipartFile file, String text) { + return iVoiceTrainingService.verifyVoice(file, text); + } } diff --git a/src/main/java/club/joylink/rtss/controller/voice/VoiceManageController.java b/src/main/java/club/joylink/rtss/controller/voice/VoiceManageController.java index a2c91f675..ba8f4b2aa 100644 --- a/src/main/java/club/joylink/rtss/controller/voice/VoiceManageController.java +++ b/src/main/java/club/joylink/rtss/controller/voice/VoiceManageController.java @@ -2,51 +2,55 @@ package club.joylink.rtss.controller.voice; import club.joylink.rtss.controller.advice.AuthenticateInterceptor; import club.joylink.rtss.services.voice.IVoiceDataConfigService; -import club.joylink.rtss.services.voice.IVoiceTrainingService; -import club.joylink.rtss.services.voice.baidu.TokenHolder; import club.joylink.rtss.vo.LoginUserInfoVO; -import club.joylink.rtss.vo.client.PageQueryVO; import club.joylink.rtss.vo.client.PageVO; -import club.joylink.rtss.vo.client.competition.VoiceErrorVO; -import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; import club.joylink.rtss.vo.voice.VoiceDiscriminateConfigVO; import club.joylink.rtss.vo.voice.VoiceQueryVO; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestAttribute; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; /** - * 语音AI接口 + * 语音管理接口 */ @RestController @RequestMapping("/api/voice/manage") public class VoiceManageController { - @Autowired - private IVoiceDataConfigService configService; + @Autowired + private IVoiceDataConfigService configService; - @PostMapping("saveOrUpdate") - public void saveOrUpdate(@RequestBody VoiceDiscriminateConfigVO configVO, @RequestAttribute(name= AuthenticateInterceptor.LOGIN_INFO_KEY) LoginUserInfoVO userInfoVO) { - this.configService.saveOrUpdate(configVO,userInfoVO); - } + @PostMapping("saveOrUpdate") + public void saveOrUpdate(@RequestBody VoiceDiscriminateConfigVO configVO, + @RequestAttribute(name = AuthenticateInterceptor.LOGIN_INFO_KEY) LoginUserInfoVO userInfoVO) { + this.configService.saveOrUpdate(configVO, userInfoVO); + } - @GetMapping("find/{id}") - public VoiceDiscriminateConfigVO find(@PathVariable(value = "id") Long id) { - return this.configService.byId(id); - } + @GetMapping("find/{id}") + public VoiceDiscriminateConfigVO find(@PathVariable(value = "id") Long id) { + return this.configService.byId(id); + } - @GetMapping("query") - public PageVO query(@ModelAttribute VoiceQueryVO queryVO){ - return this.configService.query(queryVO); - } + @GetMapping("query") + public PageVO query(@ModelAttribute VoiceQueryVO queryVO) { + return this.configService.query(queryVO); + } - /** - * 获取百度语音识别token - */ - @GetMapping("{id}/{status}") - public void changeStatus(@PathVariable(value = "id") Long id,@PathVariable(value = "status")Integer status,@RequestAttribute(name= AuthenticateInterceptor.LOGIN_INFO_KEY) LoginUserInfoVO userInfoVO) { - this.configService.changeStatus(id,status,userInfoVO); - } + /** + * 获取百度语音识别token + */ + @GetMapping("{id}/{status}") + public void changeStatus(@PathVariable(value = "id") Long id, + @PathVariable(value = "status") Integer status, + @RequestAttribute(name = AuthenticateInterceptor.LOGIN_INFO_KEY) LoginUserInfoVO userInfoVO) { + this.configService.changeStatus(id, status, userInfoVO); + } } diff --git a/src/main/java/club/joylink/rtss/services/voice/IVoiceTrainingService.java b/src/main/java/club/joylink/rtss/services/voice/IVoiceTrainingService.java index 4d2704d4c..f99e6158a 100644 --- a/src/main/java/club/joylink/rtss/services/voice/IVoiceTrainingService.java +++ b/src/main/java/club/joylink/rtss/services/voice/IVoiceTrainingService.java @@ -4,6 +4,7 @@ import club.joylink.rtss.vo.client.PageQueryVO; import club.joylink.rtss.vo.client.PageVO; import club.joylink.rtss.vo.client.competition.VoiceErrorVO; import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; +import club.joylink.rtss.vo.voice.VoiceVerifyResultVO; import org.springframework.web.multipart.MultipartFile; /** @@ -11,9 +12,18 @@ import org.springframework.web.multipart.MultipartFile; */ public interface IVoiceTrainingService { - PageVO pagedQueryErrorSet(PageQueryVO queryVO); + PageVO pagedQueryErrorSet(PageQueryVO queryVO); - VoiceRecognitionResult voiceRecognition(MultipartFile file); + VoiceRecognitionResult voiceRecognition(MultipartFile file); - VoiceRecognitionResult voiceRecognition(byte[] bytes, String filePath); + VoiceRecognitionResult voiceRecognition(byte[] bytes, String filePath); + + /** + * 校验语音识别结果 + * + * @param file 需要校验的语音 + * @param text 正确的文本 + * @return 校验结果 + */ + VoiceVerifyResultVO verifyVoice(MultipartFile file, String text); } diff --git a/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java b/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java index 32584de72..45770181d 100644 --- a/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java +++ b/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java @@ -3,62 +3,69 @@ package club.joylink.rtss.services.voice; import club.joylink.rtss.dao.CompetitionErrorSetDAO; import club.joylink.rtss.entity.CompetitionErrorSet; import club.joylink.rtss.simulation.cbtc.conversation.SimulationVoiceHandler; +import club.joylink.rtss.util.StrUtils; import club.joylink.rtss.vo.client.PageQueryVO; import club.joylink.rtss.vo.client.PageVO; import club.joylink.rtss.vo.client.competition.VoiceErrorVO; import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; import club.joylink.rtss.vo.client.voice.VoiceRecognitionVO; +import club.joylink.rtss.vo.voice.VoiceVerifyResultVO; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - import java.util.List; import java.util.stream.Collectors; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; @Service public class VoiceTrainingService implements IVoiceTrainingService { - @Autowired - private CompetitionErrorSetDAO competitionErrorSetDAO; + @Autowired + private CompetitionErrorSetDAO competitionErrorSetDAO; /* @Autowired @Qualifier("baiDuVoiceService") private IVoiceService iVoiceService;*/ - @Autowired + @Autowired // @Qualifier("baiDuVoiceService2") - private VoiceService iVoiceService; + private VoiceService iVoiceService; - @Autowired - private SimulationVoiceHandler simulationVoiceHandler; + @Autowired + private SimulationVoiceHandler simulationVoiceHandler; - @Override - public PageVO pagedQueryErrorSet(PageQueryVO queryVO) { - Page page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize()); - List sets = competitionErrorSetDAO.selectByExample(null); - List list = sets.stream().map(VoiceErrorVO::new).collect(Collectors.toList()); - return PageVO.convert(page, list); - } + @Override + public PageVO pagedQueryErrorSet(PageQueryVO queryVO) { + Page page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize()); + List sets = competitionErrorSetDAO.selectByExample(null); + List list = sets.stream().map(VoiceErrorVO::new).collect(Collectors.toList()); + return PageVO.convert(page, list); + } - @Override - public VoiceRecognitionResult voiceRecognition(MultipartFile file) { - VoiceRecognitionVO vo = VoiceRecognitionVO.load(file); - VoiceRecognitionResult result = this.iVoiceService.voiceRecognition(vo); + @Override + public VoiceRecognitionResult voiceRecognition(MultipartFile file) { + VoiceRecognitionVO vo = VoiceRecognitionVO.load(file); + VoiceRecognitionResult result = this.iVoiceService.voiceRecognition(vo); // VoiceRecognitionResult result = this.iVoiceService.voiceRecognition(file, ""); - result.setResult(simulationVoiceHandler.handle(result.getResult())); - return result; - } + result.setResult(simulationVoiceHandler.handle(result.getResult())); + return result; + } - @Override - public VoiceRecognitionResult voiceRecognition(byte[] bytes, String filePath) { - VoiceRecognitionVO vo = new VoiceRecognitionVO(bytes); - VoiceRecognitionResult result = this.iVoiceService.voiceRecognition(vo); + @Override + public VoiceRecognitionResult voiceRecognition(byte[] bytes, String filePath) { + VoiceRecognitionVO vo = new VoiceRecognitionVO(bytes); + VoiceRecognitionResult result = this.iVoiceService.voiceRecognition(vo); // VoiceRecognitionResult result = this.iVoiceService.voiceRecognition(bytes, filePath); - result.setResult(simulationVoiceHandler.handle(result.getResult())); - return result; - } + result.setResult(simulationVoiceHandler.handle(result.getResult())); + return result; + } + + @Override + public VoiceVerifyResultVO verifyVoice(MultipartFile file, String text) { + VoiceRecognitionResult voiceRecognitionResult = voiceRecognition(file); + boolean match = StrUtils.isMatch(text, voiceRecognitionResult.getResult(), 20); + return new VoiceVerifyResultVO(voiceRecognitionResult.getResult(), match); + } } diff --git a/src/main/java/club/joylink/rtss/vo/voice/VoiceVerifyResultVO.java b/src/main/java/club/joylink/rtss/vo/voice/VoiceVerifyResultVO.java new file mode 100644 index 000000000..57cacf763 --- /dev/null +++ b/src/main/java/club/joylink/rtss/vo/voice/VoiceVerifyResultVO.java @@ -0,0 +1,13 @@ +package club.joylink.rtss.vo.voice; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class VoiceVerifyResultVO { + + private String text; + + private boolean match; +}