Rodiyah commited on
Commit
6523e27
·
verified ·
1 Parent(s): 71694f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -4
app.py CHANGED
@@ -19,6 +19,11 @@ if not os.path.exists(save_dir):
19
  print("📁 Folder created:", save_dir)
20
  os.makedirs(save_dir, exist_ok=True)
21
 
 
 
 
 
 
22
  # Load model
23
  model = models.resnet50(weights=None)
24
  model.fc = torch.nn.Linear(model.fc.in_features, 2)
@@ -38,13 +43,26 @@ transform = transforms.Compose([
38
  [0.229, 0.224, 0.225])
39
  ])
40
 
41
- # Predict and save
 
 
 
 
 
 
 
 
 
 
 
 
42
  def predict_retinopathy(image):
43
- # Validate image
44
- if not looks_like_fundus(image):
45
  return (
46
  invalid_img,
47
- " Invalid image detected.\n\nPlease upload a *retinal fundus scan* for accurate Diabetic Retinopathy assessment."
 
48
  )
49
 
50
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
 
19
  print("📁 Folder created:", save_dir)
20
  os.makedirs(save_dir, exist_ok=True)
21
 
22
+
23
+ # Placeholder image for invalid uploads
24
+ invalid_img = Image.new("RGB", (224, 224), color=(200, 200, 200))
25
+
26
+
27
  # Load model
28
  model = models.resnet50(weights=None)
29
  model.fc = torch.nn.Linear(model.fc.in_features, 2)
 
43
  [0.229, 0.224, 0.225])
44
  ])
45
 
46
+ def looks_like_fundus(image):
47
+ """
48
+ Basic heuristic to check if an image is likely a retinal fundus scan.
49
+ Fundus images tend to have a bright circular region (retina)
50
+ with a darker background.
51
+ """
52
+ img = np.array(image.convert("L").resize((224, 224)))
53
+ thresh = img > 40 # bright region
54
+ white_ratio = np.sum(thresh) / img.size
55
+
56
+ # Fundus images usually occupy ~40–75% of the area
57
+ return 0.40 < white_ratio < 0.75
58
+
59
  def predict_retinopathy(image):
60
+ # Validate image first
61
+ if not looks_like_fundus(image):
62
  return (
63
  invalid_img,
64
+ "⚠️ The uploaded image does not appear to be a retinal fundus scan.\n\n"
65
+ "Please upload a valid ophthalmic retinal image for Diabetic Retinopathy assessment."
66
  )
67
 
68
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")