python-自动化篇-运维-语音识别

文章目录

理论

语音识别技术,也被称为自动语音识别,目标是以电脑自动将人类的语音内容转换为相 应的文字和文字转换为语音。

文本转换为语音

使用 pyttsx

使用名为 pyttsx 的 python 包,你可以将文本转换为语音。直接使用 pip 就可以进行安装, 命令如下:

python 复制代码
pip install pyttsx3

【示例】使用 pyttsx 实现文本转换语音

python 复制代码
import pyttsx3 as pyttsx 
engine=pyttsx.init() 
engine.say('你好 pyttsx') 
engine.runAndWait() 

使用 SAPI

在 python 中,你也可以使用 SAPI 来做文本到语音的转换。

【示例】使用 SAPI 实现文本转换语音

python 复制代码
from win32com.client import Dispatch 
msg="你好 SAPI" 
speaker = Dispatch('SAPI.SpVoice') 
speaker.Speak(msg) 
del speaker 

使用 SpeechLib

使用 SpeechLib,可以从文本文件中获取输入,再将其转换为语音。先使用 pip 安装, 命令如下:

python 复制代码
pip install comtypes 

【示例】使用 SpeechLib 实现文本转换语音

python 复制代码
from comtypes.client import CreateObject 
engine=CreateObject("SAPI.SpVoice") 
stream=CreateObject('SAPI.SpFileStream') 
from comtypes.gen import SpeechLib 
infile='demo.txt' 
outfile='demo_audio.wav'
stream.Open(outfile,SpeechLib.SSFMCreateForWrite) engine.AudioOutputStream=stream 
f=open(infile,'r',encoding='utf-8') 
theText=f.read() 
f.close() 
engine.speak(theText) 
stream.close() 

语音转换为文本

使用 PocketSphinx PocketSphinx 是一个用于语音转换文本的开源 API。它是一个轻量级的语音识别引擎, 尽管在桌面端也能很好地工作,它还专门为手机和移动设备做过调优。

首先使用 pip 命令安装所需模块,命令如下:

python 复制代码
pip install PocketSphinx 
pip install SpeechRecognition 

【示例】使用 PocketSphinx 实现语音转换文本

python 复制代码
import speech_recognition as sr 
audio_file='demo_audio.wav' 
r=sr.Recognizer() 
with sr.AudioFile(audio_file) as source: 
audio =r.record(source) try: 
	# print('文本内容:',r.recognize_sphinx(audio,language="zh_CN")) 
	print('文本内容:',r.recognize_sphinx(audio)) 
except Exception as e: 
	print(e) 

注意:

□ 安装完 speech_recognition 之后是不支持中文的,需要在 Sphinx 语音识别工具包里 面 下 载 对 应 的 普 通 话 升 学 和 语 言 模 型 。 下 载 地 址 https://sourceforge.net/projects/cmusphinx/files/Acoustic and Language Mo dels/

□ 将 下 载 好 的 普 通 话 升 学 和 语 言 模 型 放 到 安 装 speech_recognition 模 块 的 pocketsphinx-data 目录下。

代码和效果

01使用pyttsx实现文本_语音

python 复制代码
import pyttsx3 as pyttsx
engine=pyttsx.init()
engine.say('好好学习')
engine.runAndWait()

复制到vscode或其他,运行时需要 pip install pyttsx3

02使用SAPI实现文本_语音

python 复制代码
from win32com.client import Dispatch
speaker=Dispatch('SAPI.SpVoice')
speaker.Speak('大家好')
del speaker

03使用SpeechLib实现文本_语音

python 复制代码
from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
engine=CreateObject('SAPI.SpVoice')
stream=CreateObject('SAPI.SpFileStream')
infile='demo.txt'
outfile='demo_audio.wav'
stream.open(outfile,SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream=stream
#读取文本内容
f=open(infile,'r',encoding='utf-8')
theText=f.read()
f.close()
engine.speak(theText)
stream.close()

需要将文本放到电脑根目录下


04使用PocketSphinx实现语音转换文本

python 复制代码
import speech_recognition as sr
audio_file='demo_audio.wav'
r=sr.Recognizer()
#打开语音文件
with sr.AudioFile(audio_file) as source:
    audio=r.record(source)

#将语音转换为文本
# print('文本内容:',r.recognize_sphinx(audio))
print('文本内容:',r.recognize_sphinx(audio,language='zh-CN'))

一、安装环境库

ModuleNotFoundError: No module named 'speech_recognition'

是:pip install speechrecognition

再安装:pip install PocketSphinx

二、文件放根目录

相关推荐
不良人天码星2 分钟前
Linux的基础指令和环境部署,项目部署实战(下)
linux·运维·服务器
thinkMoreAndDoMore33 分钟前
深度学习(3)-TensorFlow入门(常数张量和变量)
开发语言·人工智能·python
kngines41 分钟前
【Python量化金融实战】-第1章:Python量化金融概述:1.4 开发环境搭建:Jupyter Notebook、VS Code、PyCharm
python·量化金融
kngines1 小时前
【Python量化金融实战】-第1章:Python量化金融概述:1.2 Python在量化金融中的优势与生态
python·量化金融
wapicn991 小时前
‌挖数据平台对接DeepSeek推出一键云端部署功能:API接口驱动金融、汽车等行业智能化升级
java·人工智能·python·金融·汽车·php
蓝桉8021 小时前
图片爬取案例
开发语言·数据库·python
wang_yb1 小时前
『Python底层原理』--Python整数为什么可以无限大
python·databook
m0_748244831 小时前
(CICD)自动化构建打包、部署(Jenkins + maven+ gitlab+tomcat)
自动化·jenkins·maven
EasyNVR1 小时前
基于WebRTC与AI大模型接入EasyRTC:打造轻量级、高实时、强互动的嵌入式音视频解决方案
运维·服务器·微信·小程序·webrtc·p2p·智能硬件
敲上瘾1 小时前
基础dp——动态规划
java·数据结构·c++·python·算法·线性回归·动态规划