当前位置:   article > 正文

百度语音识别的springboot应用

百度语音识别的springboot应用

1、pom依赖

<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.16.18</version>
</dependency>

2、测试的demo

创建语音识别应用

百度智能云-管理中心 (baidu.com)

代码中要配置 

  1. package com.zbIntel.integration.utils;
  2. import com.baidu.aip.speech.AipSpeech;
  3. import com.baidu.aip.util.Util;
  4. import org.json.JSONObject;
  5. import java.io.IOException;
  6. public class Sample {
  7. //设置APPID/AK/SK
  8. public static final String APP_ID = "";
  9. public static final String API_KEY = "";
  10. public static final String SECRET_KEY = "";
  11. private static final String FILENAME = "D:\\project\\speech-demo-master\\rest-api-asr\\java\\16k.wav";
  12. public static void main(String[] args) throws IOException {
  13. // 初始化一个AipSpeech
  14. AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
  15. // 可选:设置网络连接参数
  16. client.setConnectionTimeoutInMillis(2000);
  17. client.setSocketTimeoutInMillis(60000);
  18. // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
  19. // client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
  20. // client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
  21. // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
  22. // 也可以直接通过jvm启动参数设置此环境变量
  23. System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
  24. // 调用接口
  25. JSONObject res = client.asr("D:\\project\\mygpt\\src\\main\\resources\\iat\\16k_10.pcm", "pcm", 16000, null);
  26. System.out.println(res.toString(2));
  27. // 对本地语音文件进行识别
  28. String path = "D:\\project\\mygpt\\src\\main\\resources\\iat\\16k_10.pcm";
  29. JSONObject asrRes = client.asr(FILENAME, "pcm", 16000, null);
  30. System.out.println(asrRes);
  31. // 对语音二进制数据进行识别
  32. byte[] data = Util.readFileByBytes(path); //readFileByBytes仅为获取二进制数据示例
  33. JSONObject asrRes2 = client.asr(data, "pcm", 16000, null);
  34. System.out.println(asrRes2);
  35. }
  36. }

3、创建Service 支持语音转文字

  1. package com.zbIntel.integration.yuyin;
  2. import com.baidu.aip.speech.AipSpeech;
  3. import org.json.JSONObject;
  4. import org.springframework.stereotype.Service;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. @Service
  8. public class BaiduSpeechService {
  9. public static final String APP_ID = "";
  10. public static final String API_KEY = "";
  11. public static final String SECRET_KEY = "";
  12. private AipSpeech client;
  13. public BaiduSpeechService() {
  14. // 设置APPID/API Key/Secret Key
  15. client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
  16. }
  17. public Map<String, Object> recognize(String filePath) {
  18. // 调用百度语音识别接口
  19. client.setConnectionTimeoutInMillis(2000);
  20. client.setSocketTimeoutInMillis(60000);
  21. HashMap<String, Object> options = new HashMap<>();
  22. options.put("dev_pid", 1537);
  23. JSONObject res = client.asr(filePath, "wav", 16000, options);
  24. Map<String, Object> resultMap = new HashMap<>();
  25. if (res.get("result") != null) {
  26. resultMap.put("result", res.get("result"));
  27. return resultMap;
  28. }
  29. return resultMap;
  30. }
  31. }

3、创建controller 支持上传音频文件

  1. package com.zbIntel.integration.controller;
  2. import com.zbIntel.integration.utils.ReturnResult;
  3. import com.zbIntel.integration.yuyin.BaiduSpeechService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.nio.file.Files;
  13. import java.nio.file.Paths;
  14. import java.util.Map;
  15. @RestController
  16. public class SpeechController {
  17. @Autowired
  18. private BaiduSpeechService speechService;
  19. private static final String UPLOAD_DIR = "/tmp/upload/directory"; // 指定上传文件的保存目录
  20. @PostMapping("/speech/recognize")
  21. public ReturnResult recognizeSpeech(@RequestParam("file") MultipartFile file) throws IOException {
  22. // 将上传的文件保存到服务器,调用语音识别服务
  23. String filePath = saveFile(file);
  24. try {
  25. Map<String, Object> text = speechService.recognize(filePath);
  26. return ReturnResult.ok().data(text);
  27. } finally {
  28. // 确保即使识别过程中出现异常,文件也能被删除
  29. deleteFile(filePath);
  30. }
  31. }
  32. private String saveFile(MultipartFile file) throws IOException {
  33. String fileName = file.getOriginalFilename();
  34. String filePath = UPLOAD_DIR + File.separator + fileName; // 构建完整的文件路径
  35. File dest = new File(filePath);
  36. // 确保目录存在
  37. if (!dest.getParentFile().exists()) {
  38. dest.getParentFile().mkdirs();
  39. }
  40. try (FileOutputStream outputStream = new FileOutputStream(dest)) {
  41. outputStream.write(file.getBytes());
  42. }
  43. return filePath;
  44. }
  45. private void deleteFile(String filePath) {
  46. try {
  47. Files.delete(Paths.get(filePath));
  48. } catch (IOException e) {
  49. // 记录日志或者处理删除失败的情况
  50. System.err.println("Failed to delete file: " + filePath);
  51. e.printStackTrace();
  52. }
  53. }
  54. }

4、创建页面 支持 录音 上报 音频文件 等  待开展

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/539275
推荐阅读
相关标签
  

闽ICP备14008679号