Upload utils.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# utils.py# ECG Condition Classifier - Image Preprocessing Utilities
from PIL import Imageimport numpy as np
# Target input size expected by ViT modelsTARGET_SIZE = (224, 224)
def load_and_preprocess(image): """ Accepts a PIL Image or file path. Returns a clean, resized PIL Image ready for the classifier pipeline. """ # If a file path was passed instead of a PIL image, open it if isinstance(image, str): image = Image.open(image)
# Convert to RGB - handles grayscale ECGs and RGBA screenshots if image.mode != "RGB": image = image.convert("RGB")
# Resize to ViT expected input dimensions image = image.resize(TARGET_SIZE, Image.LANCZOS)
return image
def validate_image(image): """ Basic sanity checks on uploaded image. Returns (is_valid: bool, message: str) """ if image is None: return False, "No image provided. Please upload an ECG image."
# Check minimum resolution - too small likely isn't a real ECG if hasattr(image, 'size'): w, h = image.size if w < 50 or h < 50: return False, "Image resolution too low. Please upload a clearer ECG image."
return True, "OK"
def get_image_info(image): """ Returns a simple string summary of the uploaded image. Useful for debugging in the Gradio interface. """ if image is None: return "No image loaded." w, h = image.size return f"Image loaded: {w}x{h}px | Mode: {image.mode}"utils.pyAdded+54