树莓派c语言 智能车,人工智能-树莓派小车(4)——通过语音玩转智能小车

提示

今年注册的账号可能百度,改变了下发的数据格式,不再是文本格式,是以链接的形式。具体的我还没有研究。大家可以看返回的数据,payload里面是text还是url,是text 的可以用我这个方法,url的暂时还不行,等我有时间看一看如何提取,会再更新的

本文是基于百度的DuerOS Python SDK进行的,具体安装以及实现过程,可以看我的这篇教程:人工智能-树莓派小车(1)——DuerOS语音唤醒,要实现的功能有:

语音聊天:可以跟智能小车进行对话,询问现在的天气,播放想听的歌曲等等基本对话;

语音控制:可以通过语音的操作方式控制智能小车的前进后退、避障转弯等功能,进一步可以扩展到家里的台灯、空调、窗帘等等家具;

语音反馈:在你发出命令后,小车还会给出回应,与你互动,实现一个良好的反馈。

视频演示

实现了以上几个功能,做了一个演示,视频链接:通过语音玩转智能小车

核心代码

1. 命令读取

content = u'云端下发directive:%s' % (directive_content)

content里即为我们要读取的命令数据,是一种类JSON格式,所以有两种办法将数据读取出来。

采用JSON的字典格式

import json

with open('out.json','a') as f:

f.write(json.loads(content).decode('unicode-escape').encode('utf-8'))

f.close()

直接将数据保存为.txt格式

f = open("ord.txt",'a')

f.write(content)

logging.info(content)

2. 命令判断

读取到命令之后要对其进行检测,如果命令中包含一些特定的关键词,则做出相应的动作。

def recognize():

file = '/home/pi/Rascar-DuerOS-Python-Client/ord.txt' # 读取文件

f = open(file,'r')

out = f.read()

command = out.decode('unicode-escape').encode('utf-8')

print command

GPIO.output(LED_CTR, GPIO.LOW)

############################################

###############命令识别######################

############################################

if command.find(u"开") !=-1 and command.find(u"大") !=-1 and command.find(u"灯") !=-1:

print "打开前大灯"

robot.Open_Flight()

GPIO.output(LED_CTR, GPIO.HIGH)

shutil.copy("/home/pi/Rascar-DuerOS-Python-Client/app/resources/turn_on_light.mp3","/home/pi/Rascar-DuerOS-Python-Client/temp.mp3")

elif command.find(u"关") !=-1 and command.find(u"大") !=-1 and command.find(u"灯") !=-1:

print "关闭前大灯"

robot.Close_Flight()

GPIO.output(LED_CTR, GPIO.HIGH)

shutil.copy("/home/pi/Rascar-DuerOS-Python-Client/app/resources/turn_off_light.mp3","/home/pi/Rascar-DuerOS-Python-Client/temp.mp3")

elif command.find(u"前") !=-1 and command.find(u"进") !=-1:

print "前进"

robot.Motor_Forward()

time.sleep(2)

robot.Motor_Stop()

GPIO.output(LED_CTR, GPIO.HIGH)

shutil.copy("/home/pi/Rascar-DuerOS-Python-Client/app/resources/forward.mp3","/home/pi/Rascar-DuerOS-Python-Client/temp.mp3")

elif command.find(u"后") !=-1 and command.find(u"退") !=-1:

print "后退"

robot.Motor_Backward()

time.sleep(2)

robot.Motor_Stop()

GPIO.output(LED_CTR, GPIO.HIGH)

shutil.copy("/home/pi/Rascar-DuerOS-Python-Client/app/resources/backward.mp3","/home/pi/Rascar-DuerOS-Python-Client/temp.mp3")

elif command.find(u"左") !=-1 and command.find(u"转") !=-1:

print "左转"

robot.Motor_TurnLeft()

p = GPIO.PWM(11, 3)

p.start(20)

time.sleep(0.5)

p.stop()

robot.Motor_Stop()

GPIO.output(11, GPIO.LOW)

GPIO.output(LED_CTR, GPIO.HIGH)

shutil.copy("/home/pi/Rascar-DuerOS-Python-Client/app/resources/turn_left.mp3","/home/pi/Rascar-DuerOS-Python-Client/temp.mp3")

elif command.find(u"右") !=-1 and command.find(u"转") !=-1:

print "右转"

robot.Motor_TurnRight()

p = GPIO.PWM(8, 3)

p.start(20)

time.sleep(0.5)

p.stop()

robot.Motor_Stop()

GPIO.output(8, GPIO.LOW)

GPIO.output(LED_CTR, GPIO.HIGH)

shutil.copy("/home/pi/Rascar-DuerOS-Python-Client/app/resources/turn_right.mp3","/home/pi/Rascar-DuerOS-Python-Client/temp.mp3")

elif command.find(u"黑") !=-1 and command.find(u"线") !=-1:

print "黑线"

robot.TrackLine()

return

其中GPIO接口的设置以及初始化,见人工智能-树莓派小车-GPIO控制小车

最后要养成良好习惯,用完GPIO接口记得清理GPIO口

GPIO.cleanup()

3. 命令文件清空

为避免命令文件堆积,每次识别命令结束后,进行命令文件的清理,代码很简单。

f = open("ord.txt",'w')

f.truncate()

f.close()

4. 命令反馈

这部分集成到了第二部分命令判断中,判断完立即进行反馈。

要注意的是,在第二部分每一条命令都有一条这样的代码

GPIO.output(LED_CTR, GPIO.HIGH)

这一步正是屏蔽掉DuerOS原有的语音反馈的关键

首先录制好自己想要的话语,然后仿照我的代码放到对应的位置

shutil.copy("/home/pi/Rascar-DuerOS-Python-Client/app/resources/turn_right.mp3","/home/pi/Rascar-DuerOS-Python-Client/temp.mp3")

接下来是最关键的一步

在sdk/interface里面有一个speech_synthesizer.py文件,这个文件就是控制TTS语音输出的,在这对代码进行一定修改,就可以实现播放我们自定义的音频。

2.1

import RPi.GPIO as GPIO

LED_CTR = 5 #与上面端口一致,用于检测是否有控制指令发送

然后在speak函数加入一条检测指令

def speak(self, directive):

'''

播放TTS(云端directive name方法)

:param directive: 云端下发directive

:return:

'''

# directive from dueros may not have the dialogRequestId

if 'dialogRequestId' in directive['header']:

dialog_request_id = directive['header']['dialogRequestId']

if self.dueros.speech_recognizer.dialog_request_id != dialog_request_id:

return

self.token = directive['payload']['token']

url = directive['payload']['url']

if url.startswith('cid:'):

mp3_file = os.path.join(tempfile.gettempdir(), url[4:] + '.mp3')

if os.path.isfile(mp3_file):

self.finished.clear()

# os.system('mpv "{}"'.format(mp3_file))

if GPIO.input(LED_CTR) == 1 :

self.player.play('file://{}'.format('/home/pi/DuerOS-Python-Client/temp.mp3'))

os.remove("temp.mp3")

else :

self.player.play('file://{}'.format(mp3_file))

self.__speech_started()

self.dueros.state_listener.on_speaking()

# will be set at SpeechFinished() if the player reaches the End Of Stream or gets a error

self.finished.wait()

os.system('rm -rf "{}"'.format(mp3_file))

就可以啦

完整代码

所有的代码都上传到了我的GitHub上

系列教程

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值