Detect Drones in Real Time Using YOLOv8 and Google Colab

Vipin
3 min readNov 3, 2023
Photo by Alessio Soggetti on Unsplash

The world of computer vision, object detection has seen remarkable advancements, and YOLO (You Only Look Once) is at the forefront of this revolution. In this tutorial, I’ll demonstrate how to detect drones in real time using YOLOv8, a popular and efficient variant of YOLO, within the Google Colab environment.

Step 0: Prepare the data and upload to Google Drive

A freely accessible drone dataset that includes images and corresponding labels can be found via the Kaggle link provided below.

Arrange the dataset into the specified folder structure, compress it into a zip file, and subsequently upload it to Google Drive.

  • The images directory contains subfolders train and valid for training and validation images, respectively.
  • The labels directory contains subfolders train and valid for training and validation labels in text format.

The provided code snippet is used to extract the contents of a ZIP file on Google Drive using Python

import zipfile
with zipfile.ZipFile("/content/gdrive/MyDrive/DroneDataset.zip", "r") as zip_ref:
zip_ref.extractall("/content/gdrive/MyDrive/DroneDataset")

Step 1: Set Up Google Colab

Google Colab provides free access to GPU resources. First, we need to mount Google Drive to access our data and store our results.

from google.colab import drive
drive.mount('/content/gdrive')

Step 2: Install Required Libraries

Install the necessary libraries, including Ultralytics for YOLOv8 and GPU information.

!nvidia-smi
!pip install ultralytics==8.0.28

Step 3: Import Ultralytics and Check Installation

Import Ultralytics and check if the installation is successful.

from ultralytics import YOLO
ultralytics.checks()

Step 4: Train the YOLOv8 Model

To detect drones in real time, you can start by training a YOLOv8 model. Create a data.yaml file that specifies your dataset and model configuration.

Then, initiate training using your data.

!yolo task=detect mode=train model=yolov8s.pt data=/content/gdrive/MyDrive/DroneDataset/data.yaml epochs=3 plots=True

Step 5: Perform Real-Time Drone Detection

Now that your YOLOv8 model is trained, you can perform real-time drone detection. Specify your model and the source (e.g., a video file).

from ultralytics import YOLO
# Load the trained YOLOv8 model

model = YOLO('/content/runs/detect/train/weights/best.pt')
# Specify the path to the video file
source = '/content/gdrive/MyDrive/video.mp4'

# Run inference on the source
results = model(source, save=True)

The predicted results will be located in the directory path: `content/runs/detect/predict`.

Conclusion

With the power of YOLOv8 and the convenience of Google Colab, real-time detection becomes accessible and efficient. Whether it's for surveillance, tracking, or any other application, YOLOv8 is a valuable tool in your computer vision arsenal. Happy detecting!

Now you have the tools and knowledge to detect drones in real time using YOLOv8 and Google Colab. This tutorial provides a comprehensive guide to get you started on your drone detection journey. Have fun experimenting and exploring the world of computer vision!

--

--

Vipin

AI enthusiast and Machine Learning Engineer 🚀⚙️. Sharing thoughts and lessons learned!