[PaddlePaddle飞桨] PaddleSpeech-自动语音识别-小模型部署

PaddleSpeech的GitHub项目地址
环境要求:

html 复制代码
gcc >= 4.8.5
paddlepaddle <= 2.5.1
python >= 3.8
OS support: Linux(recommend), Windows, Mac OSX

pip下载指令:

html 复制代码
python -m pip install paddlepaddle-gpu==2.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple  

pip install paddlespeech==1.4.1

小模型配置代码:

python 复制代码
from paddlespeech.cli.asr.infer import ASRExecutor

ASR_MODELS = ['conformer_wenetspeech-zh-16k', 'conformer_online_wenetspeech-zh-16k',
              'conformer_u2pp_online_wenetspeech-zh-16k',
              'conformer_online_multicn-zh-16k', 'conformer_aishell-zh-16k', 'conformer_online_aishell-zh-16k',
              'transformer_librispeech-en-16k', 'deepspeech2online_wenetspeech-zh-16k',
              'deepspeech2offline_aishell-zh-16k',
              'deepspeech2online_aishell-zh-16k', 'deepspeech2offline_librispeech-en-16k',
              'conformer_talcs-codeswitch_zh_en-16k']
ASR_MODEL = 'conformer_wenetspeech'
ASR_EXECUTOR = ASRExecutor()

音频文件保存代码:

python 复制代码
import io
import os
import uuid
import soundfile as sf
# 将音频数据转换并保存为16kHz采样率、16位量化深度、单声道的WAV文件
def save_audio_file(file_path, file_content):
    # 生成一个唯一的文件名
    unique_filename = str(uuid.uuid4()) + ".wav"

    # 确保目录存在
    if not os.path.exists(file_path):
        os.makedirs(file_path)

    try:
        # 将文件流转换为音频数据
        audio_data, sample_rate = sf.read(io.BytesIO(file_content.read()))

        # 构建完整的文件路径
        file_path_with_file_name = os.path.join(file_path, unique_filename)

        # # 重采样音频数据到16kHz,单声道,16位
        if sample_rate != 16000:
            from scipy.signal import resample
            num_samples = int(len(audio_data) * (16000 / sample_rate))
            audio_data = resample(audio_data, num_samples)
            sample_rate = 16000

        # 保存音频数据为16位,16kHz,单声道的WAV文件
        sf.write(file_path_with_file_name, audio_data, 16000, subtype='PCM_16')

        return file_path_with_file_name, sample_rate

    except Exception as e:
        print(f"Error saving file: {e}")
        return None

获取语音识别结果代码:

python 复制代码
import os
# 获取指定文件的语音识别结果
def get_text_with_asr(file_path_with_file_name, sample_rate):
    if not os.path.exists(file_path_with_file_name):
        return None
    asr_result = ASR_EXECUTOR(
        audio_file=file_path_with_file_name,
        model=ASR_MODEL,
        # sample_rate=sample_rate,
        # lang='zh'
    )
    return asr_result

音频转文字代码:

python 复制代码
import os
# 音频转文字(上传音频文件)
def audio_to_text(file_content, file_name):
    file_path_without_file_name = '.' + STATIC_FILE_PATH + "/"
    if not os.path.exists(file_path_without_file_name):
        os.makedirs(file_path_without_file_name)
    file_path_with_file_name, sample_rate = save_audio_file(file_path_without_file_name, file_content)
    asr_result = get_text_with_asr(file_path_with_file_name, sample_rate)
    return asr_result
相关推荐
狂奔solar13 分钟前
Westlake-Omni 情感端音频生成式输出模型
人工智能
idkmn_13 分钟前
Daily AI 20250513 (集成学习及其与联邦学习的区别)
人工智能·神经网络·机器学习·集成学习
微刻时光40 分钟前
影刀RPA网页自动化总结
运维·人工智能·python·低代码·自动化·rpa·影刀rpa
三天不学习1 小时前
浅析AI大模型为何需要向量数据库?【入门基础】
数据库·人工智能·欧氏距离·向量数据库·余弦相似度
WenGyyyL1 小时前
研读论文——《用于3D工业异常检测的自监督特征自适应》
人工智能·python·深度学习·机器学习·计算机视觉·3d
fydw_7151 小时前
音频生成技术的前沿探索:从语音合成到智能Podcast
人工智能·音视频·语音识别
选型宝1 小时前
腾讯怎样基于DeepSeek搭建企业应用?怎样私有化部署满血版DS?直播:腾讯云X DeepSeek!
人工智能·ai·云计算·腾讯云·选型宝
多巴胺与内啡肽.2 小时前
OpenCV进阶操作:人脸检测、微笑检测
人工智能·opencv·计算机视觉
Wnq100722 小时前
基于 NanoDet 的工厂巡检机器人目标识别系统研究与实现
人工智能·机器学习·计算机视觉·目标跟踪·机器人·巡检机器人
一年春又来2 小时前
AI-02a5a6.神经网络-与学习相关的技巧-批量归一化
人工智能·神经网络·学习