Spaces:
Sleeping
Sleeping
initial commit
Browse files- README.md +41 -2
- app.py +426 -0
- models/unet_model.onnx +3 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Pavement Cell Segmentation
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
@@ -10,4 +10,43 @@ pinned: false
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Pavement Cell Segmentation
|
| 3 |
+
emoji: π£οΈ
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# π£οΈ Pavement Cell Segmentation
|
| 14 |
+
|
| 15 |
+
This Hugging Face Space provides an interactive demo for segmenting pavement cells in road surface images using a deep learning model. The model uses a U-Net architecture converted to ONNX format for efficient inference.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- **Image Upload**: Upload pavement images in various formats (JPG, PNG, TIFF, BMP)
|
| 20 |
+
- **Parameter Tuning**: Adjust processing parameters for optimal results
|
| 21 |
+
- **Real-time Processing**: Get segmentation results with visual feedback
|
| 22 |
+
- **Download Results**: Download the segmentation mask as a PNG image
|
| 23 |
+
- **Overlay Visualization**: View the segmentation overlay on the original image
|
| 24 |
+
|
| 25 |
+
## How to Use
|
| 26 |
+
|
| 27 |
+
1. **Upload Image**: Click on the upload area and select a pavement image
|
| 28 |
+
2. **Adjust Parameters**:
|
| 29 |
+
- **Tile Size**: Size of image tiles for processing (128-512 pixels)
|
| 30 |
+
- **Tile Overlap**: Overlap between adjacent tiles (32-128 pixels)
|
| 31 |
+
- **Threshold**: Segmentation threshold for binary mask (0.1-1.0)
|
| 32 |
+
- **Show Overlay**: Toggle overlay visualization
|
| 33 |
+
3. **Process**: Click "Process Image" to run the segmentation
|
| 34 |
+
4. **Download**: Save the segmentation mask using the download button
|
| 35 |
+
|
| 36 |
+
## Parameters Guide
|
| 37 |
+
|
| 38 |
+
### Tile Size
|
| 39 |
+
- **Large tiles (512px)**: Best for high-resolution images, slower processing
|
| 40 |
+
- **Medium tiles (256px)**: Good balance for most images (recommended)
|
| 41 |
+
- **Small tiles (128px)**: Faster processing, may miss larger patterns
|
| 42 |
+
|
| 43 |
+
### Tile Overlap
|
| 44 |
+
- **High overlap (64-128px)**: Better accuracy at tile boundaries, slower processing
|
| 45 |
+
- **Medium overlap (64px)**: Good balance (recommended)
|
| 46 |
+
- **Low overlap (32px)**: Faster processing, may have boundary artifacts
|
| 47 |
+
|
| 48 |
+
### Threshold
|
| 49 |
+
- **High threshold (0.8-0.9)**: Cleaner results, may miss subtle features
|
| 50 |
+
- **Medium threshold (0.6-0.8)**: Good balance (recommended)
|
| 51 |
+
- **Low threshold (0.3-0.6)**: More sensitive detection, may include noise
|
| 52 |
+
|
app.py
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Gradio demo for pavement cell segmentation using ONNX Runtime.
|
| 4 |
+
|
| 5 |
+
This demo allows users to upload pavement images and get segmentation masks
|
| 6 |
+
using a pre-trained U-Net model converted to ONNX format.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import sys
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from typing import Optional, Tuple
|
| 13 |
+
import logging
|
| 14 |
+
import tempfile
|
| 15 |
+
|
| 16 |
+
import numpy as np
|
| 17 |
+
import onnxruntime as ort
|
| 18 |
+
from PIL import Image
|
| 19 |
+
import matplotlib.pyplot as plt
|
| 20 |
+
import gradio as gr
|
| 21 |
+
import io
|
| 22 |
+
|
| 23 |
+
# Configure logging
|
| 24 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 25 |
+
logger = logging.getLogger(__name__)
|
| 26 |
+
|
| 27 |
+
class PavementCellProcessor:
|
| 28 |
+
"""Process pavement cell images using ONNX Runtime."""
|
| 29 |
+
|
| 30 |
+
def __init__(self, model_path: str, device: str = 'cpu'):
|
| 31 |
+
"""Initialize the processor with ONNX model.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
model_path: Path to the ONNX model file
|
| 35 |
+
device: Device to use ('cpu' or 'cuda')
|
| 36 |
+
"""
|
| 37 |
+
self.model_path = model_path
|
| 38 |
+
self.device = device
|
| 39 |
+
self.session = self._load_model()
|
| 40 |
+
|
| 41 |
+
def _load_model(self) -> ort.InferenceSession:
|
| 42 |
+
"""Load ONNX model and create inference session."""
|
| 43 |
+
try:
|
| 44 |
+
# Set up providers based on device
|
| 45 |
+
providers = ['CPUExecutionProvider']
|
| 46 |
+
if self.device == 'cuda' and 'CUDAExecutionProvider' in ort.get_available_providers():
|
| 47 |
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 48 |
+
logger.info("Using CUDA for inference")
|
| 49 |
+
else:
|
| 50 |
+
logger.info("Using CPU for inference")
|
| 51 |
+
|
| 52 |
+
session = ort.InferenceSession(self.model_path, providers=providers)
|
| 53 |
+
logger.info(f"Successfully loaded ONNX model from {self.model_path}")
|
| 54 |
+
return session
|
| 55 |
+
except Exception as e:
|
| 56 |
+
logger.error(f"Failed to load ONNX model: {e}")
|
| 57 |
+
raise e
|
| 58 |
+
|
| 59 |
+
def _pad_image(self, image: Image.Image, tile_size: int, overlap: int) -> Tuple[Image.Image, int, int]:
|
| 60 |
+
"""Pad image to make it divisible into tiles.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
image: Input PIL Image
|
| 64 |
+
tile_size: Size of each tile
|
| 65 |
+
overlap: Overlap between tiles
|
| 66 |
+
|
| 67 |
+
Returns:
|
| 68 |
+
Tuple of (padded_image, pad_width, pad_height)
|
| 69 |
+
"""
|
| 70 |
+
width, height = image.size
|
| 71 |
+
pad_height = (tile_size - (height % (tile_size - overlap))) % (tile_size - overlap)
|
| 72 |
+
pad_width = (tile_size - (width % (tile_size - overlap))) % (tile_size - overlap)
|
| 73 |
+
|
| 74 |
+
logger.debug(f"Original image size: {width}x{height}, Padding: {pad_width}x{pad_height}")
|
| 75 |
+
|
| 76 |
+
# Pad image using reflect mode
|
| 77 |
+
padded_image = Image.new(image.mode, (width + pad_width, height + pad_height))
|
| 78 |
+
padded_image.paste(image, (0, 0))
|
| 79 |
+
|
| 80 |
+
# Fill padded areas by reflecting edges
|
| 81 |
+
if pad_width > 0:
|
| 82 |
+
# Right edge reflection
|
| 83 |
+
for x in range(width, width + pad_width):
|
| 84 |
+
for y in range(height):
|
| 85 |
+
padded_image.putpixel((x, y), image.getpixel((width - 1 - (x - width), y)))
|
| 86 |
+
|
| 87 |
+
if pad_height > 0:
|
| 88 |
+
# Bottom edge reflection
|
| 89 |
+
for y in range(height, height + pad_height):
|
| 90 |
+
for x in range(width + pad_width):
|
| 91 |
+
ref_y = height - 1 - (y - height)
|
| 92 |
+
padded_image.putpixel((x, y), padded_image.getpixel((x, ref_y)))
|
| 93 |
+
|
| 94 |
+
return padded_image, pad_width, pad_height
|
| 95 |
+
|
| 96 |
+
def _preprocess_tile(self, tile: Image.Image, tile_size: int) -> np.ndarray:
|
| 97 |
+
"""Preprocess a tile for model inference.
|
| 98 |
+
|
| 99 |
+
Args:
|
| 100 |
+
tile: PIL Image tile
|
| 101 |
+
tile_size: Expected tile size
|
| 102 |
+
|
| 103 |
+
Returns:
|
| 104 |
+
Preprocessed numpy array ready for inference
|
| 105 |
+
"""
|
| 106 |
+
# Ensure tile is the correct size
|
| 107 |
+
if tile.size != (tile_size, tile_size):
|
| 108 |
+
# Pad tile to tile_size x tile_size
|
| 109 |
+
padded_tile = Image.new('RGB', (tile_size, tile_size), (0, 0, 0))
|
| 110 |
+
padded_tile.paste(tile, (0, 0))
|
| 111 |
+
tile = padded_tile
|
| 112 |
+
|
| 113 |
+
# Convert to numpy array and normalize
|
| 114 |
+
tile_array = np.array(tile).astype(np.float32) / 255.0
|
| 115 |
+
|
| 116 |
+
# Convert from HWC to CHW format (channels first)
|
| 117 |
+
tile_array = np.transpose(tile_array, (2, 0, 1))
|
| 118 |
+
|
| 119 |
+
# Add batch dimension
|
| 120 |
+
tile_array = np.expand_dims(tile_array, axis=0)
|
| 121 |
+
|
| 122 |
+
return tile_array
|
| 123 |
+
|
| 124 |
+
def predict_mask_full_image(self, image: Image.Image, tile_size: int = 256,
|
| 125 |
+
overlap: int = 64, threshold: float = 0.9) -> np.ndarray:
|
| 126 |
+
"""Predict segmentation mask for full image using tiled inference.
|
| 127 |
+
|
| 128 |
+
Args:
|
| 129 |
+
image: Input PIL Image
|
| 130 |
+
tile_size: Size of each tile for processing
|
| 131 |
+
overlap: Overlap between adjacent tiles
|
| 132 |
+
threshold: Threshold for binary segmentation
|
| 133 |
+
|
| 134 |
+
Returns:
|
| 135 |
+
Segmentation mask as numpy array
|
| 136 |
+
"""
|
| 137 |
+
# Pad image
|
| 138 |
+
padded_image, pad_width, pad_height = self._pad_image(image, tile_size, overlap)
|
| 139 |
+
original_width, original_height = image.size
|
| 140 |
+
|
| 141 |
+
logger.debug(f"Padded image size: {padded_image.width}x{padded_image.height}")
|
| 142 |
+
|
| 143 |
+
# Initialize output arrays
|
| 144 |
+
full_mask = np.zeros((padded_image.height, padded_image.width))
|
| 145 |
+
count_map = np.zeros((padded_image.height, padded_image.width))
|
| 146 |
+
|
| 147 |
+
tile_count = 0
|
| 148 |
+
|
| 149 |
+
# Process tiles
|
| 150 |
+
for y in range(0, padded_image.height - overlap, tile_size - overlap):
|
| 151 |
+
for x in range(0, padded_image.width - overlap, tile_size - overlap):
|
| 152 |
+
tile_count += 1
|
| 153 |
+
|
| 154 |
+
# Extract tile
|
| 155 |
+
tile = padded_image.crop((
|
| 156 |
+
x, y,
|
| 157 |
+
min(x + tile_size, padded_image.width),
|
| 158 |
+
min(y + tile_size, padded_image.height)
|
| 159 |
+
))
|
| 160 |
+
|
| 161 |
+
# Preprocess tile
|
| 162 |
+
tile_array = self._preprocess_tile(tile, tile_size)
|
| 163 |
+
|
| 164 |
+
# Run inference
|
| 165 |
+
input_name = self.session.get_inputs()[0].name
|
| 166 |
+
output_name = self.session.get_outputs()[0].name
|
| 167 |
+
|
| 168 |
+
outputs = self.session.run([output_name], {input_name: tile_array})
|
| 169 |
+
tile_mask_logits = outputs[0].squeeze()
|
| 170 |
+
|
| 171 |
+
# Apply sigmoid and threshold
|
| 172 |
+
tile_mask = 1 / (1 + np.exp(-tile_mask_logits)) # Sigmoid
|
| 173 |
+
tile_mask = (tile_mask > threshold).astype(np.float32)
|
| 174 |
+
tile_mask = 1 - tile_mask # Invert mask
|
| 175 |
+
|
| 176 |
+
# Resize mask back to original tile size if padding was added
|
| 177 |
+
tile_mask_resized = tile_mask[:tile.height, :tile.width]
|
| 178 |
+
|
| 179 |
+
# Update full mask and count map
|
| 180 |
+
full_mask[y:y + tile.height, x:x + tile.width] += tile_mask_resized
|
| 181 |
+
count_map[y:y + tile.height, x:x + tile.width] += 1
|
| 182 |
+
|
| 183 |
+
# Average the overlaps
|
| 184 |
+
full_mask = np.divide(full_mask, count_map, out=np.zeros_like(full_mask), where=count_map!=0)
|
| 185 |
+
|
| 186 |
+
# Crop out padding to get final mask
|
| 187 |
+
final_mask = full_mask[:original_height, :original_width]
|
| 188 |
+
|
| 189 |
+
logger.info(f"Processed {tile_count} tiles")
|
| 190 |
+
|
| 191 |
+
return final_mask
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# Initialize the processor
|
| 195 |
+
MODEL_PATH = "models/unet_model.onnx"
|
| 196 |
+
processor = None
|
| 197 |
+
|
| 198 |
+
def load_processor():
|
| 199 |
+
"""Load the processor with error handling."""
|
| 200 |
+
global processor
|
| 201 |
+
try:
|
| 202 |
+
processor = PavementCellProcessor(MODEL_PATH, device='cpu')
|
| 203 |
+
return True
|
| 204 |
+
except Exception as e:
|
| 205 |
+
logger.error(f"Failed to load processor: {e}")
|
| 206 |
+
return False
|
| 207 |
+
|
| 208 |
+
def process_image(image, tile_size, overlap, threshold, show_overlay):
|
| 209 |
+
"""Process uploaded image and return results."""
|
| 210 |
+
global processor
|
| 211 |
+
|
| 212 |
+
if processor is None:
|
| 213 |
+
if not load_processor():
|
| 214 |
+
return None, None, "β Failed to load model. Please try again."
|
| 215 |
+
|
| 216 |
+
try:
|
| 217 |
+
# Convert to PIL if needed
|
| 218 |
+
if not isinstance(image, Image.Image):
|
| 219 |
+
image = Image.fromarray(image).convert('RGB')
|
| 220 |
+
else:
|
| 221 |
+
image = image.convert('RGB')
|
| 222 |
+
|
| 223 |
+
# Process image
|
| 224 |
+
mask = processor.predict_mask_full_image(
|
| 225 |
+
image,
|
| 226 |
+
tile_size=int(tile_size),
|
| 227 |
+
overlap=int(overlap),
|
| 228 |
+
threshold=threshold
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
# Create visualizations
|
| 232 |
+
fig, axes = plt.subplots(1, 2 if not show_overlay else 3, figsize=(15 if show_overlay else 10, 5))
|
| 233 |
+
|
| 234 |
+
if not show_overlay:
|
| 235 |
+
axes[0].imshow(image)
|
| 236 |
+
axes[0].set_title("Original Image")
|
| 237 |
+
axes[0].axis('off')
|
| 238 |
+
|
| 239 |
+
axes[1].imshow(mask, cmap='gray')
|
| 240 |
+
axes[1].set_title("Segmentation Mask")
|
| 241 |
+
axes[1].axis('off')
|
| 242 |
+
else:
|
| 243 |
+
axes[0].imshow(image)
|
| 244 |
+
axes[0].set_title("Original Image")
|
| 245 |
+
axes[0].axis('off')
|
| 246 |
+
|
| 247 |
+
axes[1].imshow(mask, cmap='gray')
|
| 248 |
+
axes[1].set_title("Segmentation Mask")
|
| 249 |
+
axes[1].axis('off')
|
| 250 |
+
|
| 251 |
+
axes[2].imshow(image)
|
| 252 |
+
axes[2].imshow(mask, alpha=0.5, cmap='Reds')
|
| 253 |
+
axes[2].set_title("Overlay")
|
| 254 |
+
axes[2].axis('off')
|
| 255 |
+
|
| 256 |
+
plt.tight_layout()
|
| 257 |
+
|
| 258 |
+
# Save plot to bytes for display
|
| 259 |
+
buf = io.BytesIO()
|
| 260 |
+
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
|
| 261 |
+
buf.seek(0)
|
| 262 |
+
plt.close()
|
| 263 |
+
|
| 264 |
+
# Create downloadable mask image
|
| 265 |
+
mask_image = Image.fromarray((mask * 255).astype(np.uint8), mode='L')
|
| 266 |
+
|
| 267 |
+
# Save mask to temporary file for download
|
| 268 |
+
temp_dir = tempfile.mkdtemp()
|
| 269 |
+
mask_path = os.path.join(temp_dir, "segmentation_mask.png")
|
| 270 |
+
mask_image.save(mask_path)
|
| 271 |
+
|
| 272 |
+
status = f"β
Successfully processed image! Found pavement cells in the segmentation mask."
|
| 273 |
+
|
| 274 |
+
return buf.getvalue(), mask_path, status
|
| 275 |
+
|
| 276 |
+
except Exception as e:
|
| 277 |
+
logger.error(f"Error processing image: {e}")
|
| 278 |
+
return None, None, f"β Error processing image: {str(e)}"
|
| 279 |
+
|
| 280 |
+
def create_demo():
|
| 281 |
+
"""Create the Gradio demo interface."""
|
| 282 |
+
|
| 283 |
+
with gr.Blocks(
|
| 284 |
+
title="Pavement Cell Segmentation",
|
| 285 |
+
theme=gr.themes.Soft(),
|
| 286 |
+
css="""
|
| 287 |
+
.gradio-container {
|
| 288 |
+
max-width: 1200px !important;
|
| 289 |
+
}
|
| 290 |
+
"""
|
| 291 |
+
) as demo:
|
| 292 |
+
|
| 293 |
+
gr.Markdown("""
|
| 294 |
+
# π£οΈ Pavement Cell Segmentation
|
| 295 |
+
|
| 296 |
+
Upload an image of pavement to detect and segment pavement cells using a deep learning model.
|
| 297 |
+
|
| 298 |
+
**How to use:**
|
| 299 |
+
1. Upload a pavement image
|
| 300 |
+
2. Adjust the processing parameters if needed
|
| 301 |
+
3. Click "Process Image"
|
| 302 |
+
4. View the results and download the segmentation mask
|
| 303 |
+
""")
|
| 304 |
+
|
| 305 |
+
with gr.Row():
|
| 306 |
+
with gr.Column(scale=1):
|
| 307 |
+
gr.Markdown("### π€ Input")
|
| 308 |
+
|
| 309 |
+
image_input = gr.Image(
|
| 310 |
+
label="Upload Pavement Image",
|
| 311 |
+
type="pil",
|
| 312 |
+
sources=["upload"],
|
| 313 |
+
height=300
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
gr.Markdown("### βοΈ Parameters")
|
| 317 |
+
|
| 318 |
+
with gr.Row():
|
| 319 |
+
tile_size = gr.Slider(
|
| 320 |
+
label="Tile Size",
|
| 321 |
+
minimum=128,
|
| 322 |
+
maximum=512,
|
| 323 |
+
step=64,
|
| 324 |
+
value=256,
|
| 325 |
+
info="Size of image tiles for processing"
|
| 326 |
+
)
|
| 327 |
+
|
| 328 |
+
overlap = gr.Slider(
|
| 329 |
+
label="Tile Overlap",
|
| 330 |
+
minimum=32,
|
| 331 |
+
maximum=128,
|
| 332 |
+
step=16,
|
| 333 |
+
value=64,
|
| 334 |
+
info="Overlap between tiles"
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
threshold = gr.Slider(
|
| 338 |
+
label="Segmentation Threshold",
|
| 339 |
+
minimum=0.1,
|
| 340 |
+
maximum=1.0,
|
| 341 |
+
step=0.05,
|
| 342 |
+
value=0.9,
|
| 343 |
+
info="Threshold for binary segmentation"
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
show_overlay = gr.Checkbox(
|
| 347 |
+
label="Show Overlay",
|
| 348 |
+
value=True,
|
| 349 |
+
info="Show overlay of mask on original image"
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
process_btn = gr.Button(
|
| 353 |
+
"π Process Image",
|
| 354 |
+
variant="primary",
|
| 355 |
+
size="lg"
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
with gr.Column(scale=2):
|
| 359 |
+
gr.Markdown("### π Results")
|
| 360 |
+
|
| 361 |
+
status_text = gr.Textbox(
|
| 362 |
+
label="Status",
|
| 363 |
+
value="Ready to process images...",
|
| 364 |
+
interactive=False,
|
| 365 |
+
max_lines=3
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
result_image = gr.Image(
|
| 369 |
+
label="Segmentation Results",
|
| 370 |
+
type="pil",
|
| 371 |
+
height=400
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
download_file = gr.File(
|
| 375 |
+
label="π₯ Download Segmentation Mask",
|
| 376 |
+
visible=False
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
# Event handlers
|
| 380 |
+
process_btn.click(
|
| 381 |
+
fn=process_image,
|
| 382 |
+
inputs=[image_input, tile_size, overlap, threshold, show_overlay],
|
| 383 |
+
outputs=[result_image, download_file, status_text],
|
| 384 |
+
show_progress=True
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
# Update download file visibility when result is ready
|
| 388 |
+
def update_download_visibility(result, download):
|
| 389 |
+
if result is not None and download is not None:
|
| 390 |
+
return gr.File(visible=True, value=download)
|
| 391 |
+
return gr.File(visible=False)
|
| 392 |
+
|
| 393 |
+
result_image.change(
|
| 394 |
+
fn=update_download_visibility,
|
| 395 |
+
inputs=[result_image, download_file],
|
| 396 |
+
outputs=[download_file]
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
# Example images section
|
| 400 |
+
gr.Markdown("### π Example Usage")
|
| 401 |
+
gr.Markdown("""
|
| 402 |
+
**Parameter Guidelines:**
|
| 403 |
+
- **Tile Size**: Larger tiles (512) for high-resolution images, smaller tiles (256) for standard images
|
| 404 |
+
- **Overlap**: More overlap (64-128) for better accuracy, less overlap (32-64) for faster processing
|
| 405 |
+
- **Threshold**: Higher values (0.8-0.9) for cleaner results, lower values (0.5-0.7) for more sensitive detection
|
| 406 |
+
|
| 407 |
+
**Supported Formats:** JPG, PNG, TIFF, BMP
|
| 408 |
+
""")
|
| 409 |
+
|
| 410 |
+
return demo
|
| 411 |
+
|
| 412 |
+
if __name__ == "__main__":
|
| 413 |
+
# Load the processor at startup
|
| 414 |
+
logger.info("Loading pavement cell segmentation model...")
|
| 415 |
+
if load_processor():
|
| 416 |
+
logger.info("Model loaded successfully!")
|
| 417 |
+
else:
|
| 418 |
+
logger.error("Failed to load model at startup")
|
| 419 |
+
|
| 420 |
+
# Create and launch demo
|
| 421 |
+
demo = create_demo()
|
| 422 |
+
demo.launch(
|
| 423 |
+
server_name="0.0.0.0",
|
| 424 |
+
server_port=7860,
|
| 425 |
+
share=False
|
| 426 |
+
)
|
models/unet_model.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b8b51a0b219ed40c128147e1bd754765e2be91ed335b4f5f76647f726c4c9619
|
| 3 |
+
size 69068260
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.44.1
|
| 2 |
+
numpy>=1.21.0
|
| 3 |
+
onnxruntime>=1.12.0
|
| 4 |
+
pillow>=8.3.0
|
| 5 |
+
matplotlib>=3.5.0
|
| 6 |
+
tqdm>=4.62.0
|