Skip to main content

Professional image and video deblurring using NAFNet

Project description

neuro-deblur ๐Ÿš€

Professional image and video deblurring using NAFNet (Nonlinear Activation Free Network).

A production-ready Python package for motion deblurring with CUDA acceleration support. Perfect for enhancing blurry photos and videos with state-of-the-art deep learning.

Python 3.9+ PyPI version License: MIT


๐ŸŒŸ Features

  • โœ… Image Deblurring - Restore sharp details from blurry images
  • โœ… Video Deblurring - Process videos frame-by-frame
  • โœ… CUDA Acceleration - GPU support for fast processing
  • โœ… CPU Fallback - Works on systems without GPU
  • โœ… Simple API - 3 lines of code to deblur
  • โœ… Pre-trained Model - Ready to use out of the box
  • โœ… Production Ready - Clean, tested, and documented

๐Ÿ“ฆ Installation

From PyPI (Recommended)

pip install neuro-deblur

Note: Model weights (~334 MB) will be downloaded automatically on first use.

From Source

git clone https://github.com/Parshva2605/neuro-deblur.git
cd neuro-deblur
pip install -e .

Requirements

  • Python >= 3.9
  • PyTorch >= 2.0.0
  • CUDA (optional, for GPU acceleration)

First Use - Automatic Model Download

On first use, the model weights will be downloaded automatically:

from neuro_deblur import DeblurModel

# First time - downloads model (~334 MB, one-time only)
model = DeblurModel(device="cuda")
# Downloading model weights from GitHub...
# best_model.pth: 100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 334MB/334MB [30s]
# โœ“ Model downloaded successfully!

# Later uses - instant (uses cached model)
model = DeblurModel(device="cuda")
# โœ“ Using cached model

Cache Location:

  • Windows: C:\Users\YourName\AppData\Local\neuro_deblur\weights\
  • Linux/Mac: ~/.cache/neuro_deblur/weights/

๐Ÿš€ Quick Start

Deblur an Image

from neuro_deblur import DeblurModel

# Initialize model (auto-detects CUDA)
model = DeblurModel(device="cuda")

# Deblur image
model.deblur_image("blurry_photo.jpg", "sharp_photo.jpg")

Deblur a Video

from neuro_deblur import DeblurModel

# Initialize model
model = DeblurModel(device="cuda")

# Deblur video (with progress bar)
model.deblur_video("blurry_video.mp4", "sharp_video.mp4")

Process Multiple Images

from neuro_deblur import DeblurModel

# Initialize model
model = DeblurModel(device="cuda")

# Deblur all images in a folder
model.deblur_folder("input_folder/", "output_folder/")

๐Ÿ’ก Usage Examples

Command Line (Using Examples)

Image Processing

cd examples
python run_image.py input.jpg output.jpg
python run_image.py input.jpg output.jpg --device cpu

Video Processing

cd examples
python run_video.py input.mp4 output.mp4
python run_video.py input.mp4 output.mp4 --device cuda

Python API

Basic Usage

from neuro_deblur import DeblurModel

# Auto-detect device (prefers CUDA)
model = DeblurModel()

# Or specify device explicitly
model = DeblurModel(device="cuda")  # Use GPU
model = DeblurModel(device="cpu")   # Use CPU

# Deblur image
output = model.deblur_image("input.jpg", "output.jpg")

Advanced Usage

from neuro_deblur import DeblurModel
from pathlib import Path

# Use custom checkpoint
model = DeblurModel(
    checkpoint_path="path/to/custom_model.pth",
    device="cuda"
)

# Process without saving (get tensor)
output_tensor = model.deblur_image("input.jpg")

# Process multiple videos
video_files = Path("videos/").glob("*.mp4")
for video in video_files:
    output = f"deblurred/{video.name}"
    model.deblur_video(video, output)

Direct Tensor Processing

import torch
from neuro_deblur import DeblurModel

model = DeblurModel(device="cuda")

# Process tensor directly
input_tensor = torch.rand(1, 3, 256, 256)  # [B, C, H, W]
output_tensor = model.process_tensor(input_tensor)

๐Ÿ–ฅ๏ธ GPU Support

Automatic CUDA Detection

The package automatically detects and uses CUDA if available:

# Auto-detects GPU
model = DeblurModel()  # Uses CUDA if available, else CPU

# Force CPU
model = DeblurModel(device="cpu")

# Force GPU (raises error if unavailable)
model = DeblurModel(device="cuda")

Performance Benchmarks

Processing a 1920ร—1080 video on RTX 3070:

Device FPS Speed
CUDA ~3-5 FPS 20-30x faster
CPU ~0.1-0.2 FPS Baseline

Recommendation: Use GPU for videos, CPU works fine for images.


๐Ÿ“Š Model Details

NAFNet Architecture

  • Paper: Simple Baselines for Image Restoration
  • Architecture: NAFNet (Nonlinear Activation Free Network)
  • Parameters: ~29M parameters
  • Training: GoPro dataset (motion deblurring)
  • Performance: 30.33 dB PSNR on validation set

Model Configuration

NAFNet(
    width=32,
    middle_blk_num=12,
    enc_blk_nums=[2, 2, 4, 8],
    dec_blk_nums=[2, 2, 2, 2]
)

๐Ÿ“ Project Structure

neuro_deblur/
โ”œโ”€โ”€ neuro_deblur/
โ”‚   โ”œโ”€โ”€ __init__.py       # Public API
โ”‚   โ”œโ”€โ”€ model.py          # NAFNet architecture
โ”‚   โ”œโ”€โ”€ inference.py      # DeblurModel class
โ”‚   โ”œโ”€โ”€ utils.py          # Image/video utilities
โ”‚   โ””โ”€โ”€ weights/
โ”‚       โ””โ”€โ”€ best_model.pth  # Pre-trained weights
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ run_image.py      # Image deblurring example
โ”‚   โ””โ”€โ”€ run_video.py      # Video deblurring example
โ”œโ”€โ”€ setup.py              # Package setup
โ”œโ”€โ”€ pyproject.toml        # Build configuration
โ”œโ”€โ”€ requirements.txt      # Dependencies
โ”œโ”€โ”€ MANIFEST.in           # Include data files
โ”œโ”€โ”€ README.md             # This file
โ””โ”€โ”€ LICENSE               # MIT License

๐Ÿ› ๏ธ Development

Install in Development Mode

git clone https://github.com/yourusername/neuro-deblur.git
cd neuro-deblur
pip install -e ".[dev]"

Run Tests

pytest tests/

Code Formatting

black neuro_deblur/
flake8 neuro_deblur/

๐Ÿ“ API Reference

DeblurModel

Main class for deblurring operations.

Constructor

DeblurModel(checkpoint_path=None, device="cuda", width=32)

Parameters:

  • checkpoint_path (str, optional): Path to custom checkpoint. Uses bundled weights if None.
  • device (str): Device to use ("cuda" or "cpu"). Auto-detects if CUDA unavailable.
  • width (int): Model width (must match training, default: 32).

Methods

deblur_image(input_path, save_path=None)

Deblur a single image.

Parameters:

  • input_path (str): Path to blurry image
  • save_path (str, optional): Path to save output

Returns: torch.Tensor - Deblurred image tensor [1, 3, H, W]

deblur_video(input_path, output_path, show_progress=True)

Deblur a video file.

Parameters:

  • input_path (str): Path to blurry video
  • output_path (str): Path to save output
  • show_progress (bool): Show progress bar
deblur_folder(input_folder, output_folder, extensions=(...), show_progress=True)

Deblur all images in a folder.

Parameters:

  • input_folder (str): Input folder path
  • output_folder (str): Output folder path
  • extensions (tuple): Valid image extensions
  • show_progress (bool): Show progress bar
process_tensor(input_tensor)

Process a tensor directly.

Parameters:

  • input_tensor (torch.Tensor): Input tensor [1, 3, H, W] or [3, H, W]

Returns: torch.Tensor - Deblurred tensor, clamped to [0, 1]


๐ŸŽฏ Use Cases

  • Photography: Restore blurry photos (motion blur, camera shake)
  • Video Enhancement: Stabilize and sharpen video footage
  • Security Footage: Enhance low-quality surveillance videos
  • Medical Imaging: Improve clarity of medical scans
  • Autonomous Vehicles: Preprocess camera feeds
  • Sports Analytics: Enhance fast-motion sports footage

๐Ÿค Contributing

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

  1. Fork the repository at github.com/Parshva2605/neuro-deblur
  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


๐Ÿ“ง Contact

Parshva Shah - shahparshva2005@gmail.com

Project Link: https://github.com/Parshva2605/neuro-deblur


โญ Star History

If you find this project useful, please consider giving it a star! โญ


Made with โค๏ธ for the computer vision community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neuro_deblur-0.1.1.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neuro_deblur-0.1.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file neuro_deblur-0.1.1.tar.gz.

File metadata

  • Download URL: neuro_deblur-0.1.1.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for neuro_deblur-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ac8f388543721961f4c88637949c777dde5ad559376315b5d5c54cc55f359933
MD5 dae8628a9f7909913a082457efcf4f9e
BLAKE2b-256 37e54430d6c3c1e1bcc77a56132db9df830a61b618ab6ea29c0a5181ad3ca71d

See more details on using hashes here.

File details

Details for the file neuro_deblur-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: neuro_deblur-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for neuro_deblur-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f43e01f7492b984446de7e3b136e75437d8f89710f90eb729f312e5c212d5960
MD5 22cc65f1d5675b7decb36798833f98f2
BLAKE2b-256 0ac71fa54282f720562861c8fff5ffcb148506ee30826d0a540d9197de8931a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page