Commit 06c2aa48 authored by 赵啸非's avatar 赵啸非

修改音频识别结果

parent 16a414d7
......@@ -83,9 +83,6 @@
</profiles>
<dependencies>
<dependency>
<groupId>com.mortals.xhx</groupId>
<artifactId>common-lib</artifactId>
......
......@@ -32,6 +32,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static com.mortals.xhx.base.framework.exception.ExceptionHandle.*;
@RestController
@Slf4j
@RequestMapping("audio")
......@@ -88,33 +90,21 @@ public class ApiSendMsgController {
return emitter;
}
@RequestMapping(value = "upload", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@RequestMapping(value = "upload")
@UnAuth
public SseEmitter doFileUpload(MultipartFile file, @RequestParam(value = "prePath", defaultValue = "") String prePath) {
public String doFileUpload(MultipartFile file, @RequestParam(value = "prePath", defaultValue = "") String prePath) {
Map<String, Object> model = new HashMap<>();
String jsonStr = "";
SseEmitter emitter = new SseEmitter(10 * 60 * 1000L);
try {
if (file == null || file.getSize() == 0L) throw new AppException("文件为空!");
if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
throw new AppException("只支持pcm文件!");
//if (file.getOriginalFilename())
String filePath = uploadService.saveFileUpload(file, prePath, null);
filePath = uploadService.getFilePath(filePath);
log.info("filePath==>" + filePath);
//IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid, emitter);
IatModelMulUtil.file=filePath;
IatModelMulUtil.emitter=emitter;
IatModelMulUtil.appid=appid;
IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid);
String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret);
//log.info("authUrl==>" + authUrl);
......@@ -123,27 +113,28 @@ public class ApiSendMsgController {
String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
Request request = new Request.Builder().url(url).build();
WebSocket webSocket = client.newWebSocket(request, new IatModelMulUtil());
WebSocket webSocket = client.newWebSocket(request, iatModelMulUtil);
// 等待返回结果(阻塞直到收到完整数据或超时)
String result = iatModelMulUtil.getResultFuture().get(60, TimeUnit.SECONDS);
log.info("识别结果:" + result);
model.put("result", result);
// model.put("url", filePath);
model.put("fileName", file.getOriginalFilename());
// model.put("fileName", file.getOriginalFilename());
// model.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
jsonStr = JSONObject.toJSONString(model);
} catch (AppException e) {
emitter.complete();
/* log.debug(e);
model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
model.put(KEY_RESULT_MSG, e.getMessage());*/
// jsonStr = JSONObject.toJSONString(model);
return null;
model.put(KEY_RESULT_MSG, e.getMessage());
} catch (Exception e) {
emitter.complete();
return null;
/* model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
model.put(KEY_RESULT_MSG, "文件上传失败");*/
// jsonStr = JSONObject.toJSONString(model);
model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
model.put(KEY_RESULT_MSG, "文件上传失败");
jsonStr = JSONObject.toJSONString(model);
}
return emitter;
return jsonStr;
}
}
......@@ -18,12 +18,14 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
/**
* 大模型语音识别---方言识别
*/
@Slf4j
public class IatModelMulUtil extends WebSocketListener {
private final CompletableFuture<String> future = new CompletableFuture<>();
/* private static final String hostUrl = "https://iat.cn-huabei-1.xf-yun.com/v1"; // 注意多语种识别,也支持中文音频
private static final String appid = "3cc52607"; //在控制台-我的应用获取
private static final String apiSecret = "ZTdmMjFjMGYxYmJhN2VmYjFlMTg3N2Rk"; // 在控制台-我的应用获取
......@@ -39,14 +41,21 @@ public class IatModelMulUtil extends WebSocketListener {
private static Date dateEnd = new Date();
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");
private final StringBuilder responseBuilder = new StringBuilder();
public static String file; // 识别音频位置*/
public String file; // 识别音频位置*/
public static String appid;
public String appid;
public static SseEmitter emitter;
// public SseEmitter emitter;
/*
public IatModelMulUtil(String file, String appid) {
this.file = file;
this.appid = appid;
// this.emitter = emitter;
}
/*
public IatModelMulUtil(String file, String appid, SseEmitter emitter) {
this.file = file;
this.appid = appid;
......@@ -178,12 +187,15 @@ public class IatModelMulUtil extends WebSocketListener {
for (Ws ws : wsList) {
List<Cw> cwList = ws.cw;
for (Cw cw : cwList) {
log.info("识别结果==》{}", cw.w);
try {
emitter.send(cw.w);
// log.info("识别结果==》{}", cw.w);
responseBuilder.append(cw.w);
/* try {
// emitter.send(cw.w);
} catch (IOException e) {
log.error("发送异常==》{}", e.getMessage());
}
}*/
}
}
}
......@@ -196,7 +208,9 @@ public class IatModelMulUtil extends WebSocketListener {
log.info("耗时:" + (dateEnd.getTime() - dateBegin.getTime()) + "ms");
// System.out.println("最终识别结果 ==》" + decodeRes); // 按照规则替换与追加出最终识别结果
log.info("本次识别sid ==》" + jsonParse.header.sid);
emitter.complete();
// emitter.complete();
future.complete(responseBuilder.toString());
log.info("emitter 关闭");
webSocket.close(1000, "");
......@@ -222,6 +236,8 @@ public class IatModelMulUtil extends WebSocketListener {
// TODO Auto-generated catch block
log.error("onFailure error:" + e.getMessage());
}
future.completeExceptionally(t);
}
......@@ -252,6 +268,10 @@ public class IatModelMulUtil extends WebSocketListener {
return httpUrl.toString();
}
public CompletableFuture<String> getResultFuture() {
return future;
}
// 返回结果拆分与展示,仅供参考
// 返回结果拆分与展示,仅供参考
class JsonParse {
......
......@@ -6,7 +6,7 @@
"baseUrl": "http://127.0.0.1:18001/m"
},
"test": {
"baseUrl": "http://192.168.0.98:18006"
"baseUrl": "http://192.168.0.250:18006"
},
"prod": {
"baseUrl": "http://robot.scsmile.cn"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment