Update app.py
Browse files
app.py
CHANGED
|
@@ -45,28 +45,46 @@ transform = transforms.Compose([
|
|
| 45 |
def looks_like_fundus(image):
|
| 46 |
"""
|
| 47 |
Heuristic to check if an image is likely a retinal fundus scan.
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
"""
|
|
|
|
| 51 |
img = np.array(image.convert("L").resize((224, 224)))
|
| 52 |
|
| 53 |
-
# Central
|
| 54 |
center = img[40:184, 40:184]
|
| 55 |
-
center_mean = center.mean()
|
| 56 |
|
| 57 |
-
# Border = everything outside the
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
border_pixels =
|
| 61 |
|
| 62 |
-
#
|
| 63 |
if border_pixels.size == 0:
|
| 64 |
return True
|
| 65 |
|
|
|
|
| 66 |
border_mean = border_pixels.mean()
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
# -----------------------
|
|
|
|
| 45 |
def looks_like_fundus(image):
|
| 46 |
"""
|
| 47 |
Heuristic to check if an image is likely a retinal fundus scan.
|
| 48 |
+
|
| 49 |
+
Assumptions:
|
| 50 |
+
- Fundus images usually have a bright circular central region (retina)
|
| 51 |
+
and a darker outer border (background).
|
| 52 |
+
- Non-fundus images (documents, labels, screens) tend to have
|
| 53 |
+
similar brightness across the whole frame.
|
| 54 |
"""
|
| 55 |
+
# Grayscale + resize for consistency
|
| 56 |
img = np.array(image.convert("L").resize((224, 224)))
|
| 57 |
|
| 58 |
+
# Central square (potential retina)
|
| 59 |
center = img[40:184, 40:184]
|
|
|
|
| 60 |
|
| 61 |
+
# Border = everything outside the center
|
| 62 |
+
border_mask = np.ones_like(img, dtype=bool)
|
| 63 |
+
border_mask[40:184, 40:184] = False
|
| 64 |
+
border_pixels = img[border_mask]
|
| 65 |
|
| 66 |
+
# Safety: if something weird happens, don't block it
|
| 67 |
if border_pixels.size == 0:
|
| 68 |
return True
|
| 69 |
|
| 70 |
+
center_mean = center.mean()
|
| 71 |
border_mean = border_pixels.mean()
|
| 72 |
|
| 73 |
+
# Ratios of dark/bright pixels
|
| 74 |
+
border_dark_ratio = np.mean(border_pixels < 40) # dark background
|
| 75 |
+
center_bright_ratio = np.mean(center > 80) # bright retina
|
| 76 |
+
|
| 77 |
+
# Conditions (tunable):
|
| 78 |
+
# 1) Center clearly brighter than border
|
| 79 |
+
cond_contrast = center_mean - border_mean > 25
|
| 80 |
+
|
| 81 |
+
# 2) Most of the border is dark
|
| 82 |
+
cond_border_dark = border_dark_ratio > 0.5
|
| 83 |
+
|
| 84 |
+
# 3) A good portion of the center is bright
|
| 85 |
+
cond_center_bright = center_bright_ratio > 0.4
|
| 86 |
+
|
| 87 |
+
return cond_contrast and cond_border_dark and cond_center_bright
|
| 88 |
|
| 89 |
|
| 90 |
# -----------------------
|