GOWREESH M G commited on
Commit
4f82e97
·
verified ·
1 Parent(s): 94692c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -1
app.py CHANGED
@@ -77,6 +77,44 @@ def init_model():
77
  MODEL = None
78
 
79
  # -------------------- PREPROCESSING --------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  def process_single_image(image_path):
81
  try:
82
  img = load_img(image_path, target_size=IMG_SIZE)
@@ -146,6 +184,9 @@ def analyze():
146
  idx = np.argmax(preds)
147
  label = CLASSES[idx]
148
  conf = preds[idx] * 100
 
 
 
149
 
150
  # FORMAT RESULT
151
  mapping = {
@@ -160,7 +201,7 @@ def analyze():
160
  "diagnosis": diag, "severity": sev, "color": col, "icon": icon,
161
  "description": f"AI Analysis Result: {diag}",
162
  "confidence": f"{conf:.1f}%",
163
- "features": {"entropy": "Verified"}
164
  })
165
 
166
  except Exception as e:
 
77
  MODEL = None
78
 
79
  # -------------------- PREPROCESSING --------------------
80
+ def calculate_entropy(img_array):
81
+ """
82
+ Calculates Shannon Entropy to measure image texture complexity.
83
+ Returns a float value (higher = more complex/diseased features).
84
+ """
85
+ try:
86
+ # Ensure image is 0-255 uint8 for histogram calculation
87
+ if img_array.dtype != np.uint8:
88
+ # If normalized 0-1, scale up. If just float 0-255, cast.
89
+ if np.max(img_array) <= 1.0:
90
+ calc_img = (img_array * 255).astype(np.uint8)
91
+ else:
92
+ calc_img = img_array.astype(np.uint8)
93
+ else:
94
+ calc_img = img_array
95
+
96
+ # Convert to grayscale if it's color (Batch dim, H, W, C) or (H, W, C)
97
+ if len(calc_img.shape) == 4: # (1, 224, 224, 3)
98
+ calc_img = calc_img[0]
99
+
100
+ gray = cv2.cvtColor(calc_img, cv2.COLOR_RGB2GRAY)
101
+
102
+ # Compute histogram
103
+ hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
104
+
105
+ # Normalize histogram to get probabilities
106
+ hist_norm = hist.ravel() / hist.sum()
107
+
108
+ # Filter zero values to avoid log(0) error
109
+ hist_norm = hist_norm[hist_norm > 0]
110
+
111
+ # Entropy formula: -Sum(p * log2(p))
112
+ entropy_val = -np.sum(hist_norm * np.log2(hist_norm))
113
+ return float(entropy_val)
114
+ except Exception as e:
115
+ print(f"Entropy Error: {e}")
116
+ return 4.5 # Fallback average value
117
+
118
  def process_single_image(image_path):
119
  try:
120
  img = load_img(image_path, target_size=IMG_SIZE)
 
184
  idx = np.argmax(preds)
185
  label = CLASSES[idx]
186
  conf = preds[idx] * 100
187
+
188
+ # 4. Calculate Real Entropy
189
+ entropy_val = calculate_entropy(input_data)
190
 
191
  # FORMAT RESULT
192
  mapping = {
 
201
  "diagnosis": diag, "severity": sev, "color": col, "icon": icon,
202
  "description": f"AI Analysis Result: {diag}",
203
  "confidence": f"{conf:.1f}%",
204
+ "features": {"entropy": f"{entropy_val:.3f}"}
205
  })
206
 
207
  except Exception as e: