File size: 3,101 Bytes
7461183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# utils.py
# ECG Condition Classifier - Image Preprocessing

from PIL import Image, ImageFilter, ImageEnhance, ImageOps
import numpy as np

TARGET_SIZE = (224, 224)
MIN_SIZE = 50


def load_and_preprocess(image):
    """
    Preprocesses an ECG image for ViT inference.

    Key steps:
    1. Convert to RGB
    2. Auto-crop whitespace/borders
    3. Convert to grayscale then back to RGB (removes color noise from
       pink/beige ECG paper, watermarks, etc.)
    4. Enhance contrast to make waveform lines pop
    5. Resize to 224x224 for ViT
    """
    if image is None:
        raise ValueError("No image provided.")

    # Step 1: Ensure RGB
    img = image.convert("RGB")

    # Step 2: Auto-crop borders/whitespace
    img = _autocrop(img)

    # Step 3: Grayscale normalization
    # Converts pink/beige/colored ECG paper to clean black-on-white.
    # This removes color distribution shift between training data and
    # stock photos, dramatically improving model confidence.
    gray = img.convert("L")

    # Step 4: Enhance contrast — make ECG traces sharper
    enhancer = ImageEnhance.Contrast(gray)
    gray = enhancer.enhance(2.5)

    # Step 5: Sharpen edges (waveform lines)
    gray = gray.filter(ImageFilter.SHARPEN)

    # Step 6: Back to RGB (model expects 3-channel input)
    img = gray.convert("RGB")

    # Step 7: Resize to ViT input size
    img = img.resize(TARGET_SIZE, Image.LANCZOS)

    return img


def _autocrop(img, threshold=240, padding=10):
    """
    Crops away white/light borders around the ECG tracing area.
    Helps the model focus on the waveform rather than margins.
    """
    try:
        gray = img.convert("L")
        arr = np.array(gray)

        # Find rows/cols that contain dark pixels (the ECG trace)
        mask = arr < threshold
        rows = np.any(mask, axis=1)
        cols = np.any(mask, axis=0)

        if not rows.any() or not cols.any():
            return img  # Nothing to crop

        rmin, rmax = np.where(rows)[0][[0, -1]]
        cmin, cmax = np.where(cols)[0][[0, -1]]

        # Add small padding back
        h, w = arr.shape
        rmin = max(0, rmin - padding)
        rmax = min(h, rmax + padding)
        cmin = max(0, cmin - padding)
        cmax = min(w, cmax + padding)

        return img.crop((cmin, rmin, cmax, rmax))
    except Exception:
        return img  # Fall back to original if crop fails


def validate_image(image):
    """
    Validates that the uploaded image is usable.
    Returns (is_valid: bool, message: str)
    """
    if image is None:
        return False, "No image uploaded. Please upload an ECG image."

    try:
        w, h = image.size
        if w < MIN_SIZE or h < MIN_SIZE:
            return False, f"Image too small ({w}x{h}). Please upload a higher resolution ECG."
    except Exception as e:
        return False, f"Invalid image: {str(e)}"

    return True, "Valid"


def get_image_info(image):
    """Returns a summary string of image properties (for debugging)."""
    if image is None:
        return "No image"
    return f"Size: {image.size}, Mode: {image.mode}"