这几天大家都在弄EP和Tello EDU,今天呢我就连讲一下怎么样控制语音控制tello EDU,要tello edu才行哦,tello是不行的,还有就是我们不用去看每一行代码是干嘛的,我们只用知道那个函数是干嘛就行了,我们用了会返回什么值,就可以了,话不多说,我们开始讲解吧。
首先我们应该知道语音控制tello的一个大概流程,我就大概讲一下
电脑录音 ——百度AI识别——识别结果筛选——筛选结果发送给tello——tello执行命令
一,电脑录音
对于电脑怎么样录音,请参考https://blog.csdn.net/qq_43603247/article/details/105520688,这里是我以前写的,我就不多讲了。
二,百度AI识别
对于怎么样识别我们的声音呢,这里我也不多讲,请参考https://blog.csdn.net/qq_43603247/article/details/105561515,这里还有一个问题,很多朋友问我,识别的不准怎么办?,我这里有两个建议,第一个就是在百度AI里面训练一下,把我们经常说的语句上传上去,第二个就是带耳机,当然大家可以大胆的尝试。
三,识别结果的筛选
这个是什么意思呢?,就是说我们说起飞,那么电脑进行比对,发出相应的指令,tello指令大全https://dlcdn.ryzerobotics.com/downloads/Tello/Tello_SDK_2.0_%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E.pdf,大家可以看一下,怎么样筛选呢,就是运用if语句,如果百度识别结果与我们设定的结果相同,那么就发送相应的指令,下面我就做个举例。
if result == "起飞。":
send("takeoff")
print("tello无人机起飞")
如果识别结果是起飞,那么就发送"takeoff"这个指令,就是这么简单,因为有很多指令,我大概写几个吧
def action():
if result == "开始。":
command = "command"
send ( command )
print("开始")
if result == "起飞。":
send("takeoff")
print("tello无人机起飞")
if result == "降落。":
send("land")
print("tello无人机降落")
if result == "向上。":
send("up 20") #upX,X等于多少,就向上飞多少
print("tello无人机向上飞")
if result == "向下。":
send("down 20")
print("tello无人机向下飞")
if result == "向左。":
send("left 20")
print("tello无人机向左飞")
if result == "向右。":
send("right 20") #所有值都是可以任意更改的
print("tello无人机向右飞")
if result == "向前。":
send("forward 20")
print("tello无人机向前飞")
if result == "向后。":
send("back 20")
print("tello无人机向后飞")
if result == "顺时针。":
send("cw 90")
print("tello无人机顺时针旋转")
if result == "逆时针。":
send("cw 90")
print("tello无人机逆时针旋转")
if result == "悬停。":
send("stop")
print("tello无人机悬停")
这就是识别结果筛选啦,记得在比对结果后面加上句号哦,不然会报错。
四,筛选结果发送个tello
要发送指令给tello,那么就要连接tello,把指令发送到相应的IP地址去,这里参考了大疆tello给出的代码
1,连接代码
tello_address = ('192.168.10.1', 8889)
# 本地计算机的IP和端口
local_address = ('', 9000)
# 创建一个UDP连接,我们将发送命令
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 绑定到本地地址和端口
sock.bind(local_address)
def receive():
# Continuously loop and listen for incoming messages,不断循环并侦听传入的消息
while True:
# Try to receive the message otherwise print the exception
try:
response, ip_address = sock.recvfrom ( 128 )
print ( " Tello回复讯息--> " + response.decode ( encoding='utf-8' ) )
except Exception as e:
# If there's an error close the socket and break out of the loop,如果出现错误,则关闭套接字并中断循环
sock.close ()
print ( " 接收到错误讯息--> " + str ( e ) )
break
#创建并启动一个在后台运行的监听线程
#这利用了我们的接收功能,并将持续监控收到的消息
receiveThread = threading.Thread(target=receive)
receiveThread.daemon = True
receiveThread.start()
# 将消息发送给Tello,允许几秒钟的延迟
def send(message):
# 尝试发送消息,否则打印异常
try:
sock.sendto(message.encode(), tello_address)
print("你输入的指令是: " + message)
except Exception as e:
print("传输错误: " + str(e))
看代码我们就知道send()函数就是我们的指令发送函数,所以我们要发送的指令就直接给到send()函数,例如发送起飞指令,那么就send(“takeoff”),大家都懂了吗?
五,tello执行命令
就是我们给起飞,无人机就起飞,我们给降落,无人机就降落。
六,全部示例代码
import pyaudio #导入pyAudio的源代码文件,我们下面要用到,不用到就不用导入啦
import wave
from aip import AipSpeech
from playsound import playsound
from tqdm import tqdm
import socket
import threading
import time
import sys
import re
APP_ID = 'XXXXXXXXXXX' #新建AiPSpeech
API_KEY = 'XXXXXXX'
SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXX'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
#连接tello
# IP和Tello港口
tello_address = ('192.168.10.1', 8889)
# 本地计算机的IP和端口
local_address = ('', 9000)
# 创建一个UDP连接,我们将发送命令
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 绑定到本地地址和端口
sock.bind(local_address)
print('请你输入语音')
def record(): #定义函数
CHUNK = 1024
FORMAT = pyaudio.paInt16 #量化位数
CHANNELS = 1 #采样管道数
RATE = 16000 #采样率
RECORD_SECONDS = 2
WAVE_OUTPUT_FILENAME = "output.wav" #文件保存的名称
p = pyaudio.PyAudio() #创建PyAudio的实例对象
stream = p.open(format=FORMAT, #调用PyAudio实例对象的open方法创建流Stream
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
frames = [] #存储所有读取到的数据
print('* 开始录音 >>>') #打印开始录音
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK) #根据需求,调用Stream的write或者read方法
frames.append(data)
print('* 结束录音 >>>') #打印结束录音
stream.close() #调用Stream的close方法,关闭流
p.terminate() #调用pyaudio.PyAudio.terminate() 关闭会话
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') #写入wav文件里面
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def cognitive(): #读取文件
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
result = client.asr(get_file_content('output.wav'), 'wav', 16000, {
'dev_pid': 1537, #识别本地文件
} )
result_text = result["result"][0]
print("you said: " + result_text)
return result_text
def receive():
# Continuously loop and listen for incoming messages,不断循环并侦听传入的消息
while True:
# Try to receive the message otherwise print the exception
try:
response, ip_address = sock.recvfrom ( 128 )
print ( " Tello回复讯息--> " + response.decode ( encoding='utf-8' ) )
except Exception as e:
# If there's an error close the socket and break out of the loop,如果出现错误,则关闭套接字并中断循环
sock.close ()
print ( " 接收到错误讯息--> " + str ( e ) )
break
#创建并启动一个在后台运行的监听线程
#这利用了我们的接收功能,并将持续监控收到的消息
receiveThread = threading.Thread(target=receive)
receiveThread.daemon = True
receiveThread.start()
# 将消息发送给Tello,允许几秒钟的延迟
def send(message):
# 尝试发送消息,否则打印异常
try:
sock.sendto(message.encode(), tello_address)
print("你输入的指令是: " + message)
except Exception as e:
print("传输错误: " + str(e))
def action():
if result == "开始。":
command = "command"
send ( command )
print("开始")
if result == "起飞。":
send("takeoff")
print("tello无人机起飞")
if result == "降落。":
send("land")
print("tello无人机降落")
if result == "向上。":
send("up 20") #upX,X等于多少,就向上飞多少
print("tello无人机向上飞")
if result == "向下。":
send("down 20")
print("tello无人机向下飞")
if result == "向左。":
send("left 20")
print("tello无人机向左飞")
if result == "向右。":
send("right 20") #所有值都是可以任意更改的
print("tello无人机向右飞")
if result == "向前。":
send("forward 20")
print("tello无人机向前飞")
if result == "向后。":
send("back 20")
print("tello无人机向后飞")
if result == "顺时针。":
send("cw 90")
print("tello无人机顺时针旋转")
if result == "逆时针。":
send("cw 90")
print("tello无人机逆时针旋转")
if result == "悬停。":
send("stop")
print("tello无人机悬停")
else:
pass
while(True):
record() #录音模块
result = cognitive() #百度识别结果
action()
参考文档
大疆tello官方代码