Python script designed to control drone using the DroneKit library while simultaneously utilizing the YOLO model for real-time object detection.
-
Library Imports:
cv2(OpenCV): Used for capturing video from the camera and displaying it.numpy: Image processing tasks.pydarknet: A Python wrapper for the Darknet YOLO implementation, used for object detection.dronekit: A library that allows communication and control of drones using the MAVLink protocol.
-
Drone Connection:
- The drone is connected via a serial connection (usually over USB) using
dronekit.connect.
- The drone is connected via a serial connection (usually over USB) using
-
YOLO Model Loading:
- The YOLO model is loaded using the
Detectorclass frompydarknet. Configuration files and weights are specified for the model.
- The YOLO model is loaded using the
-
Camera Initialization:
- OpenCV is used to capture video from the default camera (
cv2.VideoCapture(0)).
- OpenCV is used to capture video from the default camera (
-
Main Loop:
- The script enters a loop where it continuously captures frames from the camera.
- Each frame is passed through the YOLO object detector to identify objects within the frame.
-
Object Detection and Decision Making:
- For each detected object, if the object is classified as a "person," the script calculates the error in the x and y positions between the detected person’s center and the center of the frame.
- A simple proportional controller calculates the forward and right movements needed to center the detected person in the frame.
- These movements are then translated into control commands which are sent to the drone using
vehicle.channels.overrides.
-
Display and Cleanup:
- The frame with detections is displayed using OpenCV.
- The loop continues until the 'q' key is pressed.
- Finally, the script releases the camera, closes all OpenCV windows, and closes the connection to the drone.
The script aims to demonstrate a basic example of autonomous drone control based on real-time visual input. Specifically, it uses computer vision to detect a hotspot and adjusts the drone’s position to keep the hotspot centered in the camera's field of view.