Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +85 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,86 @@
|
|
| 1 |
-
import
|
| 2 |
import numpy as np
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import cv2
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
@st.cache_resource(show_spinner=False)
|
| 8 |
+
def load_model():
|
| 9 |
+
return YOLO('best.pt') # Make sure the model file is in the same folder or provide full path
|
| 10 |
+
|
| 11 |
+
def predict(image, model):
|
| 12 |
+
img_np = np.array(image)
|
| 13 |
+
if img_np.shape[2] == 4:
|
| 14 |
+
img_np = cv2.cvtColor(img_np, cv2.COLOR_RGBA2RGB)
|
| 15 |
+
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 16 |
+
results = model(img_bgr)
|
| 17 |
+
return results[0]
|
| 18 |
+
|
| 19 |
+
def draw_results(image, results):
|
| 20 |
+
img_annotated = results.plot()
|
| 21 |
+
img_annotated_rgb = cv2.cvtColor(img_annotated, cv2.COLOR_BGR2RGB)
|
| 22 |
+
return Image.fromarray(img_annotated_rgb)
|
| 23 |
+
|
| 24 |
+
def crop_box(image, box):
|
| 25 |
+
img_np = np.array(image)
|
| 26 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 27 |
+
h, w = img_np.shape[:2]
|
| 28 |
+
pad_x, pad_y = int((x2 - x1) * 0.1), int((y2 - y1) * 0.1)
|
| 29 |
+
x1, y1 = max(0, x1 - pad_x), max(0, y1 - pad_y)
|
| 30 |
+
x2, y2 = min(w, x2 + pad_x), min(h, y2 + pad_y)
|
| 31 |
+
cropped = img_np[y1:y2, x1:x2]
|
| 32 |
+
return Image.fromarray(cropped)
|
| 33 |
+
|
| 34 |
+
def main():
|
| 35 |
+
st.title("🦷 Interactive Teeth Segmentation & Annotation")
|
| 36 |
+
|
| 37 |
+
st.markdown("""
|
| 38 |
+
Upload a panoramic dental X-ray image.
|
| 39 |
+
After detection, you can edit the tooth number labels for each detected tooth.
|
| 40 |
+
""")
|
| 41 |
+
|
| 42 |
+
model = load_model()
|
| 43 |
+
uploaded_file = st.file_uploader("Upload panoramic dental X-ray", type=["png", "jpg", "jpeg"])
|
| 44 |
+
|
| 45 |
+
if uploaded_file:
|
| 46 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 47 |
+
with st.spinner("Detecting teeth..."):
|
| 48 |
+
results = predict(image, model)
|
| 49 |
+
|
| 50 |
+
annotated_img = draw_results(image, results)
|
| 51 |
+
|
| 52 |
+
col1, col2 = st.columns(2)
|
| 53 |
+
with col1:
|
| 54 |
+
st.subheader("Original Image")
|
| 55 |
+
st.image(image, use_column_width=True)
|
| 56 |
+
with col2:
|
| 57 |
+
st.subheader("Detection & Segmentation")
|
| 58 |
+
st.image(annotated_img, use_column_width=True)
|
| 59 |
+
|
| 60 |
+
if len(results.boxes) > 0:
|
| 61 |
+
st.markdown("### Edit Detected Tooth Numbers")
|
| 62 |
+
|
| 63 |
+
edited_labels = {}
|
| 64 |
+
|
| 65 |
+
for i, box in enumerate(results.boxes):
|
| 66 |
+
class_id = int(box.cls[0])
|
| 67 |
+
default_label = results.names[class_id]
|
| 68 |
+
confidence = box.conf[0].item()
|
| 69 |
+
cropped = crop_box(image, box)
|
| 70 |
+
|
| 71 |
+
st.markdown(f"**Tooth {i+1}** (Confidence: {confidence:.2%})")
|
| 72 |
+
st.image(cropped, width=150)
|
| 73 |
+
|
| 74 |
+
new_label = st.text_input(f"Change tooth number (default: {default_label})",
|
| 75 |
+
value=default_label,
|
| 76 |
+
key=f"label_{i}")
|
| 77 |
+
|
| 78 |
+
edited_labels[i] = new_label
|
| 79 |
+
|
| 80 |
+
st.markdown("---")
|
| 81 |
+
st.subheader("Final Tooth Labels")
|
| 82 |
+
for i, label in edited_labels.items():
|
| 83 |
+
st.write(f"Tooth {i+1}: {label}")
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
st.info("No teeth detected in this image.")
|