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()

  1. 语音识别,使用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))

  1. 使用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"])

相关推荐
践行见远29 分钟前
django之视图
python·django·drf
love530love1 小时前
Windows避坑部署CosyVoice多语言大语言模型
人工智能·windows·python·语言模型·自然语言处理·pycharm
prinrf('千寻)2 小时前
MyBatis-Plus 的 updateById 方法不更新 null 值属性的问题
java·开发语言·mybatis
m0_555762902 小时前
Qt缓动曲线详解
开发语言·qt
掘金-我是哪吒3 小时前
分布式微服务系统架构第132集:Python大模型,fastapi项目-Jeskson文档-微服务分布式系统架构
分布式·python·微服务·架构·系统架构
揽你·入怀3 小时前
数据结构:ArrayList简单实现与常见操作实例详解
java·开发语言
AA-代码批发V哥3 小时前
Math工具类全面指南
java·开发语言·数学建模
xhdll3 小时前
egpo进行train_egpo训练时,keyvalueError:“replay_sequence_length“
python·egpo
Nobkins4 小时前
2021ICPC四川省赛个人补题ABDHKLM
开发语言·数据结构·c++·算法·图论
Cchaofan4 小时前
lesson01-PyTorch初见(理论+代码实战)
人工智能·pytorch·python