Skip to content

Adamos123421/object-detection

Repository files navigation

Real-Time Object Detection

A high-performance real-time object detection system using YOLOv4 and OpenCV with webcam integration. Optimized for smooth, lag-free performance with both CPU and GPU acceleration support.

Python OpenCV YOLO

🧠 Technical Deep Dive: YOLO Architecture & Implementation

Understanding YOLO (You Only Look Once)

This project leverages the YOLOv4 architecture, a state-of-the-art real-time object detection system. Here's how it works and how it's implemented in this project:

🏗️ YOLO Architecture Overview

YOLO revolutionized object detection by treating it as a single regression problem, directly predicting bounding boxes and class probabilities from full images in one evaluation.

Key Innovation: Unlike traditional methods that apply classifiers to different parts of an image, YOLO:

  1. Divides the image into an S×S grid
  2. Predicts bounding boxes and confidence scores for each grid cell
  3. Outputs class probabilities for each bounding box
  4. Performs all operations in a single forward pass

🔬 YOLOv4 Specific Features

Backbone Network: CSPDarknet53

  • CSP (Cross Stage Partial) connections improve gradient flow
  • 53 layers with residual connections for feature extraction
  • Mish activation function for better gradient flow

Neck: PANet (Path Aggregation Network)

  • Feature Pyramid structure for multi-scale detection
  • Bottom-up and top-down path augmentation
  • Enhanced feature fusion for small object detection

Head: YOLOv4 Detection Head

  • Three scales of detection (13×13, 26×26, 52×52)
  • Anchor boxes for different object sizes
  • Multi-scale predictions for objects of varying sizes

📊 Training Process & Dataset

Training Dataset: COCO (Common Objects in Context)

  • 330K images with 80 object categories
  • 1.5M object instances with precise bounding box annotations
  • Categories include: person, vehicle, animal, household objects, food, etc.

Training Methodology:

  1. Data Augmentation: Mosaic, MixUp, CutMix for robustness
  2. Loss Function: Multi-part loss combining:
    • Bounding box regression loss (CIOU loss)
    • Object confidence loss (binary cross-entropy)
    • Classification loss (binary cross-entropy)
  3. Optimization: Adam optimizer with learning rate scheduling
  4. Training Duration: ~300 epochs on multiple GPUs

Model Variants Used:

  • YOLOv4: Full model (245MB) - Maximum accuracy
  • YOLOv4-tiny: Lightweight version (23MB) - Optimized for speed

🛠️ Implementation in This Project

Model Integration:

# Load pre-trained YOLOv4 model
self.net = cv2.dnn.readNet(weights_path, config_path)

# Configure for GPU acceleration
self.net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

Input Processing:

# Preprocess frame for YOLO input
blob = cv2.dnn.blobFromImage(frame, 0.00392, (608, 608), (0, 0, 0), True, crop=False)
# 0.00392 = 1/255 for normalization
# (608, 608) = input size for optimal accuracy/speed balance

Inference Pipeline:

  1. Frame Capture: Real-time webcam input
  2. Preprocessing: Resize, normalize, create blob
  3. Forward Pass: Single network evaluation
  4. Post-processing: NMS (Non-Maximum Suppression) for duplicate removal
  5. Visualization: Draw bounding boxes and labels

Performance Optimizations Implemented:

  1. Frame Skipping: Process every Nth frame to reduce computational load
if frame_count % self.process_every_n_frames == 0:
    detections = self.detect_objects_yolo(frame)
  1. Vectorized Operations: NumPy optimizations for faster post-processing
# Vectorized confidence filtering
valid_detections = confidences_temp > self.confidence_threshold
  1. Multi-threading: Asynchronous processing for smoother video
detection_thread = threading.Thread(target=self.detection_worker)
  1. Memory Management: Efficient buffer handling and queue management

📈 Performance Metrics & Analysis

Accuracy Metrics (on COCO dataset):

  • YOLOv4: mAP@0.5 = 65.7%, mAP@0.5:0.95 = 43.5%
  • YOLOv4-tiny: mAP@0.5 = 40.2%, mAP@0.5:0.95 = 21.7%

Real-time Performance (this implementation):

  • RTX 3060 + YOLOv4-tiny: 25-30 FPS at 640×480
  • CPU Only + YOLOv4-tiny: 15-20 FPS at 640×480
  • Memory Usage: ~500MB-2GB depending on model

Optimization Trade-offs:

  • Input Resolution: 320×320 (speed) vs 608×608 (accuracy)
  • Model Size: YOLOv4-tiny (fast) vs YOLOv4 (accurate)
  • Frame Processing: Every frame (smooth) vs skip frames (efficient)

🔧 Technical Innovations in This Project

Smart Frame Processing:

  • Adaptive frame skipping based on system performance
  • Cached detections for skipped frames to maintain visual continuity
  • Dynamic performance adjustment via keyboard controls

GPU Acceleration Strategy:

  • Multi-backend support: CUDA → OpenCL → CPU fallback
  • Error handling for GPU compatibility issues
  • Performance monitoring and automatic optimization

Real-time Optimizations:

  • Vectorized post-processing using NumPy operations
  • Efficient NMS implementation for duplicate removal
  • Memory-conscious queue management for threading

💡 Computer Vision Concepts Demonstrated

  1. Object Detection Pipeline: End-to-end implementation from input to output
  2. Deep Learning Integration: Pre-trained model deployment and optimization
  3. Real-time Processing: Balancing accuracy with performance constraints
  4. GPU Acceleration: Leveraging parallel processing for computer vision
  5. Performance Profiling: FPS monitoring and system optimization
  6. User Interface Design: Interactive controls for real-time parameter adjustment

This project showcases practical implementation of modern computer vision techniques, demonstrating both theoretical understanding and practical engineering skills in deploying deep learning models for real-time applications.

🚀 Features

  • Real-time object detection with YOLOv4 and YOLOv4-tiny models
  • Smooth performance optimized for consistent high FPS
  • GPU acceleration support (CUDA/OpenCL) with CPU fallback
  • 80+ object classes detection (COCO dataset)
  • Interactive controls for real-time parameter adjustment
  • Multiple detection modes for different performance needs
  • Screenshot and video recording capabilities
  • Cross-platform compatibility (Windows, macOS, Linux)

📋 Requirements

  • Python 3.8 or higher
  • Webcam or external camera
  • (Optional) NVIDIA GPU with CUDA support for acceleration

🛠️ Installation

  1. Clone the repository

    git clone https://github.com/yourusername/real-time-object-detection.git
    cd real-time-object-detection
  2. Install dependencies

    pip install -r requirements.txt
  3. Run the application

    python smooth_object_detection.py

🎮 Usage

Basic Usage

python smooth_object_detection.py

Advanced Usage

# Adjust confidence threshold
python smooth_object_detection.py --confidence 0.5

# Use different camera
python smooth_object_detection.py --camera 1

# Adjust frame processing rate
python smooth_object_detection.py --skip-frames 2

Alternative Versions

# High-accuracy mode (slower but more precise)
python object_detection.py

# GPU-optimized mode (requires proper GPU setup)
python object_detection.py --device cuda

🎯 Detection Modes

1. Smooth Object Detection (Recommended)

  • File: smooth_object_detection.py
  • Best for: Consistent, lag-free performance
  • Features: Smart frame skipping, CPU optimized, smooth video
  • Performance: 15-25 FPS on most systems

2. Standard Object Detection

  • File: object_detection.py
  • Best for: Maximum accuracy
  • Features: Full YOLOv4 model, GPU acceleration
  • Performance: Variable based on hardware

🎛️ Interactive Controls

While running the application:

Key Action
q Quit application
s Save screenshot
+ Process more frames (faster detection)
- Process fewer frames (smoother video)
r Reset background (motion detection mode)

📊 Performance Optimization

For Maximum Speed

  • Use smooth_object_detection.py
  • Lower resolution camera settings
  • Increase frame skip rate
  • Use YOLOv4-tiny model

For Maximum Accuracy

  • Use object_detection.py
  • Higher resolution settings
  • Process every frame
  • Use full YOLOv4 model

🔧 Configuration

Edit config.json to customize:

{
    "model_settings": {
        "confidence_threshold": 0.4,
        "nms_threshold": 0.4,
        "input_size": 320
    },
    "camera_settings": {
        "width": 640,
        "height": 480,
        "fps": 30
    }
}

📁 Project Structure

real-time-object-detection/
├── smooth_object_detection.py    # Main optimized detection script
├── object_detection.py           # Full-featured detection script
├── requirements.txt              # Python dependencies
├── config.json                  # Configuration settings
├── README.md                    # This file
├── models/                      # YOLO model files (auto-downloaded)
│   ├── yolov4.weights
│   ├── yolov4.cfg
│   ├── yolov4-tiny.weights
│   └── yolov4-tiny.cfg
└── screenshots/                 # Saved screenshots (auto-created)

🎯 Detected Objects

The system can detect 80+ object classes including:

People & Animals: person, dog, cat, bird, horse, cow, elephant, bear, zebra, giraffe

Vehicles: car, motorcycle, airplane, bus, train, truck, boat, bicycle

Everyday Objects: bottle, cup, fork, knife, spoon, bowl, laptop, mouse, keyboard, cell phone

And many more!

🚀 Performance Tips

  1. For smooth video: Use smooth_object_detection.py with frame skipping
  2. For GPU acceleration: Ensure CUDA-enabled OpenCV installation
  3. For better detection: Use higher confidence thresholds
  4. For speed: Use lower camera resolution and YOLOv4-tiny

🐛 Troubleshooting

Camera Not Found

# Try different camera IDs
python smooth_object_detection.py --camera 1
python smooth_object_detection.py --camera 2

Low Performance

  • Use smooth_object_detection.py
  • Lower camera resolution
  • Increase frame skip rate
  • Close other applications

Model Download Issues

Models are automatically downloaded on first run. If download fails:

  1. Check internet connection
  2. Manually download from official YOLO releases
  3. Place in models/ directory

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Ultralytics YOLO for the YOLO implementation and training methodology
  • AlexeyAB Darknet for YOLOv4 model architecture and pre-trained weights
  • OpenCV for computer vision capabilities and DNN module
  • COCO Dataset for providing the training data and evaluation metrics

📈 Performance Benchmarks

System Model Resolution FPS GPU Usage
RTX 3060 YOLOv4-tiny 640x480 25-30 CPU Optimized
RTX 3080 YOLOv4 1280x720 15-20 GPU Accelerated
CPU Only YOLOv4-tiny 640x480 15-20 CPU Optimized

Happy Object Detecting! 🎯

For questions or support, please open an issue in the repository.

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages