File size: 5,886 Bytes
6060aa1
 
dd1ad6f
 
 
6060aa1
 
 
 
dd1ad6f
6060aa1
 
dd1ad6f
 
 
 
8a7a521
 
 
 
dd1ad6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bca4cb2
 
dd1ad6f
 
 
 
8a7a521
dd1ad6f
8a7a521
 
 
dd1ad6f
 
8a7a521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1ad6f
 
26e3023
 
 
dd1ad6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---
title: Flux Microscopy Image Enhancement
emoji: πŸ”¬
colorFrom: blue
colorTo: pink
sdk: gradio
sdk_version: 6.3.0
app_file: app.py
pinned: false
license: apache-2.0
---

# πŸ”¬ Flux Microscopy Image Enhancement

An AI-powered microscopy image enhancement tool using the FLUX.2 model. This application provides intelligent image enhancement while preserving cellular structures and fine details.

**Available in two versions:**
- 🌐 **Web UI** (Gradio): Interactive web interface with drag-and-drop support
- ⌨️ **CLI** (Command Line): Batch processing tool for automation and scripting

## ✨ Features

- **Batch Processing**: Process multiple images at once or entire archived folders
- **Archive Support**: Upload ZIP or 7Z files containing multiple images
- **Smart Enhancement**: AI-powered enhancement using FLUX.2-dev with quantization (4-bit)
- **Quality Metrics**: Automatic calculation of PSNR and SSIM to evaluate enhancement quality
- **Custom Prompts**: Customize the enhancement behavior with natural language prompts
- **Adjustable Parameters**: Fine-tune guidance scale and inference steps for optimal results
- **Structured Output**: Download results with `_flux` suffix maintaining original directory structure

## πŸš€ Quick Start

1. **Upload Images**: Upload individual images (JPG, PNG, BMP, TIFF) or compressed archives (ZIP, 7Z)
2. **Customize (Optional)**: Adjust the enhancement prompt and parameters if needed
3. **Process**: Click "Enhance Images" and wait for processing to complete
4. **Download**: Get your enhanced images as a ZIP file with quality metrics

## πŸ–ΌοΈ Supported Formats

### Input Formats
- **Images**: `.jpg`, `.jpeg`, `.png`, `.bmp`, `.tiff`, `.tif`
- **Archives**: `.zip`, `.7z` (automatically extracts and processes all images inside)

### Output Format
- All enhanced images are saved with `_flux` suffix (e.g., `image.png` β†’ `image_flux.png`)
- Results packaged in a ZIP file maintaining original folder structure

## 🎯 Default Enhancement Settings

- **Prompt**: "enhance microscopy image with subtle improvements, gently increase cellular boundary clarity, preserve original morphological structure, maintain authentic texture patterns, minimal noise reduction while keeping fine details intact"
- **Guidance Scale**: 2.0 (conservative for natural enhancement)
- **Inference Steps**: 30 (balanced quality and speed)

## πŸ“Š Quality Metrics

The application automatically calculates two important metrics for each enhanced image:

- **PSNR** (Peak Signal-to-Noise Ratio): Measures pixel-level similarity
  - Higher values indicate better quality
  - > 30 dB is considered good
  
- **SSIM** (Structural Similarity Index): Measures structural similarity
  - Values range from 0 to 1
  - > 0.9 is considered excellent
  - More aligned with human perception than PSNR

## πŸ”§ Parameters

### Guidance Scale (1.0 - 5.0)
Controls the strength of the enhancement:
- **Lower values** (1.0-2.0): More conservative, stays closer to original
- **Higher values** (3.0-5.0): More creative, stronger enhancements
- **Default**: 2.0

### Inference Steps (10 - 50)
Number of processing iterations:
- **Fewer steps** (10-20): Faster processing, lower quality
- **More steps** (30-50): Better quality, slower processing
- **Default**: 30

## πŸ’» Model Information

This application uses:
- **Model**: [diffusers/FLUX.2-dev-bnb-4bit](https://huggingface.co/diffusers/FLUX.2-dev-bnb-4bit)
- **Quantization**: 4-bit bitsandbytes quantization for efficient inference
- **Precision**: bfloat16 for optimal quality/performance balance

## πŸ› οΈ Local Installation

```bash
# Clone the repository
git clone https://huggingface.co/spaces/yichuan-huang/flux-microscopy-image-enhancement
cd flux-microscopy-image-enhancement

# Install dependencies
pip install -r requirements.txt

# Run the web UI
python app.py

# Or use the CLI version
python enhance_cli.py -i input.jpg -o output/
```

## ⌨️ CLI Usage

For batch processing and automation, use the command-line interface:

```bash
# Basic usage
python enhance_cli.py -i input.jpg -o output/

# Process multiple images
python enhance_cli.py -i img1.jpg img2.png img3.tif -o output/

# Process entire directory (recursive)
python enhance_cli.py -i images_folder/ -o output/

# Custom parameters
python enhance_cli.py -i input.jpg -o output/ \
  --guidance-scale 3.0 \
  --steps 40 \
  --prompt "enhance cellular structure"

# Quiet mode (minimal output)
python enhance_cli.py -i input.jpg -o output/ --quiet
```

### CLI Arguments

- `-i, --input`: Input path(s) - image files or directories (required)
- `-o, --output`: Output directory for enhanced images (required)
- `-p, --prompt`: Enhancement prompt (optional)
- `-g, --guidance-scale`: Guidance scale 1.0-5.0 (default: 2.0)
- `-s, --steps`: Inference steps 10-50 (default: 30)
- `-q, --quiet`: Quiet mode - minimal output

## πŸ“‹ Requirements

- Python 3.10+
- CUDA
- ~40GB GPU memory for model inference
- Dependencies listed in `requirements.txt`

## πŸŽ“ Use Cases

- Microscopy image enhancement for research
- Cellular structure visualization
- Biological sample analysis
- Medical imaging preprocessing
- Scientific publication preparation

## ⚠️ Notes

- Processing time depends on image size, number of images, and selected parameters
- GPU acceleration is highly recommended for faster processing
- The model preserves original cellular structures while enhancing clarity
- First run may take longer due to model downloading and caching

## πŸ“ License

Apache 2.0

## 🀝 Contributing

Contributions, issues, and feature requests are welcome!

## πŸ”— Links

- [FLUX.2 Model](https://huggingface.co/diffusers/FLUX.2-dev-bnb-4bit)
- [Diffusers Library](https://github.com/huggingface/diffusers)
- [Gradio Documentation](https://www.gradio.app/docs)

---

Built with ❀️ using Gradio and πŸ€— Hugging Face