CoolFace
Apppublic

sila0/image-enhancement

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
App README

๐Ÿ–ผ๏ธ Image Enhancement API

A FastAPI-powered image enhancement application that processes images in LAB color space using classical computer vision techniques โ€” no deep learning required.
โš ๏ธ Warning: This app is designed for desktop / computer use only. The UI is not optimized for mobile devices โ€” please open it on a computer for the best experience.

Live demo: https://sila0-image-enhancement.hf.space


Overview

This application exposes a REST API and a browser-based UI for enhancing low-quality images. It applies advanced image processing algorithms in the LAB color space, operating only on the L (luminance) channel to preserve natural colors while improving brightness, contrast, and sharpness.

Three enhancement modes are available, ranging from quick contrast correction to a full 6-stage professional pipeline.


Enhancement Modes

clahe โ€” Contrast Limited Adaptive Histogram Equalization

Divides the image into small tiles and equalizes the histogram of each tile independently. The clipLimit parameter prevents over-amplification of noise. Ideal for images with uneven lighting.

bilateral โ€” Bilateral Filtering

A non-linear smoothing filter that preserves edges while reducing noise. Unlike Gaussian blur, it considers both spatial proximity and pixel intensity similarity โ€” producing a "clean" look without losing structure.

combined โ€” Face-Focused Enhancement Pipeline

Designed for blurry / low-light face crops. Four stages:

StageTechniquePurpose
1Non-local Means Denoising (color-aware)Remove noise while preserving texture
2CLAHE on L channelBoost contrast without shifting hue
3Unsharp maskPull out edge / contour detail
4FSRCNN x2 super-resolution2ร— upscale via a tiny (~38 KB) ONNX-style model; falls back to bicubic if the model file is missing

The FSRCNN weights are downloaded on first use to models/FSRCNN_x2.pb.


API Endpoints

POST /enhance

Upload an image and download the enhanced version.

Parameters:

  • โ€”file โ€” JPG or PNG image
  • โ€”mode โ€” clahe | bilateral | combined (default: clahe)

Response: Enhanced image as JPEG download.


POST /enhance/metrics

Upload an image and receive the enhanced image + quality metrics in JSON.

Response:

json
{
  "mode": "clahe",
  "original_size": { "width": 1920, "height": 1080 },
  "metrics": {
    "ssim": 0.874,
    "psnr": 32.5
  },
  "enhanced_image": "<base64 encoded JPEG>"
}

Metrics explained:

  • โ€”SSIM (Structural Similarity Index) โ€” measures perceptual similarity between original and enhanced image. Range: 0โ€“1, higher = more similar structure preserved.
  • โ€”PSNR (Peak Signal-to-Noise Ratio) โ€” measures reconstruction quality in dB. Higher values indicate less distortion introduced by enhancement.

POST /chat

Chat with the Image Enhancement Assistant โ€” an AI guide that explains techniques (CLAHE, MSRCR, FFT, LAB color space), interprets metrics, and recommends the best mode for a given image.

Request body (JSON):

json
{
  "messages": [
    { "role": "user", "content": "Why is my SSIM so low?" }
  ],
  "context": "Mode used: combined | PSNR: 28.4 dB | SSIM: 0.71"
}
  • โ€”messages โ€” chat history, alternating user / assistant
  • โ€”context โ€” optional session context (current mode, metrics) so the assistant can answer with concrete numbers

Response: plain-text streamed response (text/plain; charset=utf-8). The assistant runs on Anthropic's Claude API. Set ANTHROPIC_API_KEY either in a .env file (recommended โ€” see .env.example) or as a shell environment variable.

The browser UI exposes this as a floating speech-bubble button in the bottom-right corner โ€” click it to open the chat panel. After enhancing an image, the panel automatically receives session context (current mode + metrics) so the assistant can answer with concrete numbers.


LAB Color Space

All processing is performed in CIE LAB color space:

BGR  โ†’  LAB  โ†’  [ enhance L channel only ]  โ†’  BGR
  • โ€”L โ€” Lightness (0 = black, 255 = white) โ€” this is what we modify
  • โ€”A โ€” Green โ†” Red color axis
  • โ€”B โ€” Blue โ†” Yellow color axis

By isolating the L channel, enhancements affect only brightness and contrast โ€” color hues remain untouched, preventing the color distortion common in naive histogram equalization.


Project Structure

my ai proje/
โ”œโ”€โ”€ main.py            # FastAPI app โ€” routes and endpoint logic
โ”œโ”€โ”€ lab_processing.py  # All image processing algorithms
โ”œโ”€โ”€ metrics.py         # SSIM / PSNR computation
โ”œโ”€โ”€ chat.py            # Claude-powered Image Enhancement Assistant
โ”œโ”€โ”€ requirements.txt   # Python dependencies
โ”œโ”€โ”€ start.bat          # One-click launcher (Windows)
โ””โ”€โ”€ static/
    โ”œโ”€โ”€ index.html     # Browser UI (incl. chat panel)
    โ”œโ”€โ”€ script.js      # Frontend logic
    โ””โ”€โ”€ style.css      # Styling

Tech Stack

ToolPurpose
PythonCore language
FastAPIREST API framework
OpenCVImage processing (CLAHE, bilateral, FFT, etc.)
NumPyArray operations and mathematical transforms
scikit-imageSSIM metric computation
Anthropic SDKClaude API for the in-app Image Enhancement Assistant

How to Run

bash
# 1. Clone the repository
git clone https://github.com/slasert/image-enhancement.git
cd image-enhancement

# 2. Install dependencies
pip install -r requirements.txt

# 3. Set your Anthropic API key (required for /chat โ€” the AI assistant)
#    Easiest: copy .env.example to .env and put your key in it.
cp .env.example .env
#    Then edit .env and replace the placeholder. python-dotenv loads it
#    automatically at server startup.
#
#    Or export it as a shell env var instead:
#      macOS / Linux:   export ANTHROPIC_API_KEY=sk-ant-...
#      Windows (cmd):   set ANTHROPIC_API_KEY=sk-ant-...
#      Windows (PS):    $env:ANTHROPIC_API_KEY = "sk-ant-..."

# 4. Start the server
uvicorn main:app --reload

# 5. Open in browser
# โ†’ http://localhost:8000
# โ†’ http://localhost:8000/docs  (interactive API docs)

Or on Windows, simply double-click `start.bat`. (The /chat endpoint will return an error message until ANTHROPIC_API_KEY is set; the rest of the app works without it.)


Interactive API Docs

FastAPI automatically generates interactive documentation at:

  • โ€”Swagger UI โ†’ http://localhost:8000/docs
  • โ€”ReDoc โ†’ http://localhost:8000/redoc

You can test all endpoints directly from the browser without any additional tools.


Built with OpenCV and FastAPI โ€” classical computer vision, no deep learning required.