文章目錄
- 1、簡介
- 2、Windows語音
- 2.1 簡介
- 2.2 安裝
- 2.3 代碼
- 3、pyttsx3
- 3.1 簡介
- 3.2 安裝
- 3.3 代碼
- 4、ggts
- 4.1 簡介
- 4.2 安裝
- 4.3 代碼
- 5、SAPI
- 6、SpeechLib
- 7、百度AI
- 8、百度飛槳
- 結語
1、簡介
TTS(Text To Speech) 譯為從文本到語音,TTS是人工智能AI的一個模組,是人機對話的一部分,即讓機器能夠說話。
TTS是語音合成技術應用的一種,首先采集語音波形,然后進行優化處理,最后存儲在數據庫中,合成語音是提取波形轉換成自然語音輸出。
2、Windows語音
2.1 簡介
https://support.microsoft.com/zh-cn/windows/%E5%9C%A8%E8%AF%AD%E9%9F%B3%E6%8F%90%E7%A4%BA%E4%B8%ADwindows-83ff75bd-63eb-0b6c-18d4-6fae94050571
Windows 語音識別允許你單獨通過語音控制電腦,而無需鍵盤或鼠標。 本文列出了可用于語音識別的命令。
2.2 安裝
speech模塊是一個封裝層模塊,用于調取Windows本地的語音合成服務。因此請確保你使用的OS是Windows并且有python調取Windows的API,pywin32。
pip install speech
speech.py文件進行修改:print(prompt)、import _thread
pip install pypiwin32
2.3 代碼
import speech#自動體系
speech.say("要開始啦")#輸入語音
while True:print(u"開始說話")say = speech.input() # 接收語音# speech.say("你說了" + say) # 說話speech.say(say) # 說話
import speechwhile True:say = speech.input() # 接收語音speech.say("you said:"+say) #說話if say == "你好":speech.say("How are you?")elif say == "天氣":speech.say("今天天氣晴!")
3、pyttsx3
3.1 簡介
https://pypi.org/project/pyttsx3/
適用于 Python 2 和 3 的文本轉語音 (TTS) 庫。無需互聯網連接或延遲即可工作。支持多種TTS引擎,包括Sapi5、nsss、espeak等。
pyttsx3 是 Python 中的文本到語音轉換庫。與其他庫不同,它可以離線工作,并且與 Python 2 和 3 兼容。
pyttsx3庫 : 是Python中的文本到語音轉換庫, 它可以脫機工作
優點 : 可以脫機工作, 支持將語音直接朗讀, 可調節音量和速度
缺點 : 初始只有英語(女)和中文(女)的語音包, 其他語言的語音包需要另外下載
3.2 安裝
pip install pyttsx3
3.3 代碼
#coding=utf-8
import pyttsx3
pyttsx3.speak("Hello World!")
pyttsx3.speak("持續推動我國經濟實現質的有效提升和量的合理增長")
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
import pyttsx3
engine = pyttsx3.init() # object creation""" RATE"""
rate = engine.getProperty('rate') # getting details of current speaking rate
print (rate) #printing current voice rate
engine.setProperty('rate', 125) # setting up new voice rate"""VOLUME"""
volume = engine.getProperty('volume') #getting to know current volume level (min=0 and max=1)
print (volume) #printing current volume level
engine.setProperty('volume',1.0) # setting up volume level between 0 and 1"""VOICE"""
voices = engine.getProperty('voices') #getting details of current voice
#engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male
engine.setProperty('voice', voices[1].id) #changing index, changes voices. 1 for femaleengine.say("Hello World!")
engine.say('My current speaking rate is ' + str(rate))
engine.runAndWait()
engine.stop()"""Saving Voice to a file"""
# On linux make sure that 'espeak' and 'ffmpeg' are installed
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()
4、ggts
4.1 簡介
https://pypi.org/project/gTTS/
gTTS(Google Text-to-Speech),一個 Python 庫和 CLI 工具,用于與 Google 翻譯文本轉語音 API 交互。
gTTS(Google Text-to-Speech),一個 Python 庫和 CLI 工具,用于與 Google Translate 的文本轉語音 API 交互。 將語音數據寫入文件、類似文件的對象 (bytestring) 以進行進一步的音頻操作。
4.2 安裝
pip install gTTS
4.3 代碼
gtts-cli 'hello' --output hello.mp3
或
from gtts import gTTS
tts = gTTS('hello')
tts.save('hello.mp3')
執行失敗了。
5、SAPI
SAPI是微軟Speech API , 是微軟公司推出的語音接口,而從WINXP開始,系統上就已經有語音識別的功能了,可是用武之地相當之少,他并沒有給出一些人性化的自定義方案,僅有的語音操控命令顯得相當雞脅。
操作window dll的庫,它可以實現很多功能,十分強大。
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("hello")
6、SpeechLib
comtypes依賴pyttsx3包。( comtypes Required-by: pyttsx3 )
安裝:
pip install comtypes
代碼:
from comtypes.client import CreateObjectengine = CreateObject('SAPI.SpVoice')
stream = CreateObject('SAPI.SpFileStream')from comtypes.gen import SpeechLib # 導這個包必須放在 上面3行代碼 后面,否則運行時會報錯。infile = 'demo.txt'
outfile = 'demo_audio.wav'stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
# 讀取文本內容
f = open(infile, 'r', encoding='utf-8')
theText = f.read()
f.close()
engine.speak(theText)
stream.close()
7、百度AI
https://ai.baidu.com/
基于Deep Peak2的端到端建模,將音頻流實時識別為文字,并返回每句話的開始和結束時間,適用于長句語音輸入、音視頻字幕、會議等場景.
https://ai.baidu.com/sdk#asr
https://console.bce.baidu.com/ai/#/ai/speech/overview/resource/getFree
pip install baidu_aip
from aip import AipSpeech#用上面提到的APP ID, API Key和Secret Key替換
APP_ID = 'xxxx'
API_KEY = 'xxxx'
SECRET_KEY = 'xxxx'client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)manual = r'百度你好! \
語音合成測試。'if __name__ == '__main__':print('start voice process')#語速spd: 5, 語調pit: 5, 播音per: 1result = client.synthesis(manual, 'zh', 1, {'vol': 5, 'per':1,})# 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼if not isinstance(result, dict):with open('audio.mp3', 'wb') as f:f.write(result)print('process end')
8、百度飛槳
pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
pip install paddlespeech -i https://pypi.tuna.tsinghua.edu.cn/simplepaddlespeech tts --input '人工智能體驗' --output test.wav
paddlespeech asr --input test.wav
paddlespeech asr --lang zh --input test.wav
from paddlespeech.cli.asr.infer import ASRExecutor
asr = ASRExecutor()
result = asr(audio_file="test.wav")
print(result)
結語
如果您覺得該方法或代碼有一點點用處,可以給作者點個贊,或打賞杯咖啡;
╮( ̄▽ ̄)╭
如果您感覺方法或代碼不咋地
//(ㄒoㄒ)//,就在評論處留言,作者繼續改進;
o_O???
如果您需要相關功能的代碼定制化開發,可以留言私信作者;
(????)
感謝各位大佬童鞋們的支持!
( ′ ▽′ )ノ ( ′ ▽′)っ!!!