Skip to content

AshutoshSharma-pixel/Counter-Drone-Detection-Tracking

Repository files navigation

Counter-Drone Detection and Tracking System

A deep learning-based system for detecting and tracking drones in video feeds using YOLOv8 Nano and PyTorch, optimized for Apple Silicon (M4 chip) with MPS support.

πŸ“‹ Overview

This project implements a real-time drone detection and tracking system using:

  • YOLOv8 Nano - Lightweight object detection model
  • PyTorch with MPS - Optimized for Apple Silicon
  • Norfair Tracker - Multi-object tracking (optional)
  • OpenCV - Video processing and visualization

πŸš€ Features

  • βœ… Real-time drone detection in video/webcam feeds
  • βœ… Multi-object tracking with unique IDs
  • βœ… Optimized for Apple Silicon (MPS backend)
  • βœ… Configurable confidence thresholds
  • βœ… Support for video files and webcam input
  • βœ… Clean, modular codebase with detailed comments

πŸ“ Project Structure

Counter Drone Detection & Tracking/
β”œβ”€β”€ train_model.py          # Training script
β”œβ”€β”€ detect_video.py         # Detection script (video/webcam)
β”œβ”€β”€ track_video.py          # Tracking script with Norfair
β”œβ”€β”€ organize_dataset.py     # Dataset organization script
β”œβ”€β”€ test_camera.py          # Camera test utility
β”œβ”€β”€ create_test_video.py    # Create test video from dataset
β”œβ”€β”€ data.yaml               # Dataset configuration
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ README.md              # This file
β”œβ”€β”€ Drone Dataset/
β”‚   └── drone_dataset_yolo/
β”‚       β”œβ”€β”€ images/
β”‚       β”‚   β”œβ”€β”€ train/
β”‚       β”‚   └── valid/
β”‚       └── labels/
β”‚           β”œβ”€β”€ train/
β”‚           └── valid/
└── runs/
    └── train/
        └── counter_drone/
            └── weights/
                β”œβ”€β”€ best.pt
                └── last.pt

πŸ› οΈ Installation

1. Prerequisites

  • Python 3.10 or higher
  • macOS with Apple Silicon (M1/M2/M3/M4)
  • VS Code (recommended)

2. Create Virtual Environment

# Navigate to project directory
cd "Counter Drone Detection & Tracking"

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
source venv/bin/activate

# IMPORTANT: Make sure you see (venv) in your prompt
# If you see (base) or other conda environments, deactivate them first:
# conda deactivate  # If using conda

3. Install Dependencies

# Make sure virtual environment is activated (you should see (venv) in prompt)
# If not, activate it: source venv/bin/activate

# Upgrade pip
pip install --upgrade pip

# Install PyTorch with MPS support (for Apple Silicon)
pip install torch torchvision

# Install other dependencies (this will install NumPy < 2.0 for compatibility)
pip install -r requirements.txt

# Verify NumPy version (should be < 2.0)
python -c "import numpy; print('NumPy version:', numpy.__version__)"

4. Verify Installation

# Check PyTorch MPS availability
python3 -c "import torch; print('MPS available:', torch.backends.mps.is_available())"

πŸ“Š Dataset Setup

Dataset Structure

Ensure your dataset follows this structure:

Drone Dataset/drone_dataset_yolo/
β”œβ”€β”€ images/
β”‚   β”œβ”€β”€ train/          # Training images
β”‚   └── valid/          # Validation images
└── labels/
    β”œβ”€β”€ train/          # Training labels (YOLO format)
    └── valid/          # Validation labels (YOLO format)

Organize Dataset (If Needed)

If your dataset is in a single folder (e.g., dataset_txt/), use the organization script to split it into train/valid:

# Organize dataset into train/valid splits (80/20)
python organize_dataset.py

# Custom options
python organize_dataset.py --source "Drone Dataset/drone_dataset_yolo/dataset_txt" --target "Drone Dataset/drone_dataset_yolo" --train-ratio 0.8

This script will:

  • Split images and labels into train/valid sets (80/20 by default)
  • Create the required folder structure
  • Copy files to appropriate directories
  • Maintain image-label pairs

YOLO Label Format

Each image should have a corresponding .txt file with the same name. Label format:

class_id center_x center_y width height

All coordinates are normalized (0-1). Example:

0 0.5 0.5 0.2 0.3

Configure data.yaml

Update data.yaml if your dataset path is different:

path: ./Drone Dataset/drone_dataset_yolo
train: images/train
val: images/valid
nc: 1  # Number of classes
names:
  0: drone

πŸƒ Training

Train the Model

# Activate virtual environment (if not already active)
source venv/bin/activate

# Run training
python train_model.py

Training Configuration

The training script (train_model.py) is configured with:

  • Model: YOLOv8 Nano (yolov8n.pt)
  • Epochs: 40
  • Image Size: 640x640
  • Batch Size: 16 (adjust based on available memory)
  • Device: Automatically detects MPS/CPU

Training Output

After training, the best model will be saved at:

runs/train/counter_drone/weights/best.pt

Training results (plots, metrics) will be available in:

runs/train/counter_drone/

πŸŽ₯ Detection

Detect in Video File

# Detect drones in a video file
python detect_video.py --source test.mp4 --conf 0.25

# Use custom model
python detect_video.py --model runs/train/counter_drone/weights/best.pt --source test.mp4

Detect from Webcam

# Use default webcam (index 0)
python detect_video.py --source 0

# Use specific webcam
python detect_video.py --source 1

Command-Line Options

  • --model: Path to model weights (default: runs/train/counter_drone/weights/best.pt)
  • --source: Video file path or webcam index (default: 0)
  • --conf: Confidence threshold 0-1 (default: 0.25)

Controls

  • Press q to quit
  • Press s to save current frame

🎯 Tracking

Track Drones in Video

# Track drones in a video file
python track_video.py --source test.mp4 --conf 0.25

# Track from webcam
python track_video.py --source 0

Tracking Features

  • Unique IDs: Each detected drone gets a unique ID
  • Trails: Visual trails show drone movement paths
  • Color Coding: Different colors for different tracks
  • Bounding Boxes: Detection boxes with confidence scores

Install Norfair (Optional)

If you want to use the advanced tracking features:

pip install norfair

If Norfair is not installed, the script will use a simple tracking fallback.

πŸ“ Usage Examples

Example 1: Complete Training and Detection Pipeline

# 1. Organize dataset (if not already organized)
python organize_dataset.py

# 2. Train the model
python train_model.py

# 3. Test on video file
python detect_video.py --source test.mp4

# 4. Test with tracking
python track_video.py --source test.mp4

Example 2: Real-time Webcam Detection

# Detect drones from webcam
python detect_video.py --source 0 --conf 0.3

# Track drones from webcam
python track_video.py --source 0 --conf 0.3

Example 3: Create Test Video from Dataset

# Create a test video from validation images
python create_test_video.py

# Custom options
python create_test_video.py --output my_test_video.mp4 --fps 15 --duration 10

# Then use it for detection
python detect_video.py --source my_test_video.mp4

Example 4: Custom Confidence Threshold

# Higher confidence (fewer false positives)
python detect_video.py --source test.mp4 --conf 0.5

# Lower confidence (more detections)
python detect_video.py --source test.mp4 --conf 0.15

Example 5: Test Camera Access

# List all accessible cameras
python test_camera.py --list

# Test specific camera
python test_camera.py --camera 0

πŸ”§ Troubleshooting

Issue: MPS not available

Solution: Make sure you're using PyTorch 2.0+ and macOS 12.3+ with Apple Silicon.

pip install --upgrade torch torchvision

Issue: Dataset not found

Solution: Check that data.yaml points to the correct dataset path and the folder structure matches the expected format.

Issue: Out of memory during training

Solution: Reduce batch size in train_model.py:

BATCH_SIZE = 8  # or lower

Issue: Video file not opening

Solution:

  • Make sure the video file path is correct
  • Use absolute path instead of relative path
  • Verify file exists: ls -la path/to/video.mp4
  • Check file format is supported (MP4, AVI, MOV, MKV)
  • Try a different video file to rule out corruption
  • Example: python detect_video.py --source /absolute/path/to/video.mp4

Issue: NumPy 2.x Compatibility Error

Error Message:

A module that was compiled using NumPy 1.x cannot be run in NumPy 2.3.3
AttributeError: _ARRAY_API not found

Solution:

This happens when using conda base environment with NumPy 2.x. Fix it:

  1. Use Virtual Environment (NOT conda base):

    # Deactivate conda if active
    conda deactivate
    
    # Activate venv (you should see (venv) not (base))
    source venv/bin/activate
  2. Fix NumPy Version:

    # Option 1: Use the fix script
    ./fix_numpy_issue.sh
    
    # Option 2: Manual fix
    pip uninstall numpy opencv-python opencv-contrib-python -y
    pip install -r requirements.txt
  3. Verify:

    python -c "import numpy; print('NumPy:', numpy.__version__)"  # Should be 1.x.x
    python -c "import cv2; print('OpenCV:', cv2.__version__)"     # Should work

Issue: Webcam not working

Solution:

  • macOS Camera Permissions:

    1. Go to System Settings > Privacy & Security > Camera
    2. Enable camera access for:
      • Terminal (if running from terminal)
      • Python (if available in the list)
      • VS Code (if running from VS Code)
    3. Restart Terminal/VS Code after enabling permissions
    4. Test camera access: python test_camera.py --list
  • Test Camera Access:

    # List all accessible cameras
    python test_camera.py --list
    
    # Test specific camera
    python test_camera.py --camera 0
  • Other Solutions:

    • Try different webcam indices (0, 1, 2, etc.)
    • Make sure no other application is using the webcam
    • Use a video file instead: python detect_video.py --source video.mp4

πŸ“ˆ Performance Tips

  1. Training: Use MPS backend for faster training on Apple Silicon
  2. Inference: Reduce image size for faster processing
  3. Tracking: Adjust distance_threshold in track_video.py for better tracking
  4. Confidence: Tune confidence threshold based on your use case

πŸ“š Additional Resources

🀝 Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements.

πŸ“„ License

This project is open source and available for educational and research purposes.

πŸ‘€ Author

Built for Counter-Drone Detection and Tracking System

πŸ™ Acknowledgments

  • Ultralytics for YOLOv8
  • PyTorch team for MPS support
  • Norfair for tracking capabilities

Happy Drone Detecting! 🚁

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors