python文字语音互转
生活随笔
收集整理的這篇文章主要介紹了
python文字语音互转
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
pyttsx
SAPI
SpeechLib
PocketSphinx
pyttsx
安裝?pyttsx庫:pip install pyttsx3??
import pyttsx3 as px3speak = px3.init() # 初始化語音引擎 rate = speak.getProperty('rate') print('語速:%s' % rate) # 默認:200 volume = speak.getProperty('volume') print('音量:%s' % volume) # 默認:1.0 speak.setProperty('rate',100) # 設(shè)置語速 speak.setProperty('volume',2.0) # 設(shè)置音量 speak.say('合家歡樂啊') speak.runAndWait() speak.stop()SAPI
from win32com.client import Dispatchtext = '大風一日同風起,扶搖直上九萬里' speak = Dispatch('SAPI.SpVoice') speak.Speak(text) del speakSpeechLib
使用SpeechLib,可以從文本文件中獲取輸入,再將其轉(zhuǎn)換為語音。
安裝pip install comtypes
from comtypes.client import CreateObject as ct from comtypes.gen import SpeechLibengine = 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
一個輕量級的語音識別引擎,用于語音轉(zhuǎn)換文本的開源API。
安裝庫:pip install SpeechRecognition
??????????????pip install pocketsphinx?
import speech_recognition as sraudio_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參數(shù)時,默認識別英文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中的一樣
修改后
實現(xiàn)了轉(zhuǎn)換,就是效果不理想
總結(jié)
以上是生活随笔為你收集整理的python文字语音互转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CVPR 2022 部分行人重识别
- 下一篇: Vue实现导入Excel功能