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

python文字語音互轉

編輯:Python

目錄

pyttsx

SAPI

SpeechLib

PocketSphinx


pyttsx

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

SAPI

from win32com.client import Dispatch
text = '大風一日同風起,扶搖直上九萬裡'
speak = Dispatch('SAPI.SpVoice')
speak.Speak(text)
del speak

SpeechLib

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

PocketSphinx

一個輕量級的語音識別引擎,用於語音轉換文本的開源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中的一樣

修改後

實現了轉換,就是效果不理想


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