700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python 语音识别(百度api)

python 语音识别(百度api)

时间:2020-10-17 12:38:43

相关推荐

python 语音识别(百度api)

文章目录

前言准备下载库代码实现以及编写我的key语音的录入:使用百度语音作为STT引擎并通过pyttsx3函数将返回的内容读出来形成简单的对话完整代码运行结果

前言

现在语音助手已经成为我们生活中ai的代表,下面让我们看一下语音助手的语音输入功能

准备

让我们搜索一下百度的开放平台

进入百度开放平台后选择短语音识别

然后去领取免费的资源

创建应用

记住相关的key

下载库

本次用到的库(以及引入方法):

import speech_recognition as srfrom aip import AipSpeechimport pyttsx3

代码实现以及

编写我的key

APP_ID = '27468776'API_KEY = 'ygQgtR4DIh14lDi8gwSMCm7o'SECRET_KEY = '7flGlZba42ZYxHudy3dSQ8YW4n89W5hc'client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

语音的录入:

try:def rec(rate=16000):r = sr.Recognizer()with sr.Microphone(sample_rate=rate) as source:print("请说话")audio = r.listen(source)with open("recording.wav", "wb") as f:f.write(audio.get_wav_data())except:print("语音录入发生错误")

使用百度语音作为STT引擎并通过pyttsx3函数将返回的内容读出来形成简单的对话

with open('recording.wav', 'rb') as f:audio_data = f.read()result = client.asr(audio_data, 'wav', 16000, {'dev_pid': 1536,})try:result_text = result["result"][0]print("你说: " + result_text)if result_text =="于金龙":engine = pyttsx3.init()engine.say('真帅')engine.runAndWait()elif result_text=="张新凯":engine = pyttsx3.init()engine.say('真难看')engine.runAndWait()except:print("没有识别到语音")

完整代码

import pyttsx3import speech_recognition as srfrom aip import AipSpeech# Baidu Speech API, replace with your personal keyAPP_ID = '27468776'API_KEY = 'ygQgtR4DIh14lDi8gwSMCm7o'SECRET_KEY = '7flGlZba42ZYxHudy3dSQ8YW4n89W5hc'client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)# Use SpeechRecognition to recordtry:def rec(rate=16000):r = sr.Recognizer()with sr.Microphone(sample_rate=rate) as source:print("请说话")audio = r.listen(source)with open("recording.wav", "wb") as f:f.write(audio.get_wav_data())except:print("语音录入发生错误")# 使用百度语音作为STT引擎def listen():with open('recording.wav', 'rb') as f:audio_data = f.read()result = client.asr(audio_data, 'wav', 16000, {'dev_pid': 1536,})try:result_text = result["result"][0]print("你说: " + result_text)if result_text =="阿龙":engine = pyttsx3.init()engine.say('真帅')engine.runAndWait()elif result_text=="其他人":engine = pyttsx3.init()engine.say('真难看')engine.runAndWait()except:print("没有识别到语音")while True:rec()request = listen()

运行结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。