Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,4 +64,60 @@ except AttributeError:
|
|
| 64 |
label_names = ["Background", "Caries", "Normal Teeth", "Plaque"]
|
| 65 |
|
| 66 |
def preprocess_image(image):
|
| 67 |
-
"""Load and preprocess a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
label_names = ["Background", "Caries", "Normal Teeth", "Plaque"]
|
| 65 |
|
| 66 |
def preprocess_image(image):
|
| 67 |
+
"""Load and preprocess a PIL image."""
|
| 68 |
+
if not isinstance(image, Image.Image):
|
| 69 |
+
image = Image.fromarray(image)
|
| 70 |
+
image = image.convert("RGB")
|
| 71 |
+
return inference_transform(image).unsqueeze(0)
|
| 72 |
+
|
| 73 |
+
def predict_image(image):
|
| 74 |
+
"""Run inference on image and return multi-label predictions."""
|
| 75 |
+
pixel_values = preprocess_image(image)
|
| 76 |
+
|
| 77 |
+
with torch.no_grad():
|
| 78 |
+
logits = quantized_model(pixel_values)
|
| 79 |
+
|
| 80 |
+
probs = torch.sigmoid(logits).squeeze(0)
|
| 81 |
+
preds = (probs > 0.5).int().tolist()
|
| 82 |
+
|
| 83 |
+
detected_conditions = []
|
| 84 |
+
for i, (label, pred) in enumerate(zip(label_names, preds)):
|
| 85 |
+
if pred == 1:
|
| 86 |
+
confidence = probs[i].item()
|
| 87 |
+
detected_conditions.append(f"{label} (confidence: {confidence:.2%})")
|
| 88 |
+
|
| 89 |
+
# Check for potential Caries
|
| 90 |
+
try:
|
| 91 |
+
caries_index = label_names.index("Caries")
|
| 92 |
+
caries_prob = probs[caries_index].item()
|
| 93 |
+
if 0.3 <= caries_prob < 0.5:
|
| 94 |
+
detected_conditions.append(f"Possible Caries (confidence: {caries_prob:.2%})")
|
| 95 |
+
except ValueError:
|
| 96 |
+
pass
|
| 97 |
+
|
| 98 |
+
if detected_conditions:
|
| 99 |
+
result = "Detected: " + ", ".join(detected_conditions)
|
| 100 |
+
else:
|
| 101 |
+
result = "No dental issues detected"
|
| 102 |
+
|
| 103 |
+
return result
|
| 104 |
+
|
| 105 |
+
# Example images
|
| 106 |
+
examples = [
|
| 107 |
+
["example_image1.jfif"],
|
| 108 |
+
["example_image2.jfif"],
|
| 109 |
+
["example_image3.jfif"]
|
| 110 |
+
]
|
| 111 |
+
|
| 112 |
+
# Gradio interface
|
| 113 |
+
iface = gr.Interface(
|
| 114 |
+
fn=predict_image,
|
| 115 |
+
inputs=gr.Image(type="pil"),
|
| 116 |
+
outputs="text",
|
| 117 |
+
title="Dental Image Multi-Label Classification",
|
| 118 |
+
description="Upload an image or select from the examples below to predict dental conditions. The model can detect multiple dental issues in a single image.",
|
| 119 |
+
examples=examples
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
iface.launch()
|