Rodiyah commited on
Commit
cc10294
·
verified ·
1 Parent(s): 94fd547

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
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
- Fundus images typically have a brighter central region (retina)
49
- and a darker outer border (background).
 
 
 
 
50
  """
 
51
  img = np.array(image.convert("L").resize((224, 224)))
52
 
53
- # Central crop
54
  center = img[40:184, 40:184]
55
- center_mean = center.mean()
56
 
57
- # Border = everything outside the central crop
58
- border = img.copy()
59
- border[40:184, 40:184] = 0
60
- border_pixels = border[border > 0]
61
 
62
- # If no real border pixels, don't block it
63
  if border_pixels.size == 0:
64
  return True
65
 
 
66
  border_mean = border_pixels.mean()
67
 
68
- # Fundus: center clearly brighter than border
69
- return center_mean > border_mean + 8
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  # -----------------------