1.安装PaddleSpeech、以及所需各种环境
相关命令
1.安装python3.8环境
2.安装pycharm
3.安装相应包pip install paddlepaddle==2.4.1
pip install pytest-runner
pip install paddlespeech依次运行
4.完成安装
5.可能会出现相关numpy版本的报错,按照报错信息修改就好
2.语音识别
快速上手、命令行一键体验
paddlespeech asr --lang zh --input zh.wav
Python API 一键预测
>>> from paddlespeech.cli.asr.infer import ASRExecutor
>>> asr = ASRExecutor()
>>> result = asr(audio_file="zh.wav")
>>> print(result)
我认为跑步最重要的就是给我带来了身体健康
参考https://github.com/PaddlePaddle/PaddleSpeech/blob/develop/README_cn.md
3.由flask封装语音识别服务,启动服务后直接可以在后端调用
import warnings
warnings.filterwarnings('ignore')
from flask import Flask, request, jsonify
from paddlespeech.cli.asr.infer import ASRExecutor
app = Flask(__name__)
asr = ASRExecutor()
@app.route('/speech-to-text', methods=['POST'])
def speech_to_text():
# 检查是否收到音频文件
if 'audio' not in request.files:
return jsonify({'error': 'No audio file provided'}), 400
audio_file = request.files['audio']
# 检查文件类型
if audio_file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if audio_file:
# 保存音频文件
audio_file.save('temp.wav')
try:
# 使用 PaddleSpeech 进行语音识别
result = asr(audio_file="temp.wav")
# 检查结果是否为字符串,如果不是则转换为字符串
if not isinstance(result, str):
result = str(result)
print('识别结果:', result)
# 返回识别结果到调用端
return jsonify({'text': result}), 200
except Exception as e:
print(f'Error during speech recognition: {e}')
return jsonify({'error': 'Failed to process audio file'}), 500
return jsonify({'error': 'An error occurred while processing the audio'}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
4.在其他电脑或本机调用语音识别服务
import requests
# 发送 POST 请求到 Flask 服务
url = 'http://localhost:5000/speech-to-text'
files = {'audio': open('jijiji.wav', 'rb')}
response = requests.post(url, files=files)
# 检查响应状态码
if response.status_code == 200:
# 正确调用 .json() 获取返回的 JSON 数据
result = response.json()
print('识别结果:', result.get('text'))
else:
print('请求失败:', response.status_code, response.text)