使用百度语音转文字模型
超级简单的一个demo单纯使用HTTP的形式加入,不使用SDK,EASYDL模型接入
超级简单的一个demo
单纯使用HTTP的形式加入,不使用SDK,EASYDL模型接入
/**
* 你的APP_KEY
*/
private final String APP_KEY = "你的APP_KEY";
/**
* 你的SECRET_KEY
*/
private final String SECRET_KEY = "你的SECRET_KEY";
/**
* 文件格式,目前百度支持 pcm、mp3
*/
private final String FORMAT = "pcm";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
/**
* CUID的生成固定字符
*/
private String CUID = "1234567JAVA";
/**
* 采样率固定值
*/
private final int RATE = 16000;
/**
* 百度的请求地址
*/
private String URL="https://vop.baidu.com/server_api";
/**
* 自训练平台上的 dev_id
*/
private int DEV_PID;
/**
* 自训练平台上的 lm_id
*/
private int LM_ID;
private String SCOPE;
// 自训练平台 参数
{
//自训练平台模型上线后,您会看见 第二步:“”获取专属模型参数pid:8001,modelid:1234”,按照这个信息获取 dev_pid=8001,lm_id=1234
DEV_PID = "自训练平台上的 dev_id";
LM_ID = "自训练平台上的 lm_id";
}
/**
*
* @Param speech
* @param length
* @return
* @throws IOException
* @throws DemoException
*/
@Override
public ResultDTO getBaiduApi(String speech, int length) throws IOException, DemoException {
return new Gson().fromJson(run(speech,length), ResultDTO.class);
}
public String run(String speech,int length) throws IOException, DemoException {
TokenHolder holder = new TokenHolder(APP_KEY, SECRET_KEY, SCOPE);
holder.resfresh();
String token = holder.getToken();
String result = null;
//json方式上传音频文件
result = runJsonPostMethod(token,speech,length);
return result;
}
public String runJsonPostMethod(String token,String speech,int length) throws DemoException, IOException {
// byte[] content = getFileContent(FILENAME);
// String speech = base64Encode(content);
JSONObject params = new JSONObject();
params.put("dev_pid", DEV_PID);
params.put("lm_id",LM_ID);//测试自训练平台需要打开注释
params.put("format", FORMAT);
params.put("rate", RATE);
params.put("token", token);
params.put("cuid", CUID);
params.put("channel", "1");
params.put("len", length);
params.put("speech", speech);
// System.out.println(params.toString());
HttpURLConnection conn = (HttpURLConnection) new URL(URL).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setDoOutput(true);
conn.getOutputStream().write(params.toString().getBytes());
conn.getOutputStream().close();
String result = ConnUtil.getResponseString(conn);
params.put("speech", "base64Encode(getFileContent(FILENAME))");
System.out.println("url is : " + URL);
System.out.println("params is :" + params.toString());
return result;
}
private byte[] getFileContent(String filename) throws DemoException, IOException {
File file = new File(filename);
if (!file.canRead()) {
System.err.println("文件不存在或者不可读: " + file.getAbsolutePath());
throw new DemoException("file cannot read: " + file.getAbsolutePath());
}
FileInputStream is = null;
try {
is = new FileInputStream(file);
return ConnUtil.getInputStreamContent(is);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String base64Encode(byte[] content) {
/**
Base64.Encoder encoder = Base64.getEncoder(); // JDK 1.8推荐方法
String str = encoder.encodeToString(content);
**/
char[] chars = Base64Util.encode(content); // 1.7 及以下,不推荐,请自行跟换相关库
String str = new String(chars);
return str;
}
页:
[1]