Update app.py
Browse files
app.py
CHANGED
|
@@ -15,42 +15,30 @@ output_details = interpreter.get_output_details()
|
|
| 15 |
class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
|
| 16 |
CONFIDENCE_THRESHOLD = 0.7
|
| 17 |
|
| 18 |
-
def preprocess_image(image):
|
| 19 |
-
image = image.resize((224, 224)).convert("RGB")
|
| 20 |
-
img_array = np.array(image, dtype=np.float32) / 255.0
|
| 21 |
-
return np.expand_dims(img_array, axis=0)
|
| 22 |
-
|
| 23 |
def predict_image(image):
|
| 24 |
try:
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Inference
|
| 28 |
interpreter.set_tensor(input_details[0]['index'], img_array)
|
| 29 |
interpreter.invoke()
|
| 30 |
output = interpreter.get_tensor(output_details[0]['index'])
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
class_idx = int(np.argmax(probabilities))
|
| 35 |
-
confidence = float(np.max(probabilities))
|
| 36 |
|
| 37 |
-
if confidence
|
| 38 |
-
return f"
|
| 39 |
else:
|
| 40 |
-
|
| 41 |
-
top2_idx = np.argsort(probabilities)[-2:][::-1]
|
| 42 |
-
suggestion = ", ".join(
|
| 43 |
-
[f"{class_names[i]} ({probabilities[i]:.2f})" for i in top2_idx]
|
| 44 |
-
)
|
| 45 |
-
return f"⚠️ Low confidence ({confidence:.2f}).\nPossible classes: {suggestion}"
|
| 46 |
-
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"
|
| 49 |
|
| 50 |
gr.Interface(
|
| 51 |
fn=predict_image,
|
| 52 |
-
inputs=gr.Image(type="pil"
|
| 53 |
outputs="text",
|
| 54 |
-
title="
|
| 55 |
-
description="Upload
|
| 56 |
).launch()
|
|
|
|
| 15 |
class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
|
| 16 |
CONFIDENCE_THRESHOLD = 0.7
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def predict_image(image):
|
| 19 |
try:
|
| 20 |
+
image = image.resize((128, 128)).convert("RGB")
|
| 21 |
+
img_array = np.array(image, dtype=np.float32) / 255.0
|
| 22 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 23 |
|
|
|
|
| 24 |
interpreter.set_tensor(input_details[0]['index'], img_array)
|
| 25 |
interpreter.invoke()
|
| 26 |
output = interpreter.get_tensor(output_details[0]['index'])
|
| 27 |
|
| 28 |
+
class_idx = int(np.argmax(output))
|
| 29 |
+
confidence = float(np.max(output))
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
if confidence < CONFIDENCE_THRESHOLD:
|
| 32 |
+
return f"⚠️ Low confidence ({confidence:.2f}). The model is unsure. Please try a clearer image."
|
| 33 |
else:
|
| 34 |
+
return f"✅ Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
return f"Error: {str(e)}"
|
| 37 |
|
| 38 |
gr.Interface(
|
| 39 |
fn=predict_image,
|
| 40 |
+
inputs=gr.Image(type="pil"),
|
| 41 |
outputs="text",
|
| 42 |
+
title="Muscle Disease Detection",
|
| 43 |
+
description="Upload an MRI image to detect muscle conditions."
|
| 44 |
).launch()
|