【手把手教程】大疆Tello无人机+YOLOv5目标检测实战 | 附完整代码与演示视频

在这里插入图片描述

项目gitee仓库完整代码:https://gitee.com/wsy-yjys/yolov5_-tello_-demo
B站视频demo:https://www.bilibili.com/video/BV1ZvytYZEqM/

一、环境配置注意事项

  1. robomaster适配python版本:3.6.6-3.8.9之间
    在这里插入图片描述
  2. YOLOv5适配python版本:3.8.0以上
    在这里插入图片描述
    因此,项目环境python版本最好在3.8.0-3.8.9之间

二、代码使用注意事项

  1. 运行前,已连接Tello无人机的WIFI,并确保公用网络的防火墙已关闭!
  2. self.model = 替换为自己的模型路径!

三、视频demo核心代码如下

import threading
import time
import cv2
import torch
import robomaster
from robomaster import robot
from robomaster.battery import TelloBattery as Battery
from queue import Queue


"""注意事项:
1. 运行前,已连接Tello无人机的WIFI,并确保公用网络的防火墙已关闭!
2. self.model = 替换为自己的模型路径!
"""

# For “NotImplementedError: cannot instantiate 'PosixPath' on your system" bug
import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
robomaster.config.LOCAL_IP_STR = "192.168.10.2"


class Tello():
    def __init__(self,):
        """Initializes the YOLOv5 Detections class with image info, predictions, filenames, timing and normalization."""
        super().__init__()
        # 模型初始化
        self.model = torch.hub.load(r'./', 'custom', path=r"runs/train/exp4/weights/best.pt", source="local")  # 替换为自己的模型路径)

        # 无人机初始化
        self.drone = robot.Drone()
        self.drone.initialize()
        # 电池初始化
        self.battery = Battery(self.drone)
        # 相机初始化
        self.camera = self.drone.camera
        self.camera.stop_video_stream()
        self.camera.start_video_stream(display=False)
        self.camera.set_fps("high")
        self.camera.set_resolution("high")
        self.camera.set_bitrate(6)

    def infer(self):
        # 使用YOLOv5模型进行推理tello回传的视频流
        img = self.camera.read_cv2_image(strategy="newest")
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 指定视频编码器
        video_writer = cv2.VideoWriter('output.mp4', fourcc, 30.0, (img.shape[1], img.shape[0]))  # 创建视频写入对象
        while True:
            img = self.camera.read_cv2_image(strategy="newest")    #直接读取最新帧,采用="pipeline"会在第二次启动时卡死
            results = self.model(img)     # Inference
            # results.print()
            image = results.show_cv()
            cv2.imshow('results', image)
            # 写入当前帧到视频文件
            video_writer.write(image)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            if not q.empty() and q.get()["close"]:
                break
        video_writer.release()
        cv2.destroyAllWindows()
        print("infer end")

    def get_verson(self):
        # 获取飞机版本信息
        print("Drone sdk version: {0}".format(self.drone.get_sdk_version()))
        # 获取飞机SN信息
        print("drone sn: {0}".format(self.drone.get_sn()))

    def get_battery(self):
        # 获取飞机电量信息
        print("Battery: {}%".format(self.battery.get_battery()))

    def close(self):
        # 结束:释放资源
        self.camera.stop_video_stream()
        self.drone.close()


if __name__ == "__main__":
    q = Queue(100)
    # 初始化
    tello = Tello()
    tello.get_battery()

    # YOLO推理线程
    recv_thread = threading.Thread(target=tello.infer)
    recv_thread.daemon = True
    recv_thread.start()

    # 主线程:用于实现预定动作
    time.sleep(6)
    q.put({"close":True})

    # 结束
    time.sleep(1)      # 延迟1s,确保两个线程都正常结束
    tello.close()

注意:这只是核心代码,项目完整代码请见gitee仓库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只云卷云舒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值