树莓派控制语音控制舵机

原理很简单
首先调用百度语音识别API–>将识别结果转换为wav文件–>通过该文件控制舵机
话不多说,直接上代码
一、百度语音识别代码

#usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np
from datetime import datetime
import wave
import time
import urllib, urllib2, pycurl
import base64
import json
import os
import sys

reload(sys)
sys.setdefaultencoding( "utf-8" )

save_count = 0
save_buffer = []
t = 0
sum = 0
time_flag = 0
flag_num = 0
filename = 'asr.wav'
commun = '1'
answer = '1'

flag1=0
flag2=0
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

def get_token():
    apiKey = "Your API"
    secretKey = "Your Key"
    auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;
    res = urllib2.urlopen(auth_url)
    json_data = res.read()
    return json.loads(json_data)['access_token']

def dump_res(buf):
    #global duihua
    global res
    print "字符串类型"
    print buf
    a = eval(buf)
    print type(a)
    if a['err_msg']=='success.':
        res = a['result'][0] #可以在这里输出返回的语句
    else:
        res=""
        #print duihua

def use_cloud(token):
    fp = wave.open(filename, 'rb')
    nf = fp.getnframes()
    f_len = nf * 2
    audio_data = fp.readframes(nf)
    cuid = "9691607" #产品id
    srv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + token
    http_header = [
        'Content-Type: audio/pcm; rate=16000',
        'Content-Length: %d' % f_len
    ]
    c = pycurl.Curl()
    c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode
    #c.setopt(c.RETURNTRANSFER, 1)
    c.setopt(c.HTTPHEADER, http_header)   #must be list, not dict
    c.setopt(c.POST, 1)
    c.setopt(c.CONNECTTIMEOUT, 30)
    c.setopt(c.TIMEOUT, 30)
    c.setopt(c.WRITEFUNCTION, dump_res)
    c.setopt(c.POSTFIELDS, audio_data)
    c.setopt(c.POSTFIELDSIZE, f_len)
    c.perform() #pycurl.perform() has no return val

# 将data中的数据保存到名为filename的WAV文件中
def save_wave_file(filename, data):
    wf = wave.open(filename, 'wb')
    wf.setnchannels(1)
    wf.setsampwidth(2)
    wf.setframerate(SAMPLING_RATE)
    wf.writeframes("".join(data))
    wf.close()

token = get_token()

while (True):
    print "..............等待中1................"
    os.system('sudo arecord -D "plughw:1,0" -f S16_LE -d3 -r 16000 /home/pi/Desktop/asr.wav')
    use_cloud(token)
    print "-----> return result:"+commun[0]
    print "..............等待中2................"
    print res
    
    if "关闭舵机一" in res:
        print"好的,正在关闭舵机一"
        answer = '好的,关闭,请稍后'
        url = "http://tsn.baidu.com/text2audio?tex="+answer+"&lan=zh&per=0&pit=1&spd=7&cuid=9691607&ctp=1&tok=24.de7d08e3164eed77f61cfcd1b2c0.2592000.1499256984.282335-9729638"
        print "关闭中1"
        os.system('mplayer "%s"'%(url))
        print "关闭中2"
        os.system('python /home/pi/Desktop/control3.py')
        #execfile('/home/pi/Desktop/control3.py')
        print"............已成功关闭..............."


    if "打开舵机一" in res:
        print"正在打开舵机一"
        answer = '好的,正在为您开启,请稍后'
        url = "http://tsn.baidu.com/text2audio?tex="+answer+"&lan=zh&per=0&pit=1&spd=7&cuid=9691607&ctp=1&tok=24.de7d08e3164eed77f61cfcd1b2c0.2592000.1499256984.282335-9729638"
        os.system('mplayer "%s"'%(url))
        os.system('python /home/pi/Desktop/control1.py')
        # execfile('/home/pi/Desktop/control1.py')


    if "关闭舵机二" in res:
        print"好的,正在关闭舵机二"
        answer = '好的,关闭,请稍后'
        url = "http://tsn.baidu.com/text2audio?tex="+answer+"&lan=zh&per=0&pit=1&spd=7&cuid=9691607&ctp=1&tok=24.de7d08e3164eed77f61cfcd1b2c0.2592000.1499256984.282335-9729638"
        print "关闭中1"
        os.system('mplayer "%s"'%(url))
        print "关闭中2"
        os.system('python /home/pi/Desktop/control4.py')
        # execfile('/home/pi/Desktop/control4.py')
        print"............已成功关闭..............."

    if "打开舵机二" in res:
        print"正在打开舵机二"
        answer = '好的,正在为您关闭,请稍后'
        url = "http://tsn.baidu.com/text2audio?tex="+answer+"&lan=zh&per=0&pit=1&spd=7&cuid=9691607&ctp=1&tok=24.de7d08e3164eed77f61cfc8a7cd1b2c0.2592000.1499256984.282335-9729638"
        os.system('mplayer "%s"'%(url))
        os.system('python /home/pi/Desktop/control2.py')
        # execfile('/home/pi/Desktop/control2.py')

舵机控制代码
舵机开

#usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO  
import time  
import signal  
import atexit  
  
atexit.register(GPIO.cleanup)    
  
servopin = 27  
GPIO.setmode(GPIO.BCM)  
GPIO.setup(servopin, GPIO.OUT, initial=False)  
p = GPIO.PWM(servopin,50) #50HZ  
p.start(0)  
time.sleep(2)  
  
for i in range(0,200,60):  
    p.ChangeDutyCycle(2.5 + 10 * i / 180) 
    time.sleep(0.02)                    
    p.ChangeDutyCycle(0)                 
    time.sleep(0.2)


exit()#退出文件

舵机关

#usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO  
import time  
import signal  
import atexit  
  
atexit.register(GPIO.cleanup)    
  
servopin = 17  
GPIO.setmode(GPIO.BCM)  
GPIO.setup(servopin, GPIO.OUT, initial=False)  
p = GPIO.PWM(servopin,50) #50HZ  
p.start(0)  
time.sleep(2)  
  
for i in range(0,181,90):  
    p.ChangeDutyCycle(2.5 + 10 * i / 180) 
    time.sleep(0.02)                    
    p.ChangeDutyCycle(0)                 
    time.sleep(0.2)  

exit()#退出文件

对于代码这里有更大神写了更详细的说明
树莓派实现语音识别与语音合成——百度云语音识别API(离线)
手把手教你做树莓派语音识别
在这里插入图片描述

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值