Skip to content

About

Real-time computer vision system for UAV obstacle avoidance using OpenCV. Detects colored objects, classifies threats, and generates flight commands with interactive airplane visualization GUI.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

ย 

History

37 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฏ Project Overview

Python OpenCV

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:

  1. Performance Mode: single-window, zero animation overhead, frame-rate optimized for real-world deployment
  2. Animation Mode: dual-window GUI with live aileron, elevator, and fuselage response per avoidance command

โš™๏ธ Core Capabilities

  • 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

System Flowchart
System Architecture Overview

๐Ÿ“ธ Demo Screenshots

Obstacle Detection
Performance Mode detection of dangerous obstacles with avoidance commands


GUI Animation
Animation Mode GUI showing roll and pitch responses

๐Ÿ›ฉ๏ธ Object Classification System

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

๐Ÿš€ Installation

Prerequisites

# Python 3.7 or higher required
python --version

Required Libraries

pip install opencv-python
pip install numpy
pip install matplotlib
pip install threading

Hardware Requirements

  • IP camera or webcam capable of streaming video
  • Network connection to camera (if using IP camera)
  • Sufficient processing power for real-time video analysis

โš™๏ธ Configuration

Camera Setup

  1. Update the video stream URL in the code:
video_url = "http://192.168.0.155:8080/video"  # Replace with your camera's IP
  1. For local webcam, use:
cap = cv2.VideoCapture(0)  # 0 for default camera

Color Calibration

Adjust 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])
    }
}

๐ŸŽฎ Usage

Execution Modes

The system offers two distinct operation modes:

1. Performance Mode (Recommended for Production)

ENABLE_ANIMATION = False
  • Optimized for maximum processing speed
  • Minimal resource usage
  • Single OpenCV window display
  • Best for real-world deployment

2. Animation Mode (Development/Demonstration)

ENABLE_ANIMATION = True
  • Interactive airplane GUI with realistic animations
  • Dual-window display (video + airplane visualization)
  • Enhanced visual feedback
  • Ideal for testing and demonstrations

Running the System

python flight_controller.py

Controls

  • 'q' key: Exit the application
  • Window close: Terminate all processes safely

๐Ÿง  System Architecture

Core Processing Pipeline

  1. Video Capture: Continuous frame acquisition from IP camera
  2. HSV Conversion: Color space transformation for robust color detection
  3. Object Detection: Contour analysis and shape classification
  4. Proximity Analysis: Calculate distances to identify closest threats
  5. Command Generation: Generate appropriate avoidance maneuvers
  6. Visual Feedback: Display commands and aircraft response

Avoidance Logic

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

๐ŸŽจ GUI Features

Airplane Visualization Components

  • 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

Animation Characteristics

  • 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

๐Ÿ”ง Technical Specifications

Performance Metrics

  • 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

System Requirements

  • CPU: Multi-core processor recommended
  • RAM: 4GB minimum, 8GB recommended
  • Network: Stable connection for IP camera streaming
  • Display: 1200x800 minimum resolution

๐Ÿ› Troubleshooting

Common Issues

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

Optimization Tips

  1. Reduce Processing Load:

    • Lower video resolution
    • Increase minimum contour area threshold
    • Disable GUI elements in performance mode
  2. Improve Detection Accuracy:

    • Use controlled lighting environment
    • Ensure clear color contrast
    • Regular color range calibration

๐Ÿ”ฌ Advanced Configuration

Custom Shape Detection

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 shapes

Threading Optimization

For better performance, consider adjusting thread priorities:

video_thread = threading.Thread(target=video_processing_thread, daemon=True)
video_thread.start()

๐Ÿ“Š Project Structure

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

๐Ÿค Contributing

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

๐Ÿ”— Dependencies

  • OpenCV 4.0+
  • NumPy 1.19+
  • Matplotlib 3.3+
  • Python Threading (built-in)
  • Math (built-in)

๐Ÿ“„ License

This project is open-source. Please ensure responsible use, especially when integrating with real aircraft systems.

About

Real-time computer vision system for UAV obstacle avoidance using OpenCV. Detects colored objects, classifies threats, and generates flight commands with interactive airplane visualization GUI.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages