树莓派智能音箱

树莓派智能音箱

树莓派智能音箱

制作智能音箱首先理清思路,先实现语音识别,其次语音合成,然后实现交互功能,最后导入树莓派

一、语音识别

首先进行安装插件

树莓派智能音箱

; 在进行安装python SDK

树莓派智能音箱
代码如下:
树莓派智能音箱

二、语音合成

首先注册百度云账号,获取

树莓派智能音箱

该步原理,首先进行录音,然后识别,反馈到百度云,然后在接受反馈,调用百度云,完成合成

代码如下:

def record_audio():

录音

CHUNK = 1024

FORMAT = pyaudio.paInt16

CHANNELS = 1 # 通道数

RATE = 16000

RECORD_SECONDS = 5 # 录音时间

WAVE_OUTPUT_FILENAME = “record.wav”

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT, channels=CHANNELS,

​ rate=RATE, input=True,

​ frames_per_buffer=CHUNK)

print(“* recording”)

frames = []

for i in range(0, int(RATE/CHUNK*RECORD_SECONDS)):

​ data = stream.read(CHUNK)

​ frames.append(data)

print(“* done recording”)

stream.stop_stream()

stream.close()

p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, ‘wb’)

wf.setnchannels(CHANNELS)

wf.setsampwidth(p.get_sample_size(FORMAT))

wf.setframerate(RATE)

wf.writeframes(b”.join(frames))

wf.close()

print(“done play”)

if name==” main“:

record_audio()

play_audio()

type_audio()

def get_file_content(filePath):

with open(filePath, ‘rb’) as fp:

​ return fp.read()

result=client.asr(get_file_content(‘record.wav’), ‘wav’, 16000, {

‘dev_pid’: 1537,

})

print(result[“result”][0])

def get_token():

server = “https://aip.baidubce.com/oauth/2.0/token?”

grant_type = “client_credentials”

client_id = ‘IQisWv4tqQoKg3Ui0YFObmZ6’

client_secret = ‘QpkAvrW6sj2HWqeUKubTP9KFpyC4iGkZ’

url = “%sgrant_type=%s&client_id=%s&client_secret=%s” % (

​ server, grant_type, client_id, client_secret)

res = requests.post(url)

access_token = json.loads(res.text)[“access_token”]

return access_token

access_token = get_token()

print(access_token)

即为整个代码

; 三、语音交互

首先申请百度云智能机器人,记住机器人ID

树莓派智能音箱
树莓派智能音箱

然后编写代码,引用机器人

url = ‘https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token=’ + access_token

post_data = “{“version”:”3.0″,”service_id”:”S70378″,”session_id”:””,”log_id”:”26378884″,”request”:{“terminal_id”:”88888″,”query”:”‘”+result[“result”][0]+”‘”}} “

headers = {‘content-type’: ‘application/x-www-form-urlencoded’}

response = requests.post(url, data=post_data.encode(‘utf-8’), headers=headers)

text1 = json.loads(response.text)

print(text1)

print(text1[‘result’][‘responses’][0][‘actions’][0][‘say’])

rs= client.synthesis(text1[‘result’][‘responses’][0][‘actions’][0][‘say’], ‘zh’, 1, {‘vol’: 5,’per’:0

})

识别正确返回语音二进制 错误则返回dict 参照下面错误码

if not isinstance(rs, dict):

with open(‘record.mp3’, ‘wb’) as f:

​ f.write(rs)

import mp3play

clip =mp3play.load(“record.mp3”)

clip.play()

duration = clip.milliseconds()

import time

time.sleep(duration)

clip.stop()

print(rs)

print(rs)

其中ID是你自己的机器人代号

最后导入树莓派

将树莓派连上喇叭,麦克风,即可完成树莓派智能音箱的制作
(写的不好,见谅)

Original: https://blog.csdn.net/asdrjll/article/details/125256458
Author: isisss
Title: 树莓派智能音箱

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/515962/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球