程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python text to speech conversion

編輯:Python

Catalog

pyttsx

SAPI

SpeechLib

PocketSphinx


pyttsx

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()

SAPI

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

SpeechLib

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()

PocketSphinx

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved