Update app.py
Browse files
app.py
CHANGED
|
@@ -44,44 +44,49 @@ def base64_to_pil(b64_str):
|
|
| 44 |
|
| 45 |
|
| 46 |
# --- Main detection ---
|
| 47 |
-
def
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
# --- Gradio Interface ---
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
# --- Main detection ---
|
| 47 |
+
def detect_gingivitis_safe(image, conf=0.25, iou=0.5):
|
| 48 |
+
try:
|
| 49 |
+
if image is None:
|
| 50 |
+
return [None, None, None, "❌ No image uploaded"]
|
| 51 |
+
|
| 52 |
+
# Lazy-load models
|
| 53 |
+
model_swelling = get_swelling_model()
|
| 54 |
+
model_redness = get_redness_model()
|
| 55 |
+
model_bleeding = get_bleeding_model()
|
| 56 |
+
|
| 57 |
+
# Preprocess image
|
| 58 |
+
image = preprocess(image)
|
| 59 |
+
|
| 60 |
+
# Run models
|
| 61 |
+
sw_res = model_swelling.predict(image, conf=conf, iou=iou)
|
| 62 |
+
rd_res = model_redness.predict(image, conf=conf, iou=iou)
|
| 63 |
+
bl_res = model_bleeding.predict(image, conf=conf, iou=iou)
|
| 64 |
+
|
| 65 |
+
# Convert to images
|
| 66 |
+
img_sw = sw_res[0].plot(labels=False)[:, :, ::-1]
|
| 67 |
+
img_rd = rd_res[0].plot(labels=False)[:, :, ::-1]
|
| 68 |
+
img_bl = bl_res[0].plot(labels=False)[:, :, ::-1]
|
| 69 |
+
|
| 70 |
+
sw_pil = base64_to_pil(np_to_base64(img_sw))
|
| 71 |
+
rd_pil = base64_to_pil(np_to_base64(img_rd))
|
| 72 |
+
bl_pil = base64_to_pil(np_to_base64(img_bl))
|
| 73 |
+
|
| 74 |
+
# Determine diagnosis
|
| 75 |
+
has_sw = len(sw_res[0].boxes) > 0
|
| 76 |
+
has_rd = len(rd_res[0].boxes) > 0
|
| 77 |
+
has_bl = len(bl_res[0].boxes) > 0
|
| 78 |
+
|
| 79 |
+
if has_sw and has_rd and has_bl:
|
| 80 |
+
diagnosis = "🦷 You have gingivitis."
|
| 81 |
+
else:
|
| 82 |
+
diagnosis = "🟢 You don't have gingivitis."
|
| 83 |
+
|
| 84 |
+
return [sw_pil, rd_pil, bl_pil, diagnosis]
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
# Catch all errors and return a friendly message
|
| 88 |
+
return [None, None, None, f"❌ Error during processing: {str(e)}"]
|
| 89 |
+
|
| 90 |
|
| 91 |
|
| 92 |
# --- Gradio Interface ---
|