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

相关推荐
青铜发条17 分钟前
【python】python进阶——logging日志模块
python
superlls35 分钟前
(计算机网络)JWT三部分及 Signature 作用
java·开发语言·计算机网络
无规则ai1 小时前
动手学深度学习(pytorch版):第六章节—卷积神经网络(1)从全连接层到卷积
人工智能·pytorch·python·深度学习·cnn
一只鲲1 小时前
56 C++ 现代C++编程艺术5-万能引用
开发语言·c++
秋难降1 小时前
优雅的代码是什么样的?🫣
java·python·代码规范
liulilittle2 小时前
.NET反射与IL反编译核心技术
开发语言·数据库·c#·.net·反射·反编译·il
二闹2 小时前
聊天怕被老板发现?摩斯密码来帮你
后端·python
扛麻袋的少年2 小时前
6.Kotlin的Duration类
android·开发语言·kotlin
mit6.8242 小时前
[RestGPT] OpenAPI规范(OAS)
人工智能·python
360安全应急响应中心3 小时前
Python代码保护之重置操作码映射的攻与防探究(一)
python·逆向