Noursine commited on
Commit
647cdfa
·
verified ·
1 Parent(s): 2c6c632

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +85 -39
src/streamlit_app.py CHANGED
@@ -1,40 +1,86 @@
1
- import altair as alt
2
  import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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.")