RostiS's picture
Add .gitignore file and enhance image processing in app.py
cf45ac5
Raw History Blame Contribute Delete
21 kB
#!/usr/bin/env python3
"""
Gradio demo for pavement cell segmentation using ONNX Runtime.
This demo allows users to upload pavement images and get segmentation masks
using a pre-trained U-Net model converted to ONNX format.
"""
import os
from typing import Tuple
import logging
import tempfile
import numpy as np
import onnxruntime as ort
from PIL import Image
import gradio as gr
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class PavementCellProcessor:
"""Process pavement cell images using ONNX Runtime."""
def __init__(self, model_path: str, device: str = 'cpu'):
"""Initialize the processor with ONNX model.
Args:
model_path: Path to the ONNX model file
device: Device to use ('cpu' or 'cuda')
"""
self.model_path = model_path
self.device = device
self.session = self._load_model()
def _load_model(self) -> ort.InferenceSession:
"""Load ONNX model and create inference session."""
try:
# Set up providers based on device
providers = ['CPUExecutionProvider']
if self.device == 'cuda' and 'CUDAExecutionProvider' in ort.get_available_providers():
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
logger.info("Using CUDA for inference")
else:
logger.info("Using CPU for inference")
session = ort.InferenceSession(self.model_path, providers=providers)
logger.info(f"Successfully loaded ONNX model from {self.model_path}")
return session
except Exception as e:
logger.error(f"Failed to load ONNX model: {e}")
raise e
def _pad_image(self, image: Image.Image, tile_size: int, overlap: int) -> Tuple[Image.Image, int, int]:
"""Pad image to make it divisible into tiles.
Args:
image: Input PIL Image
tile_size: Size of each tile
overlap: Overlap between tiles
Returns:
Tuple of (padded_image, pad_width, pad_height)
"""
width, height = image.size
pad_height = (tile_size - (height % (tile_size - overlap))) % (tile_size - overlap)
pad_width = (tile_size - (width % (tile_size - overlap))) % (tile_size - overlap)
logger.debug(f"Original image size: {width}x{height}, Padding: {pad_width}x{pad_height}")
# Pad image using reflect mode
padded_image = Image.new(image.mode, (width + pad_width, height + pad_height))
padded_image.paste(image, (0, 0))
# Fill padded areas by reflecting edges
if pad_width > 0:
# Right edge reflection
for x in range(width, width + pad_width):
for y in range(height):
padded_image.putpixel((x, y), image.getpixel((width - 1 - (x - width), y)))
if pad_height > 0:
# Bottom edge reflection
for y in range(height, height + pad_height):
for x in range(width + pad_width):
ref_y = height - 1 - (y - height)
padded_image.putpixel((x, y), padded_image.getpixel((x, ref_y)))
return padded_image, pad_width, pad_height
def _preprocess_tile(self, tile: Image.Image, tile_size: int) -> np.ndarray:
"""Preprocess a tile for model inference.
Args:
tile: PIL Image tile
tile_size: Expected tile size
Returns:
Preprocessed numpy array ready for inference
"""
# Ensure tile is the correct size
if tile.size != (tile_size, tile_size):
# Pad tile to tile_size x tile_size
padded_tile = Image.new('RGB', (tile_size, tile_size), (0, 0, 0))
padded_tile.paste(tile, (0, 0))
tile = padded_tile
# Convert to numpy array and normalize
tile_array = np.array(tile).astype(np.float32) / 255.0
# Convert from HWC to CHW format (channels first)
tile_array = np.transpose(tile_array, (2, 0, 1))
# Add batch dimension
tile_array = np.expand_dims(tile_array, axis=0)
return tile_array
def predict_mask_full_image(self, image: Image.Image, tile_size: int = 256,
overlap: int = 64, threshold: float = 0.9) -> Tuple[np.ndarray, int]:
"""Predict segmentation mask for full image using tiled inference.
Args:
image: Input PIL Image
tile_size: Size of each tile for processing
overlap: Overlap between adjacent tiles
threshold: Threshold for binary segmentation
Returns:
Tuple of (segmentation mask as numpy array, processed tile count)
"""
# Pad image
padded_image, pad_width, pad_height = self._pad_image(image, tile_size, overlap)
original_width, original_height = image.size
logger.debug(f"Padded image size: {padded_image.width}x{padded_image.height}")
# Initialize output arrays
full_mask = np.zeros((padded_image.height, padded_image.width))
count_map = np.zeros((padded_image.height, padded_image.width))
tile_count = 0
# Process tiles
for y in range(0, padded_image.height - overlap, tile_size - overlap):
for x in range(0, padded_image.width - overlap, tile_size - overlap):
tile_count += 1
# Extract tile
tile = padded_image.crop((
x, y,
min(x + tile_size, padded_image.width),
min(y + tile_size, padded_image.height)
))
# Preprocess tile
tile_array = self._preprocess_tile(tile, tile_size)
# Run inference
input_name = self.session.get_inputs()[0].name
output_name = self.session.get_outputs()[0].name
outputs = self.session.run([output_name], {input_name: tile_array})
tile_mask_logits = outputs[0].squeeze()
# Apply sigmoid and threshold
tile_mask = 1 / (1 + np.exp(-tile_mask_logits)) # Sigmoid
tile_mask = (tile_mask > threshold).astype(np.float32)
tile_mask = 1 - tile_mask # Invert mask
# Resize mask back to original tile size if padding was added
tile_mask_resized = tile_mask[:tile.height, :tile.width]
# Update full mask and count map
full_mask[y:y + tile.height, x:x + tile.width] += tile_mask_resized
count_map[y:y + tile.height, x:x + tile.width] += 1
# Average the overlaps
full_mask = np.divide(full_mask, count_map, out=np.zeros_like(full_mask), where=count_map!=0)
# Crop out padding to get final mask
final_mask = full_mask[:original_height, :original_width]
logger.info(f"Processed {tile_count} tiles")
return final_mask, tile_count
# Initialize the processor
MODEL_PATH = "models/unet_model.onnx"
processor = None
def load_processor():
"""Load the processor with error handling."""
global processor
try:
processor = PavementCellProcessor(MODEL_PATH, device='cpu')
return True
except Exception as e:
logger.error(f"Failed to load processor: {e}")
return False
def process_image(image, tile_size, overlap, threshold, show_overlay, overlay_alpha):
"""Process uploaded image and return results.
Returns separate previews for mask and overlay to support a cleaner UI layout.
"""
global processor
if processor is None:
if not load_processor():
return None, None
if image is None:
return None, None
try:
# start_time could be used for future metrics; keeping code minimal for UI
# Convert to PIL if needed and handle different formats including TIFF
if not isinstance(image, Image.Image):
image = Image.fromarray(image)
# Ensure image is in RGB mode for processing
# - Collapse multi-frame TIFFs to first frame
if hasattr(image, "n_frames") and getattr(image, "n_frames", 1) > 1:
try:
image.seek(0)
except Exception:
pass
if image.mode == 'RGBA':
# Convert RGBA to RGB by compositing over white background
background = Image.new('RGB', image.size, (255, 255, 255))
background.paste(image, mask=image.split()[-1] if len(image.split()) == 4 else None)
image = background
elif image.mode in ['L', 'P', 'CMYK']:
image = image.convert('RGB')
elif image.mode in ['I;16', 'I;16B', 'I;16L', 'I']:
# Downscale 16-bit grayscale to 8-bit then to RGB for display & processing
array_16 = np.array(image)
array_8 = (array_16 / 256).astype(np.uint8)
image = Image.fromarray(array_8, mode='L').convert('RGB')
elif image.mode != 'RGB':
image = image.convert('RGB')
# Process image
mask, tile_count = processor.predict_mask_full_image(
image,
tile_size=int(tile_size),
overlap=int(overlap),
threshold=threshold
)
# Prepare separate previews: mask and overlay
mask_image_pil = Image.fromarray((mask * 255).astype(np.uint8)).convert('L')
# Build an overlay preview using alpha blending for performance
base_np = np.array(image).astype(np.float32) / 255.0
overlay_np = base_np.copy()
boolean_mask = mask > 0.5
alpha = float(overlay_alpha)
# Red overlay on masked regions
red_color = np.array([1.0, 0.0, 0.0], dtype=np.float32)
overlay_np[boolean_mask] = overlay_np[boolean_mask] * (1.0 - alpha) + red_color * alpha
overlay_image_pil = Image.fromarray((overlay_np * 255).astype(np.uint8))
# Save mask to temporary file for download
temp_dir = tempfile.mkdtemp()
mask_path = os.path.join(temp_dir, "segmentation_mask.png")
mask_image_pil.save(mask_path)
# (Optional) metrics calculation removed from return to keep UI minimal
# Return previews and a ready-to-download mask path
return (
mask_image_pil,
overlay_image_pil,
gr.update(value=mask_path, visible=True)
)
except Exception as e:
logger.error(f"Error processing image: {e}")
return None, None, ""
def create_demo():
"""Create the Gradio demo interface."""
with gr.Blocks(
title="Pavement Cell Segmentation",
theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"),
# css="""
# .gradio-container { max-width: none !important; width: 100% !important; margin: 0; padding: 0 12px; }
# .section-title h3 { margin-top: 8px; text-align: center; }
# .controls-row { margin-top: 8px; justify-content: center; }
# .controls-row .gr-button { margin-right: 8px; }
# .tab-content img, .tab-content canvas { border-radius: 10px; }
# .spaced-row { gap: 16px; }
# .centered { justify-content: center; align-items: center; }
# .download-row { display: flex; justify-content: center; }
# .gr-button.primary { background: linear-gradient(90deg, #6366f1 0%, #7c3aed 100%); border: none; }
# .gr-button.secondary { background: #f3f4f6; }
# """
) as demo:
gr.Markdown("""
# 🛣️ Pavement Cell Segmentation
Upload an image of pavement to detect and segment pavement cells using a deep learning model.
**How to use:**
1. Upload a pavement image
2. Adjust the processing parameters if needed
3. Click "Process Image"
4. View the results and download the segmentation mask
---
""")
# Image Upload & Results
with gr.Group():
gr.Markdown("### 📤 Upload & Results", elem_classes=["section-title"])
with gr.Row(elem_classes=["centered"]):
with gr.Column(scale=1):
with gr.Tabs():
with gr.TabItem("Image"):
image_input = gr.Image(
label="Upload Pavement Image",
type="pil",
sources=["upload"],
height=512,
format="png",
image_mode="RGB"
)
# Fixed tile size for robustness and performance
tile_size_state = gr.State(256)
with gr.Column(scale=1):
with gr.Tabs():
with gr.TabItem("Mask"):
mask_image = gr.Image(label="Segmentation Mask", type="pil", height=512, image_mode="L")
with gr.TabItem("Overlay"):
overlay_image = gr.Image(label="Overlay", type="pil", height=512, image_mode="RGB")
# Unified controls row for consistent horizontal alignment
with gr.Row(elem_classes=[""]):
with gr.Column(scale=1):
with gr.Row():
clear_btn = gr.Button("Clear", variant="secondary")
process_btn = gr.Button("🚀 Process Image", variant="primary", interactive=False)
reset_btn = gr.Button("Reset Parameters", variant="secondary")
with gr.Column(scale=1):
download_btn = gr.DownloadButton("📥 Download Segmentation Mask", variant="primary", visible=False)
# Parameters Section
with gr.Group():
gr.Markdown("### ⚙️ Processing Parameters \n\n", elem_classes=["section-title"])
with gr.Row():
preset = gr.Dropdown(
label="Preset",
choices=[
"Fast",
"Balanced (default)",
"Accurate"
],
value="Balanced (default)",
info="Choose speed vs. quality profile"
)
overlap = gr.Slider(
label="Tile Overlap",
minimum=32,
maximum=128,
step=16,
value=64,
info="Overlap between tiles"
)
threshold = gr.Slider(
label="Segmentation Threshold",
minimum=0.1,
maximum=1.0,
step=0.05,
value=0.9,
info="Threshold for binary segmentation"
)
with gr.Accordion("Advanced settings", open=False):
with gr.Row():
show_overlay = gr.Checkbox(
label="Show Overlay",
value=True,
info="Show overlay of mask on original image"
)
overlay_alpha = gr.Slider(
label="Overlay Opacity",
minimum=0.0,
maximum=1.0,
step=0.05,
value=0.5,
info="Transparency of the mask overlay",
visible=True
)
# Event handlers
process_btn.click(
fn=process_image,
inputs=[image_input, tile_size_state, overlap, threshold, show_overlay, overlay_alpha],
outputs=[mask_image, overlay_image, download_btn],
show_progress=True
)
# Enable/disable Process button depending on whether an image is uploaded
def toggle_process_button(img):
return gr.update(interactive=img is not None)
image_input.change(
fn=toggle_process_button,
inputs=[image_input],
outputs=[process_btn]
)
# Sanitize uploaded images (e.g., 16-bit or multi-frame TIFF) for correct preview
def sanitize_input_image(img):
if img is None:
return None
if hasattr(img, "n_frames") and getattr(img, "n_frames", 1) > 1:
try:
img.seek(0)
except Exception:
pass
if img.mode == 'RGBA':
background = Image.new('RGB', img.size, (255, 255, 255))
background.paste(img, mask=img.split()[-1] if len(img.split()) == 4 else None)
img = background
elif img.mode in ['I;16', 'I;16B', 'I;16L', 'I']:
array_16 = np.array(img)
array_8 = (array_16 / 256).astype(np.uint8)
img = Image.fromarray(array_8, mode='L').convert('RGB')
elif img.mode in ['L', 'P', 'CMYK']:
img = img.convert('RGB')
elif img.mode != 'RGB':
img = img.convert('RGB')
return img
image_input.change(
fn=sanitize_input_image,
inputs=[image_input],
outputs=[image_input]
)
# Preset behavior and small helpers
def apply_preset(p):
if p == "Fast":
return (
gr.update(value=32),
gr.update(value=0.8)
)
if p == "Accurate":
return (
gr.update(value=96),
gr.update(value=0.9)
)
return (
gr.update(value=64),
gr.update(value=0.9)
)
def toggle_overlay_opacity(show):
return gr.update(visible=bool(show))
preset.change(
fn=apply_preset,
inputs=[preset],
outputs=[overlap, threshold]
)
show_overlay.change(
fn=toggle_overlay_opacity,
inputs=[show_overlay],
outputs=[overlay_alpha]
)
def clear_all():
return (
None, # mask_image
None, # image_input
None, # overlay_image
gr.update(visible=False, value=None), # download button
gr.update(interactive=False) # process button
)
clear_btn.click(
fn=clear_all,
inputs=None,
outputs=[mask_image, image_input, overlay_image, download_btn, process_btn]
)
# Reset parameters to defaults
def reset_params():
return (
gr.update(value=0.9), # threshold
gr.update(value=True), # show_overlay
gr.update(value=0.5, visible=True), # overlay_alpha
gr.update(value="Balanced (default)"), # preset
gr.update(value=64) # overlap
)
reset_btn.click(
fn=reset_params,
inputs=None,
outputs=[threshold, show_overlay, overlay_alpha, preset, overlap]
)
# Example images section
gr.Markdown("### 📋 Example Usage")
gr.Markdown("""
**Parameter Guidelines:**
- **Tile Size**: Fixed at 256 for stability and good performance
- **Overlap**: More overlap (64-128) for better accuracy, less overlap (32-64) for faster processing
- **Threshold**: Higher values (0.8-0.9) for cleaner results, lower values (0.5-0.7) for more sensitive detection
**Supported Formats:** JPG, PNG, TIFF, BMP
""")
return demo
if __name__ == "__main__":
# Load the processor at startup
logger.info("Loading pavement cell segmentation model...")
if load_processor():
logger.info("Model loaded successfully!")
else:
logger.error("Failed to load model at startup")
# Create and launch demo
demo = create_demo()
# Gradio 5 queue signature uses no keyword args here
demo.queue()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)