大体思路:
- 用Python做服务器
- ESP8266WIFI模块接受服务器信息,
- 麦克风录制语音上传百度识别,识别后返回结果
- 服务器判断返回的结果并发出相应的指令信息(让车前进、后退等)给ESP8266
- ESP8266通过串口通信再给Arduino发射信息控制小车
所需物品:
- Arduino主板及组装小车所需主板(item.taobao.com/item.htm)
- WIFI模块,ESP8266(item.taobao.com/item.htm)
- 杜板线多条
- 麦克风
一:按照教程组装好小车,此时小车有寻迹、超声波避障、蓝牙遥控
二:ESP8266烧入程序(zhongbest.com/2017/01/02/esp8266-01%E6%97%A0%E7%BA%BF%E6%A8%A1%E5%9D%97%E7%9A%84arduino%E7%83%A7%E5%86%99%E6%96%B9%E5%BC%8F/)
三:Python编写服务器
1. 语音录制
2. 语音识别
3. 服务器
语音录制(record.py):
import wave
from pyaudio import PyAudio,paInt16
from speech import speechChina
framerate=8000
NUM_SAMPLES=2000
channels=1
sampwidth=2
TIME=2
def save_wave_file(filename,data):
'''save the date to the wavfile'''
wf=wave.open(filename,'wb')
wf.setnchannels(channels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(b"".join(data))
wf.close()
def my_record():
pa=PyAudio()
stream=pa.open(format = paInt16,channels=1,
rate=framerate,input=True,
frames_per_buffer=NUM_SAMPLES)
my_buf=[]
count=0
while count<TIME*3:#控制录音时间
string_audio_data = stream.read(NUM_SAMPLES)
my_buf.append(string_audio_data)
count+=1
print('.')
save_wave_file('record/01.wav',my_buf)
stream.close()
chunk=2014
def play():
wf=wave.open(r"record/01.wav",'rb')
p=PyAudio()
stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=
wf.getnchannels(),rate=wf.getframerate(),output=True)
while True:
data=wf.readframes(chunk)
if data=="":break
stream.write(data)
stream.close()
p.terminate()
def record():
# while True:
isPass = input("开始录音(时间5秒):")
if isPass == '0':
print("录音")
my_record()
print("语音识别")
return speechChina()
else:
return "前进"
语音识别(speech.cy):
- 百度AI开放平台注册开发者账号获取APP_ID,API_KEY,SECRET_KEY
- 下载Python语音识别SDK
from aip import AipSpeech
import json
APP_ID = '997'
API_KEY = '7yopVC8Y7WlRq1VnU'
SECRET_KEY = 'P33gdGkbuBByrSjquk'
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = aipSpeech.synthesis('你好阿里巴巴', 'zh', 1, {
'vol': 5,
})
# 读取文件
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
print(filePath)
return fp.read()
def speechChina():
results = aipSpeech.asr(get_file_content('record/01.wav'), 'wav', 8000, {
'lan': 'zh',
})
print(results['result'])
return results['result'][0]
if __name__ == '__main__':
results = speechChina()
print(results)
服务器(service.py):
# 导入 socket、sys 模块
import socket
import sys
# 创建 socket 对象
from record import record
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
# 获取本地主机名
host = socket.gethostname()
port = 8866
# 绑定端口
serversocket.bind((host, port))
# 设置最大连接数,超过后排队
serversocket.listen(5)
while True:
# 建立客户端连接
print("进来了")
clientsocket, addr = serversocket.accept()
print(addr)
while True:
# accept_data = clientsocket.recv(8024)## conn.recv()接收客户端的内容,接收到的是bytes类型数据
# accept_data2 = str(accept_data, encoding="utf8") # str(data,encoding="utf8")用“utf8”进行解码
# if accept_data2 == 'byebye':
# break
# print("".join(("接收内容:", accept_data2, "客户端口:", str(addr[1]))))
#send_data = input("输入发送内容:")
control = record()
if control.find(u"前") != -1:
control = 'A'
print('前进')
elif control.find(u"后") != -1:
control = 'B'
print('后退')
elif control.find(u"左") != -1:
control = 'C'
print('左边')
elif control.find(u"右") != -1:
control = 'D'
print('右边')
elif control.find(u"停") != -1:
control = 'E'
print('停止')
else:
control = 'A'
clientsocket.sendall(bytes(control, encoding="utf8")) # 发送内容必须为bytes类型数据,bytes(data, encoding="utf8")用“utf8”格式进行编码
clientsocket.close()
print("关闭了吗?")