python进阶-13-语音识别
2020-04-03 本文已影响0人
西海岸虎皮猫大人
1.文字转语音
安装模块:
pip install pyttsx3
异常:
ModuleNotFoundError: No module named 'pythoncom'
解决:
pip install pywin32
代码:
使用pyttsx3
# coding=utf-8
import pyttsx3 as pyttsx
engine = pyttsx.init()
# 调节语速
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 100)
engine.say('吴佩祯我爱你')
engine.runAndWait()
使用SAPI(无需安装模块)
# coding=utf-8
from win32com.client import Dispatch
speaker = Dispatch('SAPI.SpVoice')
speaker.Speak('吴佩祯我爱你')
del speaker
2.文本文件转语音
使用SpeechLib模块
安装:
pip install comtypes
代码:
# coding=utf-8
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)
# 读取文件内容
f = open(infile, 'r', encoding='utf-8')
theText = f.read()
f.close()
engine.speak(theText)
stream.close()
3.文本文件转语音
使用开源第三方模块PocketSphinx
pip install PocketSphinx
pip install SpeechRecognition
异常:
error: command 'swig.exe' failed:
解决:
改用下载安装的方式,下载地址:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pocketsphinx
执行:
python -m pip install .\pocketsphinx-0.1.15-cp37-cp37m-win_amd64.whl
代码实现:
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'))
异常:
ecognize_sphinx raise UnknownValueError() # no transcriptions available
另外,似乎不支持mp3和wmv格式,坑