[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
相关推荐
星辰徐哥2 分钟前
Python AI基础:Python面向对象编程
开发语言·人工智能·python
可涵不会debug4 分钟前
当AI学会了“讲故事“:我用魔珐星云做了一个沉浸式互动叙事Agent
人工智能
沐自礼7 分钟前
图像伪造识别和定位
人工智能·llm
AI 小老六8 分钟前
Google AX 控制面拆解:分布式 Agent 如何把断点恢复、审计策略和执行调度收进同一条链路
人工智能·分布式·后端·ai·架构·ai编程
阿部多瑞 ABU10 分钟前
数据循环悖论:AI检测模型的技术局限与生态灾难
大数据·人工智能·安全·机器学习·ai·自然语言处理
Python私教12 分钟前
我准备用 AI 二开 shadcn-admin,做一个可卖的后台管理系统模板
人工智能
天青色等烟雨..21 分钟前
智慧农林核心遥感技术99个案例实践
运维·人工智能·spring boot·后端·自动化
数智化精益手记局21 分钟前
拆解复杂项目管理流程:用项目管理流程解决跨部门协作低效难题
大数据·运维·数据库·人工智能·产品运营
xhtdj22 分钟前
Uber 如何通过批处理实现单账户每秒30+次更新
大数据·数据库·人工智能·安全·动态规划
yuguo.im28 分钟前
ElevenLabs:用一个 API 让 AI 开口说话
人工智能·eventlabs·voice ai