【山东大学软件学院创新项目实训】时空漫游 语音转文字后端

工作流程

客户端上传音频
后端接收
转wav格式
请求Azure接口
获得文本
传回客户端

后端接收文件并调用Python

@PostMapping("/getText")
public String getText(MultipartFile file, String id){
    String response;
    try {
        String fileName = file.getOriginalFilename();
        assert fileName != null;
        // 重命名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String timeStamp = "" + System.currentTimeMillis();
        String newName = id + "_" + timeStamp + suffix;
        file.transferTo(new File(Tool.origin_path + newName));
        // python
        Process process = Runtime.getRuntime().exec("python oral.py " +
                                                    Tool.origin_path + newName +
                                                    ' ' + Tool.dst_path + id + "_" + timeStamp + ".wav");
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String res = "";
        String line;
        while( ( line = in.readLine() ) != null ) {
            res += line;
        }
        response = res;
    }catch (Exception e){
        response = "error: " + e.getMessage();
    }
    return response;
}

Python格式转换并请求语音识别

from pydub import AudioSegment
import azure.cognitiveservices.speech as speechsdk
import sys

key = '***'
region = 'eastus'

# to wav
def get_wav(origin_path, dst_path):
  sound = AudioSegment.from_file(origin_path)
  sound.export(dst_path, 'wav')
  
# to text
def from_file(path):
  speech_config = speechsdk.SpeechConfig(subscription=key, region=region)
  audio_config = speechsdk.AudioConfig(filename=path)
  speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
  result = speech_recognizer.recognize_once_async().get()
  return result.text

if __name__=='__main__':
  origin_path = sys.argv[1]
  dst_path = sys.argv[2]
  get_wav(origin_path, dst_path)
  text = from_file(dst_path)
  print(text)
  • 19
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值