Skip to content

A real-time background replacement application that uses advanced image processing techniques to segment foreground subjects and composite them onto custom backgrounds.

Notifications You must be signed in to change notification settings

Tasneemmohammed0/realtime-background-replacement

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

$\color{rgba(170, 51, 106)}{\textsf{Real Time Backround Replacement}}$

A real-time background replacement application that uses advanced image processing techniques to segment foreground subjects and composite them onto custom backgrounds. The system features a PyQt5-based GUI with live camera feed processing and multiple segmentation pipelines.

Preview

Adventure Time

Features

  • Real-Time Processing: Live camera feed with minimal latency
  • Dual Pipeline Architecture:
    • Custom segmentation pipeline using background subtraction and guided filtering
    • MediaPipe-based segmentation for high-quality results
  • Interactive GUI: User-friendly interface built with PyQt5
  • Dynamic Background Selection: Choose from preset backgrounds or upload custom images
  • Background Initialization: Capture and learn static backgrounds for improved segmentation
  • Guided Filter Refinement: Edge-aware mask refinement for natural compositing
  • Alpha Blending: Smooth foreground-background composition

Technical Overview

Pipeline Architecture

The application implements two parallel processing pipelines:

  1. Custom Pipeline (src/pipeline/pipeline.py)

    • Background modeling using median-based static background capture
    • Color-based segmentation in LAB color space
    • Guided filter for mask refinement
    • Alpha blending for final composition
  2. MediaPipe Pipeline (src/pipeline/ideal_pipeline.py)

    • MediaPipe Selfie Segmentation for robust person detection
    • Guided filter post-processing
    • High-quality segmentation masks

Key Components

  • Background Modeling (background_modeling.py): Captures and maintains a static background model
  • Segmentation (segmenter.py, mediapipe_segmenter.py): Foreground/background separation
  • Guided Filter (guided_filter.py): Edge-preserving mask refinement
  • Compositor (background_compositor.py): Alpha blending and final composition
  • Camera Application (ui/camera_app.py): PyQt5-based user interface

Installation

Prerequisites

  • Python 3.12 or higher
  • Webcam or camera device
  • Windows/Linux/macOS

Setup

  1. Clone the repository:
git clone https://github.com/habibayman/realtime-background-replacement.git
cd realtime-background-replacement
  1. Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Usage

Running the Application

python main.py

Workflow

  1. Initialize Background: Click "Initialize Background" and step out of the camera frame for 2 seconds
  2. Select Background: Choose a background image from the carousel or upload a custom one
  3. Start Replacement: Click "Start Replacement" to begin real-time processing
  4. Compare Results: View side-by-side comparison of raw feed, custom pipeline output, and MediaPipe output

Adding Custom Backgrounds

Place background images in the assets/ directory, or use the "Upload Custom Background" button in the application to add images dynamically.

Project Structure

realtime-background-replacement/
├── main.py                          # Application entry point
├── requirements.txt                 # Python dependencies
├── assets/                          # Background images and resources
├── data/                            # Data files and samples
├── notebooks/                       # Jupyter notebooks for experimentation
└── src/
    ├── app.py                       # Alternative entry point
    ├── config.py                    # Configuration and hyperparameters
    ├── pipeline/                    # Image processing pipeline modules
    │   ├── background_modeling.py   # Background capture and modeling
    │   ├── segmenter.py             # Custom segmentation implementation
    │   ├── mediapipe_segmenter.py   # MediaPipe-based segmentation
    │   ├── guided_filter.py         # Edge-aware filtering
    │   ├── background_compositor.py # Alpha blending and composition
    │   ├── pipeline.py              # Custom pipeline orchestration
    │   └── ideal_pipeline.py        # MediaPipe pipeline orchestration
    ├── ui/
    │   └── camera_app.py            # PyQt5 GUI application
    └── utils/
        └── commonfunctions.py       # Utility functions

Dependencies

Core dependencies include:

  • OpenCV (opencv-contrib-python-headless): Image processing and camera capture
  • PyQt5: GUI framework
  • NumPy: Numerical operations
  • MediaPipe: Person segmentation (optional, for ideal pipeline)
  • Matplotlib: Visualization and analysis
  • Pillow: Image I/O operations

See requirements.txt for complete dependency list.

Algorithm Details

Background Subtraction

The custom pipeline uses a median-based background model that captures multiple frames of the empty scene and computes a robust background estimate. Segmentation is performed by comparing the current frame against this model in LAB color space.

Guided Filter

The guided filter is an edge-preserving smoothing filter that refines the segmentation mask while maintaining sharp boundaries at edges. It uses the input image as a guidance image to transfer structural details to the filtered output.

Alpha Blending

Final composition uses alpha blending with the refined mask to smoothly composite the foreground subject onto the replacement background, avoiding hard edges and artifacts.

Performance Considerations

  • Target frame rate: ~30 FPS
  • Resolution: 640x480 (configurable)
  • Camera backend: DirectShow (Windows), V4L2 (Linux), or platform default
  • Processing optimizations: NumPy vectorization, efficient color space conversions

Troubleshooting

Camera Not Opening

  • Ensure no other application is using the camera
  • Try different camera backends by modifying camera_id in camera_app.py
  • Check camera permissions on your operating system

Qt Plugin Conflicts

The application handles Qt plugin path conflicts between OpenCV and PyQt5 automatically. If issues persist, ensure you're using compatible versions of both libraries.

MediaPipe Import Errors

If MediaPipe fails to import, the application will fall back to the custom segmentation pipeline. Ensure MediaPipe is properly installed for your platform.

Contributing

Contributions are welcome. Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes with clear commit messages
  4. Test thoroughly
  5. Submit a pull request

License

This project is available for educational and research purposes.

Acknowledgments

  • MediaPipe by Google for person segmentation
  • Guided Filter implementation based on "Guided Image Filtering" by He et al.
  • PyQt5 for the GUI framework
  • OpenCV community for computer vision tools

Contributors

This project was developed by:

Habiba Ayman
Habiba Ayman
Tasneem Mohammed
Tasneem Mohammed
Loay Ahmed
Loay Ahmed
Omar Gamal
Omar Gamal

Contact

For questions or issues, please open an issue on the GitHub repository.

About

A real-time background replacement application that uses advanced image processing techniques to segment foreground subjects and composite them onto custom backgrounds.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 98.0%
  • Python 2.0%