Catalog
pyttsx
SAPI
SpeechLib
PocketSphinx
install pyttsx library :pip install pyttsx3
import pyttsx3 as px3
speak = px3.init() # Initialize the voice engine
rate = speak.getProperty('rate')
print(' The speed :%s' % rate) # Default :200
volume = speak.getProperty('volume')
print(' The volume :%s' % volume) # Default :1.0
speak.setProperty('rate',100) # Set speed
speak.setProperty('volume',2.0) # set volume
speak.say(' Happy family ')
speak.runAndWait()
speak.stop()
from win32com.client import Dispatch
text = ' The strong wind rises with the wind on the same day , Up to 90000 miles '
speak = Dispatch('SAPI.SpVoice')
speak.Speak(text)
del speak
Use SpeechLib, You can get input from a text file , And then convert it into speech .
install 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()
A lightweight speech recognition engine , Open source for voice to text API.
Installation Library :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)) # Don't specify language When parameters are , English is recognized by default en-US
print(r.recognize_sphinx(audio, language='zh-CN'))
except Exception as e:
print(e)
There is no Chinese bag by default , Use times wrong missing PocketSphinx language model parameters directory: "E:\python\Lib\site-packages\speech_recognition\pocketsphinx-data\zh-CN", Need to download :CMU Sphinx - Browse /Acoustic and Language Models at SourceForge.net
After the downloaded package is unzipped, the name is modified to :zh-CN, And put it in the English bag en-US Same directory
modify zh-CN File name and en-US The same as in China
After modification
Realized conversion , The effect is not ideal