Scratch Removal Tool
Overview
This Scratch Removal Tool is the first part of a larger image restoration project. It provides intelligent detection and removal of scratches from images using advanced computer vision techniques. The application combines deep learning-based scratch detection with inpainting algorithms to produce high-quality restored images.
Environment
This application uses a Python environment similar to those used for DeOldify or ESRGAN projects. The primary dependencies include:
- Python 3.10+
- PyTorch 1.10
- OpenCV
- FastAPI
- Streamlit
- PIL (Pillow)
Key Features
- Scratch Detection: Automatically identifies scratches in images using a deep learning model
- Scratch Removal: Applies state-of-the-art inpainting algorithms to remove detected scratches
- Multiple Inpainting Methods: Choose between TELEA, Navier-Stokes (NS), or LaMa (AI-based)
- Adjustable Parameters: Fine-tune threshold, radius, and dilation for optimal results
- Web UI: User-friendly interface for uploading and processing images
Project Structure
api.py
- FastAPI implementation for the backend servicesapp.py
- Streamlit web application for the user interfacedetection.py
- Deep learning model for scratch detectionremoval.py
- Implementation of scratch removal algorithms
Getting Started
Installation
Clone the repository
Create a compatible environment ( preferred with conda )
Install dependencies:
Using conda:
conda env create -f environment.yml conda activate venv
Running the Application
To start both the API server and web interface:
python api.py
And in another terminal:
python -m streamlit run app.py
API Usage
The API provides several endpoints:
/detect/
- Detect scratches in an image/remove/
- Remove scratches using a provided mask/detect-and-remove/
- Perform both detection and removal in one step/status/
- Check API server status and resource usage
Example API call:
import requests
# Detect and remove scratches
url = "http://localhost:8000/detect-and-remove/"
files = {"file": open("image.jpg", "rb")}
params = {
"method": "telea",
"radius": 2,
"threshold": 47,
"dilation": 1,
"use_dilation": True,
"use_lama": False # Set to True to use LAMA AI-based inpainting
}
response = requests.post(url, files=files, data=params)
with open("restored.png", "wb") as f:
f.write(response.content)
Web Interface Usage
- Open the web interface at http://localhost:8501
- Upload an image with scratches
- Click "Detect Scratches" to visualize the detected scratches
- Adjust parameters if needed (including selecting LAMA for higher quality)
- Click "Remove Scratches" to perform restoration
- Download the restored image
Customization Options
- Inpainting Method:
telea
- Alexandru Telea algorithm (better for thin scratches)ns
- Navier-Stokes based method (better for larger areas)LAMA
- AI-based inpainting (best quality but slower)
- Radius: Pixel radius for the inpainting algorithm (1-10, only for OpenCV methods)
- Mask Threshold: Sensitivity of scratch detection (1-254)
- Dilation: Expand detected scratch areas for better coverage (1-5)
Technical Details
Scratch Detection
The detection system uses a U-Net architecture trained on a dataset of scratched images. It identifies scratches with high precision, producing a binary mask that highlights damaged areas.
NOTE: the detection pretrained model retrieved from microsoft bring old photos back to life project releases.
The detection algorithm (detection.py
) works by:
- Loading a pre-trained U-Net model from the
checkpoints/detection/
directory - Processing input images to the appropriate size
- Running inference to produce probability maps highlighting scratch regions
- Post-processing these maps to create binary masks
- Saving the resulting masks as PNG files
Scratch Removal
The removal process uses inpainting algorithms to reconstruct the damaged areas based on surrounding pixels. The tool supports:
- TELEA Algorithm: Fast and effective for thin scratches
- Navier-Stokes: Better quality for larger damaged areas, but slower
- LAMA: AI-based inpainting that provides the highest quality results, especially for complex textures
LAMA Integration
LAMA (Large Mask Inpainting) is an advanced AI-based inpainting algorithm that has been integrated into the tool. It often provides superior results compared to traditional methods, especially for larger areas and complex textures.
- How it works: LAMA uses a deep learning model to fill in masked regions with content that is contextually appropriate
- Performance: LAMA processing is slower than OpenCV methods but produces higher quality results
- Fallback: If LAMA fails for any reason, the system will automatically fall back to the selected OpenCV method
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
- OpenCV for image processing capabilities
- PyTorch for the deep learning framework
- FastAPI and Streamlit for the web components
- LaMa for the advanced inpainting algorithm
- Microsoft's "Bringing Old Photos Back to Life" project for the detection model