File size: 18,625 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
package soe.v20180724;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.UUID;
import com.tencentcloudapi.soe.v20180724.models.*;
import com.tencentcloudapi.soe.v20180724.SoeClient;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.profile.ClientProfile;
/**
* 运行步骤:
* 1 下载测试音频数据到本地
* bike.mp3: https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/bike.mp3
* cn.mp3: https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/cn.mp3
* Rex was a big dog.wav : https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/Rex%20was%20a%20big%20dog.wav
* I see sixteen eggs and eighteen apples.wav : https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/I%20see%20sixteen%20eggs%20and%20eighteen%20apples.wav
* 2 配置测试音频数据目录常量 AUDIO_FOR_ONCE、AUDIO_FOR_CN、AUDIO_FOR_SEQ、AUDIO_FOR_ASYNC 为音频下载目录
* 3 修改密钥常量 SECRET_ID、SECRET_KEY 为腾讯云帐号密钥
* 4 运行示例程序
*/
public class EvaluationExamples {
public static final String REGION = "ap-beijing";
public static final String END_POINT = "soe.ap-beijing.tencentcloudapi.com";
public static final String SECRET_ID = "";
public static final String SECRET_KEY = "";
public static final int EVAL_MODE_WORD = 0;
public static final int EVAL_MODE_SENTENCE = 1;
public static final int EVAL_MODE_PARA = 2;
public static final int EVAL_MODE_FREETALK = 3;
public static final int WORK_MODE_STREAM = 0;
public static final int WORK_MODE_ONCE = 1;
public static final int MP3 = 3;
public static final int WAV = 2;
public static final int RAW = 1;
public static final int CN = 1;
public static final int EN = 0;
public static final int PKG_SIZE = 16 * 1024;
public static final String FINISHED = "Finished";
public static final String FAILED = "Failed";
public static final String EVALUATING = "Evaluating";
/*
* 测试音频下载地址
* bike.mp3: https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/bike.mp3
* cn.mp3: https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/cn.mp3
* Rex was a big dog.wav : https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/Rex%20was%20a%20big%20dog.wav
* I see sixteen eggs and eighteen apples.wav : https://soe-common-1255701415.cos.ap-guangzhou.myqcloud.com/I%20see%20sixteen%20eggs%20and%20eighteen%20apples.wav
*
*/
public static final String AUDIO_FOR_ONCE = "./bike.mp3";
public static final String AUDIO_FOR_CN = "./cn.mp3";
public static final String AUDIO_FOR_SEQ = "Rex was a big dog.wav";
public static final String AUDIO_FOR_ASYNC = "./I see sixteen eggs and eighteen apples.wav";
public static String getSessionId() {
// sessionID 保证唯一性,建议使用时间戳加随机数或者直接使用 uuid
String sessionId = UUID.randomUUID().toString().replaceAll("-", "");
return sessionId;
}
/**
* 过长文本转段落模式重试的初始化请求
* @param client
* @param req
* @return
* @throws TencentCloudSDKException
*/
public static InitOralProcessResponse InitWithRetry(SoeClient client, InitOralProcessRequest req) throws TencentCloudSDKException {
if (req == null) {
return null;
}
if (client == null) {
return null;
}
try {
InitOralProcessResponse resp = client.InitOralProcess(req);
return resp;
} catch (TencentCloudSDKException e) {
// 长字符串采用段落模式,可以根据业务情景修改重试策略
if (e.getMessage().indexOf("InvalidParameterValue.RefTxtTooLang") >= 0) {
if (req.getEvalMode() == EVAL_MODE_WORD || req.getEvalMode() == EVAL_MODE_SENTENCE) {
req.setEvalMode(EVAL_MODE_PARA);
return client.InitOralProcess(req);
}
}
throw e;
}
}
/**
* 当网络错误导致参数缺失重试数据传输请求
* @param client
* @param req
* @return
* @throws TencentCloudSDKException
*/
public static TransmitOralProcessResponse TransmitWithRetry(SoeClient client,TransmitOralProcessRequest req) throws TencentCloudSDKException {
if (req == null) {
return null;
}
if (client == null) {
return null;
}
try {
TransmitOralProcessResponse resp = client.TransmitOralProcess(req);
return resp;
} catch (TencentCloudSDKException e) {
// 避免网络丢包数据,可以根据业务情景修改重试策略
if (e.getMessage().indexOf("MissingParameter") >= 0){
TransmitOralProcessResponse resp = client.TransmitOralProcess(req);
return resp;
}
throw e;
}
}
/**
* 一次性评估示例,用于较短时间音频的评估,例如单词或者短句,评估一次性发布所有音频数据
*
* @param client
*/
public static void EvaluationOnce(SoeClient client) {
System.out.println("----------一次性评估示例------------");
String sessionId = getSessionId();
InitOralProcessRequest req = new InitOralProcessRequest();
//设置初始化,参数详情可见官方文档
req.setSessionId(sessionId);
req.setEvalMode(EVAL_MODE_WORD);
req.setRefText("bike");
req.setStorageMode(1);
// 设置为流式评估
req.setWorkMode(WORK_MODE_ONCE);
req.setScoreCoeff(1.0f);
try {
InitOralProcessResponse resp = InitWithRetry(client,req);
System.out.println("初始化返回结果:");
System.out.println(InitOralProcessRequest.toJsonString(resp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
TransmitOralProcessRequest transReq = new TransmitOralProcessRequest();
transReq.setSessionId(sessionId);
transReq.setVoiceEncodeType(1);
transReq.setVoiceFileType(MP3);
try {
byte[] buf = Files.readAllBytes(Paths.get(AUDIO_FOR_ONCE));
String base64Str = new sun.misc.BASE64Encoder().encode(buf);
//String base64Str = Base64.getEncoder().encodeToString(buf);
transReq.setUserVoiceData(base64Str);
transReq.setSeqId(1);
transReq.setIsEnd(1);
TransmitOralProcessResponse transResp = TransmitWithRetry(client,transReq);
System.out.println(transResp.getStatus());
System.out.println(TransmitOralProcessResponse.toJsonString(transResp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
}
/**
* 中文评估示例,除了init参数 ServerType 以外,和英文评估完全一样
*
* @param client
*/
public static void EvaluationCn(SoeClient client) {
System.out.println("----------中文评估示例------------");
String sessionId = getSessionId();
InitOralProcessRequest req = new InitOralProcessRequest();
//设置初始化,参数详情可见官方文档
req.setSessionId(sessionId);
req.setEvalMode(EVAL_MODE_SENTENCE);
req.setRefText("轻轻的我走了,正如我轻轻的来");
// 设置为流式评估
req.setWorkMode(WORK_MODE_ONCE);
req.setServerType(CN);
req.setScoreCoeff(1.0f);
try {
InitOralProcessResponse resp = InitWithRetry(client,req);
System.out.println("初始化返回结果:");
System.out.println(InitOralProcessRequest.toJsonString(resp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
// 传输音频数据到评估服务
TransmitOralProcessRequest transReq = new TransmitOralProcessRequest();
transReq.setSessionId(sessionId);
transReq.setVoiceEncodeType(1);
transReq.setVoiceFileType(MP3);
try {
byte[] buf = Files.readAllBytes(Paths.get(AUDIO_FOR_CN));
String base64Str = new sun.misc.BASE64Encoder().encode(buf);
transReq.setUserVoiceData(base64Str);
transReq.setSeqId(1);
transReq.setIsEnd(1);
TransmitOralProcessResponse transResp = TransmitWithRetry(client,transReq);
System.out.println(TransmitOralProcessResponse.toJsonString(transResp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
}
/**
* 流式分片传输评估示例,中等长度的音频或者边录边传模式,将音频分片传输到评估服务
*
* @param client
*/
public static void EvaluationMutipleSeq(SoeClient client) {
System.out.println("----------流式评估示例------------");
String sessionId = getSessionId();
InitOralProcessRequest req = new InitOralProcessRequest();
//设置初始化,参数详情可见官方文档
req.setSessionId(sessionId);
req.setEvalMode(EVAL_MODE_SENTENCE);
req.setRefText("Rex was a big dog.");
req.setWorkMode(WORK_MODE_STREAM);
req.setScoreCoeff(3.5f);
req.setIsAsync(1);
try {
InitOralProcessResponse resp = InitWithRetry(client,req);
System.out.println("初始化返回结果:");
System.out.println(InitOralProcessRequest.toJsonString(resp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
// 获取所有音频数据
byte[] data;
try {
data = Files.readAllBytes(Paths.get(AUDIO_FOR_SEQ));
} catch (IOException e) {
e.printStackTrace();
return;
}
// 分片传输数据
int pkgNum = (int) Math.ceil((double) data.length / PKG_SIZE);
for (int i = 1; i <= pkgNum; i++) {
int lastIndex = i * PKG_SIZE;
if (i == pkgNum) {
lastIndex = data.length;
}
byte[] buf = Arrays.copyOfRange(data, (i - 1) * PKG_SIZE, lastIndex);
try {
TransmitOralProcessRequest transReq = new TransmitOralProcessRequest();
String base64Str = new sun.misc.BASE64Encoder().encode(buf);
transReq.setUserVoiceData(base64Str);
transReq.setSessionId(sessionId);
transReq.setVoiceEncodeType(1);
transReq.setVoiceFileType(WAV);
transReq.setSeqId(i);
if (i == pkgNum) {
transReq.setIsEnd(1);
} else {
transReq.setIsEnd(0);
}
TransmitOralProcessResponse transResp = TransmitWithRetry(client,transReq);
System.out.println(TransmitOralProcessResponse.toJsonString(transResp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
return;
}
}
// 评估结束或者评估超时可以查询评估结果
TransmitOralProcessRequest transReq = new TransmitOralProcessRequest();
transReq.setSessionId(sessionId);
transReq.setVoiceEncodeType(1);
transReq.setVoiceFileType(WAV);
try {
transReq.setUserVoiceData("");
transReq.setSeqId(0);
transReq.setIsEnd(0);
transReq.setIsQuery(1);
TransmitOralProcessResponse transResp = TransmitWithRetry(client,transReq);
System.out.println("查询结果:");
System.out.println(TransmitOralProcessResponse.toJsonString(transResp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
}
/**
* 异步评估示例,较长音频的评估,短时间内获取不了结果(评估时间超过 20s ),采用异步模式,主动轮询评估服务得到评估结果,可用于段落或者离线评估
*
* @param client
*/
public static void EvaluationAsync(SoeClient client) {
System.out.println("----------异步评估示例------------");
String sessionId = getSessionId();
InitOralProcessRequest req = new InitOralProcessRequest();
//设置初始化,参数详情可见官方文档
req.setSessionId(sessionId);
req.setEvalMode(EVAL_MODE_SENTENCE);
req.setRefText("I see sixteen eggs and eighteen apples.wav");
req.setWorkMode(WORK_MODE_STREAM);
req.setScoreCoeff(3.5f);
req.setIsAsync(1);
try {
InitOralProcessResponse resp = InitWithRetry(client,req);
System.out.println("初始化返回结果:");
System.out.println(InitOralProcessRequest.toJsonString(resp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
// 获取所有音频数据
byte[] data = null;
try {
data = Files.readAllBytes(Paths.get(AUDIO_FOR_ASYNC));
} catch (IOException e) {
e.printStackTrace();
return;
}
TransmitOralProcessResponse transResp;
int pkgNum = (int) Math.ceil((double) data.length / PKG_SIZE);
for (int i = 1; i <= pkgNum; i++) {
int lastIndex = i * PKG_SIZE;
if (i == pkgNum) {
lastIndex = data.length;
}
byte[] buf = Arrays.copyOfRange(data, (i - 1) * PKG_SIZE, lastIndex);
try {
TransmitOralProcessRequest transReq = new TransmitOralProcessRequest();
String base64Str = new sun.misc.BASE64Encoder().encode(buf);
transReq.setUserVoiceData(base64Str);
transReq.setSessionId(sessionId);
transReq.setVoiceEncodeType(1);
transReq.setVoiceFileType(WAV);
transReq.setSeqId(i);
if (i == pkgNum) {
transReq.setIsEnd(1);
} else {
transReq.setIsEnd(0);
}
transResp = TransmitWithRetry(client,transReq);
System.out.println(TransmitOralProcessResponse.toJsonString(transResp));
if (transResp.getStatus().equals(FINISHED) || transResp.getStatus().equals(FAILED)) {
return;
}
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
return;
}
}
//查询结果
for (int i = 0; i < 100; i++) {
TransmitOralProcessRequest transReq = new TransmitOralProcessRequest();
transReq.setSessionId(sessionId);
transReq.setVoiceEncodeType(1);
transReq.setVoiceFileType(WAV);
try {
transReq.setUserVoiceData("");
transReq.setSeqId(0);
transReq.setIsEnd(0);
//设置为查询标识
transReq.setIsQuery(1);
transResp = TransmitWithRetry(client,transReq);
System.out.println("查询结果:");
System.out.println(TransmitOralProcessResponse.toJsonString(transResp));
if (transResp.getStatus().equals(FINISHED) || transResp.getStatus().equals(FAILED)) {
System.out.println("exit");
return;
}
Thread.sleep(10000);
} catch (Exception e) {
System.out.println(e.toString());
return;
}
}
}
/**
* 中文评估示例,除了init参数 ServerType 以外,和英文评估完全一样
*
* @param client
*/
public static void EvaluationTransWithInit(SoeClient client) {
System.out.println("----------TransWithInit------------");
String sessionId = getSessionId();
TransmitOralProcessWithInitRequest req = new TransmitOralProcessWithInitRequest();
//设置初始化,参数详情可见官方文档
req.setSessionId(sessionId);
req.setEvalMode(EVAL_MODE_WORD);
req.setRefText("bike");
// 设置为流式评估
req.setWorkMode(WORK_MODE_ONCE);
req.setServerType(EN);
req.setScoreCoeff(1.0f);
req.setVoiceEncodeType(1);
req.setVoiceFileType(MP3);
try {
byte[] buf = Files.readAllBytes(Paths.get(AUDIO_FOR_ONCE));
String base64Str = new sun.misc.BASE64Encoder().encode(buf);
req.setUserVoiceData(base64Str);
req.setSeqId(1);
req.setIsEnd(1);
TransmitOralProcessWithInitResponse transResp = client.TransmitOralProcessWithInit(req);
System.out.println(TransmitOralProcessResponse.toJsonString(transResp));
} catch (Exception e) {
System.out.println(e.toString());
return;
}
}
public static void main(String[] args) {
Credential cred = new Credential(SECRET_ID, SECRET_KEY);
HttpProfile httpProfile = new HttpProfile();
/**
* 设置访问域名,如果需要就近部署,可以使用 soe-tencentcloudapi.com, 腾讯云将根据访问的地域解析到合适的服务器上,如果调用服务已确定地域,如华南地区
* 可以直接使用地域域名,加快访问速度
*/
httpProfile.setEndpoint(END_POINT);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SoeClient client = new SoeClient(cred, REGION, clientProfile);
EvaluationOnce(client);
EvaluationCn(client);
EvaluationMutipleSeq(client);
EvaluationAsync(client);
EvaluationTransWithInit(client);
}
} |