Interactive image inpainting combining Segment Anything Model (SAM) with Stable Diffusion for powerful AI-driven image editing.
- π±οΈ Click-to-Segment: Interactive pixel selection with SAM
- π¨ AI Inpainting: Text-guided image generation with Stable Diffusion
- π Web Interface: User-friendly Gradio web app
- β‘ CLI Tools: Command-line interface for batch processing
- βοΈ Configurable: YAML-based configuration system
- π§ Modular: Clean, maintainable code architecture
- π Logging: Comprehensive logging and error handling
# Clone repository
git clone https://github.com/angadbawa/Oracle.git
cd Oracle
# Install dependencies
pip install -r requirements.txt
# Download SAM weights (if not already present)
# Place sam_vit_h_4b8939.pth in ./weights/ directory# Start Gradio web app
python main.py web
# Custom host/port
python main.py web --host 0.0.0.0 --port 8080
# Create public link
python main.py web --share# Inpaint from click coordinates
python main.py cli inpaint-click image.jpg 100 150 "a beautiful red flower"
# Inpaint with existing mask
python main.py cli inpaint-mask image.jpg mask.jpg "a sunset sky"
# Generate mask only
python main.py cli mask-only image.jpg "100,150,200,250"
# Show system information
python main.py infoOracle/
βββ src/
β βββ core/ # Core AI modules
β β βββ sam_predictor.py # SAM model wrapper
β β βββ diffusion_pipeline.py # Stable Diffusion wrapper
β β βββ image_processor.py # Main orchestrator
β βββ ui/ # User interfaces
β β βββ gradio_app.py # Web interface
β β βββ cli_app.py # Command line interface
β βββ utils/ # Utilities
β β βββ config.py # Configuration management
β β βββ logger.py # Logging setup
β β βββ helpers.py # Helper functions
β βββ config/
β βββ default.yaml # Configuration file
βββ main.py # Unified entry point
βββ requirements.txt # Dependencies
βββ README.md # This file
SAMPredictor: Segment Anything Model wrapper for interactive segmentationDiffusionInpainter: Stable Diffusion pipeline for text-guided inpaintingImageProcessor: Main orchestrator combining SAM + DiffusionOracleGradioApp: Web interface with interactive UIOracleCLI: Command-line interface for automation
Oracle uses YAML configuration files for easy customization:
# src/config/default.yaml
models:
sam:
model_type: "vit_h"
checkpoint_path: "./weights/sam_vit_h_4b8939.pth"
device: "cpu" # or "cuda"
stable_diffusion:
model_name: "stabilityai/stable-diffusion-2-inpainting"
device: "cpu" # or "cuda"
image:
default_size: [512, 512]
max_file_size_mb: 10
processing:
diffusion:
num_inference_steps: 20
guidance_scale: 7.5
strength: 0.8# Use custom config file
python main.py web --config my_config.yaml
# Override device settings
python main.py web --device cudaThe Gradio web interface provides an intuitive way to use Oracle:
- πΈ Upload Image: Load your image
- π±οΈ Click to Segment: Click on areas to segment with SAM
- β¨ Enter Prompt: Describe what you want to generate
- π¨ Generate: Create AI-powered inpainting results
- Interactive point selection
- Real-time mask generation
- Advanced parameter controls
- System information display
- Example prompts
Powerful command-line tools for automation and batch processing:
python main.py cli inpaint-click \
image.jpg 150 200 \
"a beautiful sunset" \
--negative "blurry, low quality" \
--steps 30 \
--guidance 8.0 \
--output-dir ./resultspython main.py cli inpaint-mask \
image.jpg mask.jpg \
"a field of flowers" \
--output-dir ./resultspython main.py cli mask-only \
image.jpg "100,150,200,250,300,350" \
--output-dir ./masksfrom src import ImageProcessor
# Initialize processor
processor = ImageProcessor()
# Process image with click coordinates
results = processor.process_click_to_inpaint(
image="image.jpg",
x=150, y=200,
prompt="a beautiful garden",
save_output=True
)
# Access results
original = results['original_image']
mask = results['mask']
inpainted = results['inpainted_image']from src.utils.config import get_config
# Load custom config
config = get_config("custom_config.yaml")
# Override settings
config.set('models.sam.device', 'cuda')
config.set('processing.diffusion.num_inference_steps', 30)- Python 3.8+
- 8GB RAM
- 2GB disk space
- Python 3.9+
- 16GB+ RAM
- NVIDIA GPU with 8GB+ VRAM
- CUDA 11.8+
For faster inference, install CUDA-enabled PyTorch:
# CUDA 11.8
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
# Update config to use GPU
python main.py web --device cuda-
SAM weights not found
# Download SAM weights wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth mkdir -p weights && mv sam_vit_h_4b8939.pth weights/
-
Out of memory errors
# Enable memory optimizations in config performance: enable_memory_efficient_attention: true enable_cpu_offload: true
-
Slow inference
# Use GPU acceleration python main.py web --device cuda # Reduce inference steps python main.py cli inpaint-click image.jpg 100 150 "prompt" --steps 10
Enable debug logging for troubleshooting:
python main.py web --log-level DEBUG# Basic launch
python main.py web
# Advanced launch with custom settings
python main.py web --host 0.0.0.0 --port 8080 --device cuda --share# System information
python main.py info
# Batch processing with advanced settings
python main.py cli inpaint-click \
input.jpg 200 300 "vibrant flowers in a garden" \
--negative "blurry, low quality, distorted" \
--steps 25 --guidance 7.5 \
--output-dir ./batch_results