0


【小沐学Python】Python实现TTS文本转语音(speech、pyttsx3、百度AI)

文章目录

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。

  1. pip install speech

在这里插入图片描述
speech.py文件进行修改:print(prompt)、import _thread

  1. pip install pypiwin32

在这里插入图片描述

2.3 代码

  1. import speech
  2. #自动体系
  3. speech.say("要开始啦")#输入语音whileTrue:print(u"开始说话")
  4. say = speech.input()# 接收语音# speech.say("你说了" + say) # 说话
  5. speech.say(say)# 说话

在这里插入图片描述

  1. import speech
  2. whileTrue:
  3. say = speech.input()# 接收语音
  4. speech.say("you said:"+say)#说话if say =="你好":
  5. speech.say("How are you?")elif say =="天气":
  6. 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 安装

  1. pip install pyttsx3

在这里插入图片描述

3.3 代码

  1. #coding=utf-8import pyttsx3
  2. pyttsx3.speak("Hello World!")
  3. pyttsx3.speak("持续推动我国经济实现质的有效提升和量的合理增长")
  1. import pyttsx3
  2. engine = pyttsx3.init()
  3. engine.say("I will speak this text")
  4. engine.runAndWait()
  1. import pyttsx3
  2. engine = pyttsx3.init()# object creation""" RATE"""
  3. rate = engine.getProperty('rate')# getting details of current speaking rateprint(rate)#printing current voice rate
  4. engine.setProperty('rate',125)# setting up new voice rate"""VOLUME"""
  5. volume = engine.getProperty('volume')#getting to know current volume level (min=0 and max=1)print(volume)#printing current volume level
  6. engine.setProperty('volume',1.0)# setting up volume level between 0 and 1"""VOICE"""
  7. voices = engine.getProperty('voices')#getting details of current voice#engine.setProperty('voice', voices[0].id) #changing index, changes voices. o for male
  8. engine.setProperty('voice', voices[1].id)#changing index, changes voices. 1 for female
  9. engine.say("Hello World!")
  10. engine.say('My current speaking rate is '+str(rate))
  11. engine.runAndWait()
  12. engine.stop()"""Saving Voice to a file"""# On linux make sure that 'espeak' and 'ffmpeg' are installed
  13. engine.save_to_file('Hello World','test.mp3')
  14. 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 安装

  1. pip install gTTS

在这里插入图片描述

4.3 代码

  1. gtts-cli 'hello'--output hello.mp3

  1. from gtts import gTTS
  2. tts = gTTS('hello')
  3. tts.save('hello.mp3')

在这里插入图片描述
执行失败了。

5、SAPI

SAPI是微软Speech API , 是微软公司推出的语音接口,而从WINXP开始,系统上就已经有语音识别的功能了,可是用武之地相当之少,他并没有给出一些人性化的自定义方案,仅有的语音操控命令显得相当鸡胁。
操作window dll的库,它可以实现很多功能,十分强大。

  1. import win32com.client
  2. speaker = win32com.client.Dispatch("SAPI.SpVoice")
  3. speaker.Speak("hello")

6、SpeechLib

comtypes依赖pyttsx3包。( comtypes Required-by: pyttsx3 )
安装:

  1. pip install comtypes

代码:

  1. from comtypes.client import CreateObject
  2. engine = CreateObject('SAPI.SpVoice')
  3. stream = CreateObject('SAPI.SpFileStream')from comtypes.gen import SpeechLib # 导这个包必须放在 上面3行代码 后面,否则运行时会报错。
  4. infile ='demo.txt'
  5. outfile ='demo_audio.wav'
  6. stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
  7. engine.AudioOutputStream = stream
  8. # 读取文本内容
  9. f =open(infile,'r', encoding='utf-8')
  10. theText = f.read()
  11. f.close()
  12. engine.speak(theText)
  13. 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
在这里插入图片描述

  1. pip install baidu_aip
  1. from aip import AipSpeech
  2. #用上面提到的APP ID, API Key和Secret Key替换
  3. APP_ID ='xxxx'
  4. API_KEY ='xxxx'
  5. SECRET_KEY ='xxxx'
  6. client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
  7. manual = r'百度你好! \
  8. 语音合成测试。'
  9. if __name__ =='__main__':print('start voice process')#语速spd: 5, 语调pit: 5, 播音per: 1
  10. result = client.synthesis(manual,'zh',1,{'vol':5,'per':1,})# 识别正确返回语音二进制 错误则返回dict 参照下面错误码ifnotisinstance(result,dict):withopen('audio.mp3','wb')as f:
  11. f.write(result)print('process end')

8、百度飞桨

  1. pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
  2. pip install paddlespeech -i https://pypi.tuna.tsinghua.edu.cn/simple
  3. paddlespeech tts --input'人工智能体验'--output test.wav
  4. paddlespeech asr --input test.wav
  5. paddlespeech asr --lang zh --input test.wav
  1. from paddlespeech.cli.asr.infer import ASRExecutor
  2. asr = ASRExecutor()
  3. result = asr(audio_file="test.wav")print(result)

结语

  1. 如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;

╮( ̄▽ ̄)╭

  1. 如果您感觉方法或代码不咋地

//(ㄒoㄒ)//

  1. ,就在评论处留言,作者继续改进;

o_O???

  1. 如果您需要相关功能的代码定制化开发,可以留言私信作者;

(✿◡‿◡)

  1. 感谢各位大佬童鞋们的支持!

( ´ ▽´ )ノ ( ´ ▽´)っ!!!


本文转载自: https://blog.csdn.net/hhy321/article/details/134900548
版权归原作者 爱看书的小沐 所有, 如有侵权,请联系我们删除。

“【小沐学Python】Python实现TTS文本转语音(speech、pyttsx3、百度AI)”的评论:

还没有评论