基于java的百度语音识别示例
来源:互联网 发布:教程网网站源码php 编辑:程序博客网 时间:2024/06/16 07:35
最近在弄语音识别这块,然后就看了下百度的语音识别:
这里介绍了使用java实验语音识别。首先你需要的东西:
1、一个pcm或这个wav格式的语音文件(我使用的是官网提供的案列16k.pcm)点击下载。
2、准备sdk包(将这个包的jar包通过build path附上就可以)点击下载;
3、需要在百度云上申请一个百度语音的项目,然后将apikey和secretkey复制到项目中。
注意的是:如果我的语音文件是16k的,rate就是16000,8k的rate就是8000(可以参考官方文档).cuid填本机mac地址就可以了,这个值没有什么要求,我用的就是sercretkey。
下面代码:
package test;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLDecoder;import java.net.URLEncoder;import javax.xml.bind.DatatypeConverter;import org.json.JSONObject;public class demo2 { private static final String serverURL = "http://vop.baidu.com/server_api"; private static String token = ""; private static final String testFileName = "D:\\workspace\\test2\\src\\16k.pcm"; // 百度语音提供技术支持 //put your own params here // 下面3个值要填写自己申请的app对应的值 private static final String apiKey = "jrDWBaBvmhPBYQRjlT7Fk1yk"; private static final String secretKey = "wI2MZhBGKthjBFdyo1miAFsforCesYbX"; private static final String cuid = "wI2MZhBGKthjBFdyo1miAFsforCesYbX"; public static void main(String[] args) throws Exception { getToken(); method1(); method2(); } private static void getToken() throws Exception { String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id=" + apiKey + "&client_secret=" + secretKey; HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL).openConnection(); token = new JSONObject(printResponse(conn)).getString("access_token"); System.out.println(token); } private static void method1() throws Exception { File pcmFile = new File(testFileName); System.out.println(pcmFile.exists()); HttpURLConnection conn = (HttpURLConnection) new URL(serverURL).openConnection(); // construct params JSONObject params = new JSONObject(); params.put("format", "pcm"); params.put("rate", 16000); params.put("channel", "1"); params.put("token", token); params.put("lan", "zh"); params.put("cuid", cuid); params.put("len", pcmFile.length()); params.put("speech", DatatypeConverter.printBase64Binary(loadFile(pcmFile))); // add request header conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); conn.setDoInput(true); conn.setDoOutput(true); // send request DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(params.toString()); wr.flush(); wr.close(); printResponse(conn); } private static void method2() throws Exception { File pcmFile = new File(testFileName); HttpURLConnection conn = (HttpURLConnection) new URL(serverURL + "?cuid=" + cuid + "&token=" + token).openConnection(); // add request header conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "audio/pcm; rate=16000"); conn.setDoInput(true); conn.setDoOutput(true); // send request DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.write(loadFile(pcmFile)); wr.flush(); wr.close(); System.out.println(printResponse(conn)); } private static String printResponse(HttpURLConnection conn) throws Exception { if (conn.getResponseCode() != 200) { // request error System.out.println("conn.getResponseCode() = " + conn.getResponseCode()); return ""; } InputStream is = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); System.out.println(new JSONObject(response.toString()).toString(4)); return response.toString(); } private static byte[] loadFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { is.close(); throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; } }运行结果:
{ "scope": "public brain_all_scope audio_voice_assistant_get audio_tts_post wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower bnstest_fasf lpq_开放", "session_secret": "a1872f3e791eeb5adf327086e6a0d769", "expires_in": 2592000, "refresh_token": "25.f74b044ed07383f7643b8259fb9af970.315360000.1827655515.282335-10467473", "session_key": "9mzdWT+D4w5xoNHb1JejPBZ1qZKj90nUTAVDhweqqQJjH1o4PvJAYHgT8QynTP9BCVSjxA5tihkh2QCabkExrQFTpcK1VQ==", "access_token": "24.be8efa5a534453370396a4accce50aa6.2592000.1514887515.282335-10467473"}24.be8efa5a534453370396a4accce50aa6.2592000.1514887515.282335-10467473true{ "result": ["北京科技馆,"], "err_msg": "success.", "sn": "592584910701512295515", "err_no": 0, "corpus_no": "6495259779570311967"}{ "result": ["北京科技馆,"], "err_msg": "success.", "sn": "587847696871512295516", "err_no": 0, "corpus_no": "6495259783832855718"}{"corpus_no":"6495259783832855718","err_msg":"success.","err_no":0,"result":["北京科技馆,"],"sn":"587847696871512295516"}
阅读全文
0 0
- 基于java的百度语音识别示例
- 基于java的百度语音识别示例
- 【百度语音识别】JavaAPI方式语音识别示例 MP3转PCM文件Java实现
- Android 开发基于百度语音识别技术的小程序
- android之基于百度语音合讯飞语音识别的语音交互
- 基于科大讯飞的语音识别
- 科大讯飞和百度语音平台语音识别Java调用记录
- 百度语音识别的文档链接
- 调用百度语音SDK,简单的语音识别控件
- 百度语音API的Python语音识别实践
- 百度语音识别
- node百度语音识别
- iOS-百度语音识别
- Android 语音识别示例
- 基于TMS320VC5509A的语音识别与控制系统
- 基于matlab的语音识别系统设计
- 基于DBN的语音识别技术分析
- 基于DBN的语音识别技术分析
- bzoj2956 模积和 (分块)
- Androd 动画
- go http client, http server
- 后端_第八课_数据库
- C#中Invoke的用法
- 基于java的百度语音识别示例
- mac/linux conda:command no found
- 1032. 挖掘机技术哪家强(20) PAT乙级真题
- BZOJ1090(SCOI2003)[字符串折叠]--区间DP
- CodeForces
- 1052. 卖个萌 (20)
- 机器学习实战 Kmeans
- 关于操作系统进程
- 深入理解 Neutron -- OpenStack 网络实现:VXLAN 模式