现在人工智能非常火爆,很多朋友都想学,但是一般的教程都是为博硕生准备的,太难看懂了。最近发现了一个非常适合小白入门的教程,不仅通俗易懂而且还很风趣幽默。所以忍不住分享一下给大家。点这里可以跳转到教程。
小程序 语音识别(一)中写的是,实现语音识别的小程序端的代码,本篇文章主要写后端的 实现
录音上传代码:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String upload1(HttpServletRequest request) throws JSONException, IOException, InterruptedException {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> t = multiRequest.getFileNames();
MultipartFile fileDetail = multiRequest.getFile(t.next());
String fileName = System.currentTimeMillis() + ".mp3";
// 设置过期时间
String path = null;
if(OS.isWindows()){
path = "D:\\voice\\";
}else{
path = "/custom/voice/";
}
File file = new File(path);
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
RandomAccessFile randomFile = null;
try {
randomFile = new RandomAccessFile(path + fileName, "rw");
randomFile.seek(randomFile.length());
randomFile.write(fileDetail.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 转码
String res = null;
try {
String filePath = VoiceController.zm(path, fileName);
// Thread.sleep(5000);
File f = new File(filePath);
for(int i=0;i<100000;i++){
Thread.sleep(50);
if(f.exists()){
res = VoiceController.sb(filePath);
//识别完毕删除文件
final String fName = fileName.split("\\.")[0];
final String p = path;
new Thread(){
@Override
public void run(){
File f = new File(p+fName+".mp3");
File f2 = new File(p+fName+".pcm");
f.delete();
f2.delete();
}
}.start();
break;
}
}
System.out.println("语音识别结果="+res);
} catch (Exception e) {
System.out.println("语音识别失败");
e.printStackTrace();
}
if(StringUtils.isBlank(res)){
return null;
}
res = res.trim();
// StringBuilder sb = new StringBuilder();
// if(res != null && !"".equals(res)){
// for(int i=0;i<res.length();i++){
// if(res.charAt(i)>=48 && res.charAt(i)<=57){
// sb.append(res.charAt(i));
// }
// }
// }
return res;
}
mp3转pcm格式代码
public static String zm(String path, String fileName) throws IOException, InterruptedException {
// ffmpeg -y -i d:/voice/b.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 d:/voice/b.pcm
// ./ffmpeg -y -i /custom/voice/a.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 /custom/voice/a.pcm
// 16000 d:/voice/b2.pcm
String zmName = fileName.split("\\.")[0] + ".pcm";
String ffmpegPath = null;
if(OS.isWindows()){
ffmpegPath = "D:/voice/ffmpeg/bin";
Runtime run = Runtime.getRuntime();
try {
// run.exec("cmd /k shutdown -s -t 3600");
Process process = run.exec("cmd.exe /k start " + ffmpegPath+"/ffmpeg -y -i "+path+fileName+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+path+zmName);
// InputStream in = process.getInputStream();
// while (in.read() != -1) {
// System.out.println(in.read());
// }
// in.close();
// process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}else{
ffmpegPath = "/custom/voice/ffmpeg/";
String cmd = ffmpegPath+"ffmpeg -y -i "+path+fileName+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+path+zmName;
try {
String[] cmdA = { "/bin/sh", "-c", cmd };
Process process = Runtime.getRuntime().exec(cmdA);
LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line).append("\n");
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
return path+zmName;
}
//识别代码
public static String sb(String filePath) throws JSONException, InterruptedException{
try {
// 初始化一个AipSpeech
AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 调用接口
JSONObject res = client.asr(filePath, "pcm", 16000, null);
System.out.println(res.toString(2));
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(res.toString(2));
if(json.containsKey("result")){
List<String> result = (List)json.get("result");
return result.get(0).substring(0, result.get(0).length()-1);
}else{
return null;
}
} catch (Exception e) {
return null;
}
}