说明:
1,百度语音极速版,需要事先保存声音文件,然后调用百度接口。

2,关于文件的上传,可以用JSON格式,上传文件的base64编码,也可以用row格式直接上传文件。下面的代码是JSON格式。

3,使用接口前需要向百度AI开放平台注册APPID,得到秘钥,然后用得到的APPID和秘钥来获得TOKEN。调用百度语音接口时需要TOKEN。

4,百度提供了一个工具,可以从拼音角度判断相似度,score越小说明拼音越相似,挺好用,非常适合需要匹配的场景。

5,相关Maven库:

<dependency><groupId>com.github.stuxuhai</groupId><artifactId>jpinyin</artifactId><version>1.1.8</version>
</dependency>

上代码:

package test;import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Encoder;import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;/*** 百度语音识别相关工具*/
public class VopUtil {public final static String URL = "https://vop.baidu.com/pro_api";public final static String TOKEN = "";  //需要事先注册public final static String CUID = "zheshiyigeceshi"; //用户唯一标识,用来区分用户,计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内。public static void main(String[] args){String filePath="D:\\语音test\\我要买美汁源果粒橙.wav";    //语音文件路径,这个文件是我自己用手机的录音机录的一段语音,内容就是文件名String base64=getBaset64(filePath);                       //文件转为base64格式long fileLength=getFileLength(filePath);                  //获得文件长度,单位字节JSONObject vopResult = getVopStr(base64, fileLength);     //调用百度语音识别接口System.out.println("语音识别接口返回值:"+vopResult.toJSONString());String errMsg = vopResult.get("err_msg").toString();if (!"success.".equals(errMsg)) {System.out.println("识别失败");}String resultStr = vopResult.get("result").toString();resultStr = resultStr.replaceAll("\\[", "").replaceAll("]", "").replaceAll("\"", "");System.out.println("语音识别的结果:"+resultStr);String[] nameArr={"可乐","雪碧","美年达","美汁源","红牛","尖叫","牛奶","酸奶","百事"};  //备选名字列表,用于匹配拼音相似度String targetName = getSimilarWord(resultStr, nameArr);System.out.println("拼音最相似:"+targetName);}/*** 请求百度语音识别接口* @param file 文件的base64编码* @param len 文件长度,字节数* @return*/public static JSONObject getVopStr(String file,long len) {JSONObject params = new JSONObject(true);params.put("format", "wav");params.put("rate", 16000);params.put("channel", 1);params.put("cuid", CUID);params.put("token", TOKEN);params.put("dev_pid", 80001);params.put("len", len);params.put("speech", file);String result = "";BufferedReader reader = null;BufferedReader in = null;try {URL url = new URL(URL);// 创建连接HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setInstanceFollowRedirects(true);connection.setRequestMethod("POST"); // 设置请求方式connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式connection.connect();if (params != null) {// 设置文件长度//   connection.setRequestProperty("Content-Length", String.valueOf(writebytes.length));OutputStream outwritestream = connection.getOutputStream();outwritestream.write(params.toJSONString().getBytes());outwritestream.flush();outwritestream.close();}int responseCode = connection.getResponseCode();InputStream inputStream = null;if (responseCode == 200) {inputStream = new BufferedInputStream(connection.getInputStream());} else {inputStream = new BufferedInputStream(connection.getErrorStream());}in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e) {e.printStackTrace();}}}return JSONObject.parseObject(result);}/*** 从目标字符串列表中查询拼音最相似的字符串* @param input 待查询的字符串* @param arr 备选词语库* @return*/public static String getSimilarWord(String input,String[] arr){try {Search d = new Search(arr);List<Search.Score> list=d.search(input,10);System.out.println("拼音相似度列表:"+list);if(list!=null&&list.size()>0){Search.Score score=list.get(0);return score.word.toString();}}catch (Exception e){e.printStackTrace();}return null;}/*** 文件转base64* @param filePath 文件路径* @return*/private static String getBaset64(String filePath){String base64="";try {File file = new File(filePath);FileInputStream inputFile = new FileInputStream(file);byte[] buffer = new byte[(int)file.length()];inputFile.read(buffer);inputFile.close();base64= new BASE64Encoder().encode(buffer);if(base64.contains("\n")){base64=base64.replaceAll("\n","");}if(base64.contains("\r")){base64=base64.replaceAll("\r","");}}catch (Exception e){e.printStackTrace();}return base64;}/*** 得到文件大小,单位:字节* @param filePath 文件路径* @return*/private static long getFileLength(String filePath){File file = new File(filePath);long fileLength=file.length();return fileLength;}}

下面是Search类,这个类是百度提供的:

package test;import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;/*** 本文件用来演示如何通过文本相似度进行纠错* <p>* <pre>* String[] list = new String[]{"张三", "张衫", "张丹", "张成", "李四", "李奎"};* Search d = new Search(list);* System.out.println(d.search("张三", 10));* System.out.println(d.search("李四", 10));** 输出:* [{word=张三, score=0}, {word=张衫, score=1}, {word=张丹, score=1}, {word=张成, score=5}, {word=李四, score=9}, {word=李奎, score=10}]* [{word=李四, score=0}, {word=李奎, score=3}, {word=张三, score=9}, {word=张衫, score=10}, {word=张丹, score=10}, {word=张成, score=12}]* </pre>*/
public class Search {final List<Word> targets = new ArrayList<Word>();public Search(String[] list) throws PinyinException {for (String s : list) {Word w = new Word(s);targets.add(w);}}public List<Score> search(String input, int limit) throws PinyinException {Word w = new Word(input);return targets.stream().map(x -> {Score s = new Score();s.word = x;s.score = x.compareTo(w);return s;}).sorted().limit(limit).collect(Collectors.toList());}public static int getEditDistance(String s, String t) {int d[][]; // matrixint n; // length of sint m; // length of tint i; // iterates through sint j; // iterates through tchar s_i; // ith character of schar t_j; // jth character of tint cost; // cost// Step 1n = s.length();m = t.length();if (n == 0) {return m;}if (m == 0) {return n;}d = new int[n + 1][m + 1];// Step 2for (i = 0; i <= n; i++) {d[i][0] = i;}for (j = 0; j <= m; j++) {d[0][j] = j;}// Step 3for (i = 1; i <= n; i++) {s_i = s.charAt(i - 1);// Step 4for (j = 1; j <= m; j++) {t_j = t.charAt(j - 1);// Step 5cost = (s_i == t_j) ? 0 : 1;// Step 6d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1,d[i - 1][j - 1] + cost);}}// Step 7return d[n][m];}private static int Minimum(int a, int b, int c) {int im = a < b ? a : b;return im < c ? im : c;}class Word implements Comparable {final String word;final String pinyin1;final String pinyin2;Word(String word) throws PinyinException {this.word = word;this.pinyin1 = PinyinHelper.convertToPinyinString(word, ",", PinyinFormat.WITH_TONE_NUMBER);this.pinyin2 = PinyinHelper.convertToPinyinString(word, ",", PinyinFormat.WITHOUT_TONE);}@Overridepublic String toString() {return word;}@Overridepublic int compareTo(Object o) {if (o instanceof Word) {Word o1 = (Word) o;int score1 = getEditDistance(this.pinyin1, o1.pinyin1);int score2 = getEditDistance(this.pinyin2, o1.pinyin2);return score1 + score2;}return 0;}}class Score implements Comparable {Word word;int score;@Overridepublic int compareTo(Object o) {if (o instanceof Score) {return score - ((Score) o).score;}return 0;}@Overridepublic String toString() {return "{" +"word=" + word +", score=" + score +'}';}}
}

本地执行main方法后的输出结果:

语音识别接口返回值:{"result":["我要买美汁源果粒橙。"],"err_msg":"success.","sn":"275665891801562141461","corpus_no":"6709346487565939628","err_no":0}
语音识别的结果:我要买美汁源果粒橙。
拼音相似度列表:[{word=美汁源, score=58}, {word=美年达, score=66}, {word=百事, score=73}, {word=红牛, score=74}, {word=尖叫, score=74}, {word=酸奶, score=75}, {word=可乐, score=76}, {word=牛奶, score=76}, {word=雪碧, score=77}]
拼音最相似:美汁源

百度语音识别极速版的使用例子(JAVA)相关推荐

  1. react-native 使用百度语音识别极速版语音转文字

    项目有需求,需要集成语音转文字,于是开始研究,一步一个坑 1.集成录音 react-native-audio 2.集成文件操作 react-native-fs 3.集成音频格式转换 react-nat ...

  2. 天猫精灵 python 控制_树莓派4B小爱同学、天猫精灵、智能音箱、百度语音极速版、百度语音标准版、语音识别、语音合成终极方案...

    来源:树莓派创客空间 https:/r-pi.cn,里面更多教程 第一句话,python2版本的代码,python3你改改就完事了 第一句话,python2版本的代码,python3你改改就完事了 流 ...

  3. 树莓派小爱同学、天猫精灵、智能音箱、百度语音极速版、百度语音标准版、语音识别、语音合成终极方案

    第一句话,python2版本的代码,python3你改改就完事了 流程图如下: 全套教程下载地址点击下载 首先需要调用这个方法安装百度的语音识别 https://ai.baidu.com/ai-doc ...

  4. 百度大脑语音识别极速版~C#开荒攻略

    作者:goJhou 语音识别极速版 API 将60秒以内的完整音频文件识别为文字,适用于近场短语音交互,如手机语音搜索.聊天输入等场景.输入完整音频文件,输出识别结果文字. 采用流式多级截断注意力模型 ...

  5. 百度地图-极速版搭建手册

    1.百度地图极速版简介 本套API是专为手机浏览器提供的API,您可使用该套API,在手机页面中展示地图.标注位置.检索poi.查询线路等. 特点: 较同时兼容PC和手机浏览器的JavaScript ...

  6. python调用百度语音搜索_使用 Python 和百度语音识别生成视频字幕

    从视频中提取音频 安装 moviepy pip install moviepy 相关代码: audio_file = work_path + '\\out.wav' video = VideoFile ...

  7. 使用 Python 和百度语音识别生成视频字幕

    文章目录 从视频中提取音频 根据静音对音频分段 使用百度语音识别 获取 Access Token 使用 Raw 数据进行合成 生成字幕 总结 从视频中提取音频 安装 moviepy pip insta ...

  8. python怎么加字幕_使用Python和百度语音识别生成视频字幕的实现

    从视频中提取音频 安装 moviepy pip install moviepy 相关代码: audio_file = work_path + '\\out.wav' video = VideoFile ...

  9. python 视频语音转文字_使用Python和百度语音识别生成视频字幕的实现

    从视频中提取音频 安装 moviepy pip install moviepy 相关代码: audio_file = work_path + '\\out.wav' video = VideoFile ...

最新文章

  1. mysql所有表查询
  2. mysql jdbc 创建存储过程_mysql JDBC 调用存储过程
  3. 【Spring框架家族】SpringBoot整合第三方
  4. ubuntu安装配置samba共享
  5. 里写注释 postman_没用过这些IDEA插件?怪不得写代码头疼
  6. linux查看某端口进程占用,Linux下查看某端口占用进程
  7. form表单的onsubmit()问题 集合
  8. 起一卦,还是那个破事。还是大凶。
  9. 老司机找BUG指南,赶紧拿走。。
  10. C++学习 优雅的实现对象到文件的序列化/反序列化 关键字serialize
  11. (17)FPGA速度和面积互换原则
  12. js 回调函数 精析
  13. php连接mysql的方式_php连接mysql的三种方式
  14. TensorRT 进阶用法
  15. 云上压缩软件卸载方法
  16. PS给图片四周加上圆角
  17. Springboot汽车配件销售管理系统毕业设计源码131650
  18. 一步一步学网络爬虫(从python到scrapy)
  19. JeecgBoot 低代码平台 2.4.5 版本发布,钉钉与企业微信集成版本
  20. 这么多编程学习网站,总有一个适合你吧

热门文章

  1. 封包工具Enigma Virtual Box
  2. PHPWord 表格居中和合并单元格
  3. python用户登录a_python实现用户登录
  4. linux 切换网络 命令,Linux 的常用网络命令
  5. CSharp调用Matlab编译的dll
  6. 《Node.js》中间件和洋葱圈模型
  7. 笔记:联立方程模型总结
  8. 2020年数学建模国赛B题:穿越沙漠
  9. 火山中文编程 -- MD5算法和SHA算法
  10. UnicodeMath编码教程(转载)