python中文语音识别

1) 生成需要识别的wav文件,SpeechRecognition需要wav文件,不能识别mp3文件

安装库:

sudo apt install espeak ffmpeg libespeak1
pip install pyttsx3

代码:

def demo_tts_wav():
    import pyttsx3

    engine = pyttsx3.init()

    engine.setProperty('rate', 150)
    engine.setProperty('volume', 1.0)
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[0].id)

    text = '你好,我是一个AI机器人'
    #engine.say(text)
    filename = 'ni_hao.wav'
    engine.save_to_file(text, filename)

    engine.runAndWait()
 

2. 语音识别,使用speech_recognition

安装库:

pip install SpeechRecognition

pip install pocketsphinx

下载模型文件:CMU Sphinx - Browse /Acoustic and Language Models/Mandarin at SourceForge.net

pip install vosk

下载模型文件到代码目录下:VOSK Models

解压,并且重命名为model

代码

def demo_speech_recognition():
    import speech_recognition as sr

    r = sr.Recognizer()
    try:
        audio_file = sr.AudioFile('ni_hao.wav')
        with audio_file as source:
            audio_data = r.record(source)
        #text = r.recognize_google(audio_data, language='zh-Cn')
        #text = r.recognize_wit(audio_data)
        text = r.recognize_vosk(audio_data, language='zh-Cn')
        print("识别结果:", text)
    except Exception as e:
        print("无法识别语音:", str(e))

3. 使用whisper库,效果最好,可以离线

安装:

pip install -U openai-whisper

权重文件不方便下载的话可以到这下载:https://download.csdn.net/download/love_xunmeng/88651611

然后移动到:

mv small.pt /home/user_account/.cache/whisper/

代码:

def demo_whisper():
    import whisper
    model = whisper.load_model("small")
    result = model.transcribe("ni_hao.wav")
    print(result["text"])

相关推荐

  1. python中文语音识别

    2023-12-22 12:36:02       78 阅读
  2. 中文语音识别实战(ASR)

    2023-12-22 12:36:02       28 阅读
  3. Whisper——部署fast-whisper中文语音识别模型

    2023-12-22 12:36:02       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-22 12:36:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-22 12:36:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-22 12:36:02       82 阅读
  4. Python语言-面向对象

    2023-12-22 12:36:02       91 阅读

热门阅读

  1. 华纳云:怎么用python实现进程,线程和协程

    2023-12-22 12:36:02       62 阅读
  2. AI write rust 2

    2023-12-22 12:36:02       60 阅读
  3. rust热门前后端框架

    2023-12-22 12:36:02       62 阅读
  4. 密钥盐技术简介及作用(含示例)

    2023-12-22 12:36:02       65 阅读
  5. 力扣61. 旋转链表

    2023-12-22 12:36:02       63 阅读