video-bg-removal/
├── app.py # Main Flask application
├── requirements.txt # Python dependencies
├── templates/
│ └── index.html # Frontend template
├── uploads/ # Temporary upload folder (created automatically)
├── processed/ # Processed videos folder (created automatically)
└── README.md # This file
- AI Normal Mode: High-accuracy deep learning model for best results
- AI Fast Mode: Faster processing with good quality
- Traditional Methods: MOG2, GrabCut, and Contours for fallback
- Supports multiple video formats (MP4, AVI, MOV, MKV, WEBM)
- Real-time progress tracking
- Drag & drop file upload
- Responsive web interface
- Automatic cleanup of temporary files
- GPU acceleration support (if available)
Create a new directory and save all the files:
app.py- Main Flask applicationrequirements.txt- Dependencies- Create a
templatesfolder and saveindex.htmlinside it
python -m venv video-bg-env
source video-bg-env/bin/activate # On Windows: video-bg-env\Scripts\activatepip install -r requirements.txtNote: The transparent-background library will download pre-trained models on first use (~100MB).
For better performance, you might need:
# For GPU support (NVIDIA)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
# For CPU-only (if you don't have GPU)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpupython app.pyOpen your browser and go to: http://localhost:5000
- Click the upload area or drag & drop your video file
- Supported formats: MP4, AVI, MOV, MKV, WEBM
- Maximum file size: 100MB
- AI Normal: Best quality, slower processing (recommended)
- AI Fast: Good quality, faster processing
- MOG2: Traditional method, good for moving subjects
- GrabCut: Good for static backgrounds
- Contours: Fastest, basic edge detection
- Click "Process Video" button
- Monitor progress in real-time
- Processing time varies based on video length and method
- Preview the processed video
- Download the result
- Process another video or cleanup
The application uses the transparent-background library which includes:
- InSPyReNet (Inference for Segmentation Pyramid Network)
- U²-Net based architectures
- Pre-trained on large datasets for accurate segmentation
-
AI Normal/Fast:
- Uses deep learning models
- Processes frame-by-frame
- Converts to PIL → AI processing → OpenCV
-
Traditional Methods:
- MOG2: Background subtraction using Mixture of Gaussians
- GrabCut: Interactive foreground extraction
- Contours: Edge-based object detection
- GPU: Significantly faster for AI methods
- CPU: Works but slower, especially for AI methods
- Memory: ~2-4GB RAM recommended for smooth operation
- Timeout: 19-minute processing limit for safety
-
Import Error for transparent_background
pip install transparent-background -
CUDA/GPU Issues
- Install appropriate PyTorch version for your CUDA version
- Or use CPU-only version:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
-
Memory Issues
- Use AI Fast mode instead of Normal
- Process shorter videos
- Ensure sufficient RAM
-
Slow Processing
- Use AI Fast mode
- Try traditional methods (MOG2, Contours)
- Consider GPU acceleration
On first run, the AI models will be downloaded automatically:
- Models are stored in
~/.cache/transparent-background/ - Total size: ~100-200MB
- Internet connection required for first run
GET /- Main interfacePOST /upload- Upload and start processingGET /status/<task_id>- Check processing statusGET /download/<filename>- Download processed videoPOST /cleanup/<task_id>- Cleanup temporary files
Modify in app.py:
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MBModify in app.py:
if time.time() - start_time >= 19 * 60: # 19 minutesThe application automatically detects and uses available GPU. To force CPU:
import torch
torch.cuda.is_available = lambda: FalseModify the AI processing method to use different background types:
# In _process_with_ai method
processed_pil = remover.process(pil_img, type='map') # or 'white', 'blur', etc.The current implementation processes one video at a time. For batch processing, you'd need to implement a queue system.
- The app includes basic file validation
- Temporary files are cleaned up automatically
- Consider adding authentication for production use
- File size limits prevent abuse
This application integrates several open-source libraries:
- Flask - Web framework
- OpenCV - Computer vision
- transparent-background - AI background removal
- PyTorch - Deep learning framework
For issues related to:
- Flask/Web Interface: Check Flask documentation
- AI Models: Check transparent-background GitHub repository
- OpenCV: Check OpenCV documentation
- General Python: Check respective library documentation