为编程爱好者分享易语言教程源码的资源网
好用的代理IP,游戏必备 ____广告位招租____ 服务器99/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器
好用的代理IP,游戏必备 ____广告位招租____ 服务器低至38/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器

网站首页 > 网络编程 > 其它综合 正文

利用Python和API轻松实现:语音转文字,文字转语音

三叶资源网 2022-10-04 19:18:50 其它综合 446 ℃ 0 评论

利用Python和API轻松实现:语音转文字,文字转语音。

语音转文字(支持实时麦克风输入和从音频文件读入):

  • 百度 API
  • 科大讯飞 API
  • SpeechRecognition (CMU PocketSphinx)

文字转语音:

  • pyttsx3

Environment

  • Python 3.6.7
  • MacOS(以下环境配置方式均基于Mac系统,其他系统的配置方式可能会有一些不同)

Speech to Text

百度

在 https://cloud.baidu.com/product/speech 申请API。

文档:http://ai.baidu.com/docs#/ASR-API

Configuration

安装:

pip install baidu-aip

在 speech_to_text_baidu() 中填入APPID、API_KEY、SECRET_KEY:

APP_ID = ""
API_KEY = ""
SECRET_KEY = ""

(也可以直接使用REST API:Demo

Usage

from Speech_and_Text import speech_to_text_baidu
# 从文件读入
speech_to_text_baidu(audio_path = "path_of_audio", if_microphone = False)
# 从麦克风读入
speech_to_text_baidu(if_microphone = True)

科大讯飞

在 https://www.xfyun.cn/services/voicedictation 申请API。

文档:https://doc.xfyun.cn/rest_api/index.html

Configuration

在 speech_to_text_ifly() 填入 APPID、API_KEY:

URL = "http://api.xfyun.cn/v1/service/v1/iat"
APPID = ""
API_KEY = ""

要在讯飞管理面板中添加调用方api,否则会报错。

Usage

from Speech_and_Text import speech_to_text_ifly
# 从文件读入
speech_to_text_ifly(audio_path = "path_of_audio", if_microphone = False)
# 从麦克风读入
speech_to_text_ifly(if_microphone = True)

SpeechRecognition

使用了Python的语音识别库 SpeechRecognition

源码:https://github.com/Uberi/speech_recognition

Configuration

SpeechRecognition

安装:

pip install SpeechRecognition
PyAudio

使用麦克风进行输入

主页:http://people.csail.mit.edu/hubert/pyaudio/

# Mac上的安装方式

xcode-select --install	# 安装xcode, 已经装好的的话,执行的时候会提示

# 先用homebrew安装portaudio(pyaudio需要的库),否则会提示:'portaudio.h' file not found
brew remove portaudio	# 先用homebrew卸载
brew install portaudio	# 重新安装

sudo pip install pyaudio	# 安装pyaudio

Reference: https://stackoverflow.com/questions/33851379/pyaudio-installation-on-mac-python-3

PocketSphinx

CMU Sphinx 是卡内基梅隆大学开发的开源语音识别引擎,可以离线工作,支持多种语言(包括中文)。

源码:https://github.com/cmusphinx

PocketSphinx 是 CMU Sphinx 的 Python 封装接口。

源码:https://github.com/cmusphinx/pocketsphinx-python

安装:

pip install PocketSphinx

添加中文语言包:

查看 SpeechRecognition 包的安装路径('/path'):

python -c "import speech_recognition as sr, os.path as p; print(p.dirname(sr.__file__))"

然后下载并解压 Mandarin Chinese 语言包,把 zh-CN 文件夹放入 '/path/pocketsphinx-data' 中

Usage

from Speech_and_Text import speech_to_text_cmu
# 从文件读入
speech_to_text_cmu(audio_path = "path_of_audio", if_microphone = False)
# 从麦克风读入
speech_to_text_cmu(if_microphone = True)

Text to Speech

使用了Python的文字转语音库 pyttsx3

源码:https://github.com/nateshmbhat/pyttsx3

文档:https://pyttsx3.readthedocs.io

Configuration

pip install pyttsx3
pip install pyobjc # 依赖模块

Usage

from Speech_and_Text import text_to_speech
# Example
text_to_speech(sentence = "人类的本质是复读机")


源码下载地址:https://github.com/Renovamen/Speech-and-Text/archive/master.zip

来源:三叶资源网,欢迎分享,公众号:iisanye,(三叶资源网⑤群:21414575

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

百度站内搜索
关注微信公众号
三叶资源网⑤群:三叶资源网⑤群

网站分类
随机tag
伊对APP模块RFID读卡器滑块坐标识别QQ消息后台发送B站弹幕助手源码Tencent企点加好友qzonetoken易语言CEF3模块登陆JS图像批量缩放工具多线程进度下载GDI图像处理屏幕取色取电脑硬件信息淘宝登录EXUI例程会员系统截图模块源码数学函数图像
最新评论