目錄
pyttsx
SAPI
SpeechLib
PocketSphinx
安裝 pyttsx庫:pip install pyttsx3
import pyttsx3 as px3
speak = px3.init() # 初始化語音引擎
rate = speak.getProperty('rate')
print('語速:%s' % rate) # 默認:200
volume = speak.getProperty('volume')
print('音量:%s' % volume) # 默認:1.0
speak.setProperty('rate',100) # 設置語速
speak.setProperty('volume',2.0) # 設置音量
speak.say('合家歡樂啊')
speak.runAndWait()
speak.stop()
from win32com.client import Dispatch
text = '大風一日同風起,扶搖直上九萬裡'
speak = Dispatch('SAPI.SpVoice')
speak.Speak(text)
del speak
使用SpeechLib,可以從文本文件中獲取輸入,再將其轉換為語音。
安裝pip install comtypes
from comtypes.client import CreateObject as ct
from comtypes.gen import SpeechLib
engine = ct('SAPI.SpVoice')
stream = ct('SAPI.SpFileStream')
infile = 'shiju.txt'
outfile = 'luming_audio.wav'
stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
with open(infile, 'r', encoding='utf-8') as f:
theText = f.read()
engine.speak(theText)
stream.close()
一個輕量級的語音識別引擎,用於語音轉換文本的開源API。
安裝庫:pip install SpeechRecognition
pip install pocketsphinx
import speech_recognition as sr
audio_file = 'luming_audio.wav'
r = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = r.record(source)
try:
# print(r.recognize_sphinx(audio)) # 不指定language參數時,默認識別英文en-US
print(r.recognize_sphinx(audio, language='zh-CN'))
except Exception as e:
print(e)
默認沒有漢語包,使用時報錯missing PocketSphinx language model parameters directory: "E:\python\Lib\site-packages\speech_recognition\pocketsphinx-data\zh-CN",需要下載:CMU Sphinx - Browse /Acoustic and Language Models at SourceForge.net
下載後的包解壓後修改名稱為:zh-CN,並將其放在英文包en-US同目錄下
修改zh-CN中的文件名和en-US中的一樣
修改後
實現了轉換,就是效果不理想