Real-time monocular depth estimation for DJI Tello drone collision avoidance using PyDNet. Based on the paper "Towards real-time unsupervised monocular depth estimation on CPU" (IROS 2018).
Major Update v2.0: Complete refactor with TensorFlow 2.x, webcam support, comprehensive tests, and modern Python packaging.
- 🚁 Tello Drone Integration: Real-time depth estimation on DJI Tello video stream
- 📷 Webcam Support: Test without a drone using any USB/built-in webcam
- 🧠 PyDNet Depth Estimation: Fast monocular depth estimation optimized for CPU
- 🎯 Collision Avoidance: Autonomous navigation based on depth maps
- 🔧 Modern Architecture: Clean, modular codebase with TF2.x
- ✅ Comprehensive Tests: Full unit test coverage with mocks
- 📊 Real-time Visualization: Depth maps with configurable colormaps
- ⚙️ Flexible Configuration: Easy-to-use config system
- Python 3.8 or higher
- pip package manager
- (Optional) CUDA-capable GPU for faster inference
# Clone the repository
git clone https://github.com/dronefreak/dji-tello-collision-avoidance-pydnet.git
cd dji-tello-collision-avoidance-pydnet
# Create virtual environment
python -m venv venv
# Activate it
source venv/bin/activate # Linux/Mac
# or
venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# For development
pip install -r requirements-dev.txt
# Or install as package
pip install -e .Test depth estimation with your webcam:
python webcam_demo.py --camera_id 0 --colormap plasmaControls:
qorESC- Quitp- Pause/Resumes- Save screenshota- Toggle collision avoidance analysis
Important: Commands are disabled by default for safety.
# Connect to Tello WiFi first, then:
python tello_demo.pyControls:
t- Takeoffl- Lande- Emergency stopq- Quit
Enable collision avoidance (use with caution):
python tello_demo.py --enable_commands --enable_auto_flightDownload pretrained PyDNet weights:
# Create checkpoint directory
mkdir -p checkpoint/IROS18/pydnet
# Download weights (link TBD - see original PyDNet repo)
# Place the checkpoint files in: checkpoint/IROS18/pydnet/The code will run without weights (for testing), but results will be random.
.
├── src/
│ ├── __init__.py # Package initialization
│ ├── config.py # Configuration management
│ ├── depth_estimator.py # PyDNet depth estimation
│ ├── utils.py # Utility functions
│ ├── camera_interface.py # Abstract camera interface
│ ├── webcam_source.py # Webcam implementation
│ ├── tello_source.py # Tello drone implementation
│ └── collision_avoidance.py # Navigation logic
├── tests/ # Unit tests
│ ├── test_depth_estimator.py
│ ├── test_utils.py
│ ├── test_webcam_demo.py
│ ├── test_tello_interface.py
│ └── test_collision_avoidance.py
├── webcam_demo.py # Webcam demo script
├── tello_demo.py # Tello demo script
├── requirements.txt # Dependencies
├── README.md # This file
├── CODE_OF_CONDUCT.md # Community guidelines
├── LICENSE # Apache 2.0 License
└── SECURITY.md # Security policy
from src.config import Config
from src.depth_estimator import DepthEstimator
from src.webcam_source import WebcamSource
from src.utils import preprocess_image
# Setup
config = Config()
estimator = DepthEstimator(config)
camera = WebcamSource(config)
# Open camera
camera.open()
# Capture and estimate depth
success, frame = camera.read()
input_img = preprocess_image(frame, (256, 512), normalize=True)
depth_map = estimator.predict(input_img)
# Cleanup
camera.release()from src.collision_avoidance import CollisionAvoidance
ca = CollisionAvoidance(config)
# Analyze depth map
analysis = ca.analyze_depth(depth_map)
print(f"Suggested action: {analysis['suggested_action']}")
print(f"Safe to move forward: {analysis['is_safe']}")
# Get RC command for drone
left_right, forward_backward, up_down, yaw = ca.get_rc_command(depth_map)from src.config import Config
config = Config(
input_width=640,
input_height=320,
resolution=1, # 1=High, 2=Quarter, 3=Eighth
colormap='viridis',
use_gpu=True,
camera_id=0,
min_safe_depth=0.4,
tello_enable_commands=False # Safety first!
)Key configuration parameters in src/config.py:
| Parameter | Default | Description |
|---|---|---|
input_width |
512 | Model input width |
input_height |
256 | Model input height |
resolution |
1 | Output resolution (1/2/3) |
colormap |
'plasma' | Depth visualization colormap |
use_gpu |
True | Enable GPU acceleration |
camera_id |
0 | Webcam device ID |
min_safe_depth |
0.3 | Obstacle detection threshold |
tello_enable_commands |
False | Enable drone commands |
enable_collision_avoidance |
False | Enable autonomous navigation |
Run the test suite:
# Run all tests
python -m unittest discover tests/
# Run specific test file
python -m unittest tests/test_depth_estimator.py
# Run with verbose output
python -m unittest discover tests/ -vApproximate inference times on different hardware:
| Hardware | Resolution | FPS |
|---|---|---|
| Intel i7 (CPU) | High (512x256) | ~15-20 |
| Intel i7 (CPU) | Quarter | ~30-40 |
| NVIDIA RTX 3060 | High (512x256) | ~60-80 |
| NVIDIA RTX 3060 | Quarter | ~120+ |
Results may vary based on system configuration
- Autonomous flight features are EXPERIMENTAL
- Always test in a safe, open environment
- Keep emergency stop ready (
ekey) - Start with commands DISABLED
- Never fly near people, animals, or fragile objects
- Check local drone regulations
- Battery level must be >20% for takeoff
- The developers are not responsible for any damage or injury
# List available cameras (Linux)
ls /dev/video*
# Try different camera IDs
python webcam_demo.py --camera_id 1- Ensure Tello is powered on (flashing yellow light)
- Connect to Tello's WiFi network (TELLO-XXXXXX)
- Check that no other app is using the Tello
- Try restarting both Tello and computer
- Use lower resolution:
--resolution 2or--resolution 3 - Disable GPU if causing issues:
--no_gpu - Reduce input dimensions:
--width 256 --height 128
# Reinstall dependencies
pip install --upgrade -r requirements.txtContributions are welcome! Please read CODE_OF_CONDUCT.md first.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
If you use this work, please cite the original PyDNet paper:
@inproceedings{pydnet18,
title = {Towards real-time unsupervised monocular depth estimation on CPU},
author = {Poggi, Matteo and
Aleotti, Filippo and
Tosi, Fabio and
Mattoccia, Stefano},
booktitle = {IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS)},
year = {2018}
}- Original PyDNet by Matteo Poggi
- Monodepth framework by Clément Godard
- DJITelloPy library
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Author: dronefreak
- GitHub: @dronefreak
- Issues: GitHub Issues
- Complete refactor with TensorFlow 2.x
- Added webcam support for testing
- Comprehensive unit tests
- Modern Python packaging
- Improved documentation
- Safety features and command control
- Configurable parameters
- Initial release with TensorFlow 1.8
- Basic Tello integration
- PyDNet depth estimation
Star ⭐ this repo if you find it useful!