基于python通过语音控制Tello edu

这几天大家都在弄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官方代码

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
此代码需要安装tellopy库和opencv-python库。 ```python import time import cv2 import tellopy # 定义手势识别函数 def gesture_recognition(): # 打开电脑摄像头 cap = cv2.VideoCapture(0) # 读取手势识别模型 gesture = cv2.CascadeClassifier("gesture.xml") while True: # 读取摄像头中的图像 ret, frame = cap.read() # 灰度化处理 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 进行手势检测 hands = gesture.detectMultiScale(gray, 1.3, 5) # 绘制矩形框 for (x, y, w, h) in hands: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # 显示图像 cv2.imshow("gesture recognition", frame) # 按下q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() cv2.destroyAllWindows() # 定义tello无人机控制函数 def tello_control(): # 创建tello对象 drone = tellopy.Tello() # 连接tello无人机 drone.connect() # 启动视频流传输 drone.start_video() # 等待无人机准备就绪 time.sleep(1) # 打开电脑摄像头 cap = cv2.VideoCapture(0) # 读取手势识别模型 gesture = cv2.CascadeClassifier("gesture.xml") # 设置飞行方向 direction = "" while True: # 读取摄像头中的图像 ret, frame = cap.read() # 灰度化处理 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 进行手势检测 hands = gesture.detectMultiScale(gray, 1.3, 5) # 绘制矩形框 for (x, y, w, h) in hands: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # 判断手势 if w > h: direction = "left" if x < 200 else "right" if x > 400 else "" else: direction = "up" if y < 150 else "down" if y > 350 else "" # 显示图像 cv2.imshow("tello control", frame) # 按下q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 控制tello无人机飞行 if direction == "left": drone.left(30) elif direction == "right": drone.right(30) elif direction == "up": drone.up(30) elif direction == "down": drone.down(30) else: drone.hover() # 停止视频流传输 drone.stop_video() # 断开连接 drone.disconnect() # 释放资源 cap.release() cv2.destroyAllWindows() if __name__ == '__main__': # 执行手势识别函数 # gesture_recognition() # 执行tello无人机控制函数 tello_control() ``` 在代码中,手势识别函数使用OpenCV库进行手势检测,识别手势后显示在电脑屏幕上,通过判断手势的方向,将其转换为对tello无人机的控制指令。 tello无人机控制函数通过连接tello无人机,启动视频流传输,打开电脑摄像头,读取手势识别模型,检测手势方向,并将其转换为对tello无人机的控制指令。最后,停止视频流传输,断开连接,释放资源。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值