UAV obstacle avoidance system that processes live IP camera footage, classifies airborne obstacles by color and shape into dangerous, boundary, or safe categories, then issues roll and pitch commands based on proximity and frame position.
Ships in two modes:
- Performance Mode: single-window, zero animation overhead, frame-rate optimized for real-world deployment
- Animation Mode: dual-window GUI with live aileron, elevator, and fuselage response per avoidance command
- Real-time Object Detection: HSV color space analysis with dual-range red detection
- Intelligent Classification: Dangerous, boundary, or safe zone categorization per frame
- Dynamic Avoidance Logic: Roll and pitch commands issued based on proximity and frame position
- Dual Execution Modes: Performance pipeline for deployment, animation mode for demonstration
- Interactive Airplane Visualization: Aileron, elevator, and fuselage animations per avoidance command
| Object Type | Color | Shape | Classification | Action |
|---|---|---|---|---|
| Dangerous Obstacle | ๐ด | โณ | High Priority Avoidance | Roll/Pitch commands generated |
| Boundary Marker | ๐ต | โข | Navigation Reference | Visual tracking only |
| Safe Zone | ๐ข | โฏ | Safe Area | No avoidance needed |
# Python 3.7 or higher required
python --versionpip install opencv-python
pip install numpy
pip install matplotlib
pip install threading- IP camera or webcam capable of streaming video
- Network connection to camera (if using IP camera)
- Sufficient processing power for real-time video analysis
- Update the video stream URL in the code:
video_url = "http://192.168.0.155:8080/video" # Replace with your camera's IP- For local webcam, use:
cap = cv2.VideoCapture(0) # 0 for default cameraAdjust HSV color ranges in the color_ranges dictionary based on your lighting conditions:
color_ranges = {
'red': {
'lower1': np.array([0, 120, 70]), # Lower red hue range
'upper1': np.array([10, 255, 255]),
'lower2': np.array([170, 120, 70]), # Upper red hue range
'upper2': np.array([180, 255, 255])
},
'green': {
'lower': np.array([36, 100, 100]), # Green range
'upper': np.array([86, 255, 255])
},
'blue': {
'lower': np.array([94, 120, 100]), # Blue range
'upper': np.array([126, 255, 255])
}
}The system offers two distinct operation modes:
ENABLE_ANIMATION = False- Optimized for maximum processing speed
- Minimal resource usage
- Single OpenCV window display
- Best for real-world deployment
ENABLE_ANIMATION = True- Interactive airplane GUI with realistic animations
- Dual-window display (video + airplane visualization)
- Enhanced visual feedback
- Ideal for testing and demonstrations
python flight_controller.py- 'q' key: Exit the application
- Window close: Terminate all processes safely
- Video Capture: Continuous frame acquisition from IP camera
- HSV Conversion: Color space transformation for robust color detection
- Object Detection: Contour analysis and shape classification
- Proximity Analysis: Calculate distances to identify closest threats
- Command Generation: Generate appropriate avoidance maneuvers
- Visual Feedback: Display commands and aircraft response
IF dangerous_obstacle_detected:
closest_obstacle = find_nearest_threat()
IF horizontal_distance > vertical_distance:
IF obstacle_right_of_center:
COMMAND = "Roll Left"
ELSE:
COMMAND = "Roll Right"
ELSE:
IF obstacle_below_center:
COMMAND = "Pitch Up"
ELSE:
COMMAND = "Pitch Down"
ELSE:
COMMAND = "Clear"
๐ View Detailed Logic Flowchart - Complete decision tree diagram
- Fuselage: Main aircraft body with cockpit detail
- Wings: Dynamic roll movement with realistic proportions
- Control Surfaces:
- Ailerons (roll control) - move differentially
- Elevators (pitch control) - synchronized movement
- Rudder (yaw control) - currently static
- Visual References: Horizon lines and center markers
- Command Display: Real-time command status with color coding
- Smooth Interpolation: Gradual movement transitions (120ms response time)
- Realistic Physics: Wing banking angles up to ยฑ35 degrees
- Control Surface Detail: Separate aileron and elevator animations
- Color-Coded Feedback:
- ๐ข Green: Clear/Safe
- ๐ Orange: Roll maneuvers
- ๐ด Red: Pitch maneuvers
- Frame Rate: ~30 FPS (Performance Mode)
- Detection Latency: <50ms
- Min Object Size: 400 pixels area
- Color Detection: HSV-based with dual-range red detection
- Shape Accuracy: Polygon approximation with 4% tolerance
- CPU: Multi-core processor recommended
- RAM: 4GB minimum, 8GB recommended
- Network: Stable connection for IP camera streaming
- Display: 1200x800 minimum resolution
Video Stream Connection Failed
Error: Could not open video stream.
- Verify camera IP address and network connectivity
- Check if camera supports the streaming URL format
- Try using different video codecs or resolutions
Poor Color Detection
- Calibrate HSV ranges under current lighting conditions
- Ensure adequate contrast between objects and background
- Consider lighting improvements or camera settings adjustment
Performance Issues
- Switch to Performance Mode (
ENABLE_ANIMATION = False) - Reduce video resolution if possible
- Close unnecessary applications
- Check CPU usage during operation
-
Reduce Processing Load:
- Lower video resolution
- Increase minimum contour area threshold
- Disable GUI elements in performance mode
-
Improve Detection Accuracy:
- Use controlled lighting environment
- Ensure clear color contrast
- Regular color range calibration
Modify the detect_shape() function to add new shape categories:
def detect_shape(contour):
# Add custom shape detection logic
if len(approx) == 5:
shape = "pentagon"
elif len(approx) == 6:
shape = "hexagon"
# ... additional shapesFor better performance, consider adjusting thread priorities:
video_thread = threading.Thread(target=video_processing_thread, daemon=True)
video_thread.start()OpenCV-Flight-Controller/
โโโ assets/
โ โโโ PerformanceMode.png
โ โโโ AnimationMode.png
โ โโโ System-flowchart.png
โ โโโ Logic-flowchart.png
โโโ flight_controller.py
โ โโโ Section 1: Setup & Configuration
โ โโโ Section 2: Shape Detection Functions
โ โโโ Section 3: Core Video Processing
โ โโโ Section 4: Object Detection & Classification
โ โโโ Section 5: Obstacle Avoidance Logic
โ โโโ Section 6: Display & Visual Aids
โ โโโ Section 7-8: Airplane Animation Classes
โ โโโ Section 9: Threaded Video Processing
โ โโโ Section 10: Main Execution Controller
โ โโโ Section 11: Cleanup & Error Handling
|โโ README.md
Contributions are welcome! Areas for improvement:
- Additional object shapes and colors
- Machine learning integration for better detection
- 3D visualization enhancements
- Real hardware integration (drone/aircraft APIs)
- Performance optimizations
- Mobile platform support
- OpenCV 4.0+
- NumPy 1.19+
- Matplotlib 3.3+
- Python Threading (built-in)
- Math (built-in)
This project is open-source. Please ensure responsible use, especially when integrating with real aircraft systems.


