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"])

相关推荐
孤寂大仙v2 分钟前
【C++】STL----list常见用法
开发语言·c++·list
D11_28 分钟前
Pandas缺失值处理
python·机器学习·数据分析·numpy·pandas
花生了什么树~.1 小时前
python基础知识(四)--if语句,for\while循环
python
她似晚风般温柔7891 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
咩咩大主教1 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
FuLLovers1 小时前
2024-09-13 冯诺依曼体系结构 OS管理 进程
linux·开发语言
IT毕设梦工厂2 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
everyStudy2 小时前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
luthane2 小时前
python 实现average mean平均数算法
开发语言·python·算法
码农研究僧3 小时前
Flask 实现用户登录功能的完整示例:前端与后端整合(附Demo)
python·flask·用户登录