Update app.py
Browse files
app.py
CHANGED
@@ -5,58 +5,59 @@ import numpy as np
|
|
5 |
import cv2
|
6 |
|
7 |
st.set_page_config(page_title="Détection de fractures osseuses")
|
8 |
-
|
9 |
st.title("Détection de fractures osseuses par rayons X")
|
10 |
|
11 |
@st.cache_resource
|
12 |
def load_model():
|
13 |
-
return pipeline("
|
14 |
|
15 |
model = load_model()
|
16 |
|
17 |
uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"])
|
18 |
|
19 |
if uploaded_file:
|
20 |
-
# Load and resize image
|
21 |
image = Image.open(uploaded_file)
|
22 |
-
# Resize to max 800px width while maintaining aspect ratio
|
23 |
if image.size[0] > 800:
|
24 |
ratio = 800.0 / image.size[0]
|
25 |
size = (800, int(image.size[1] * ratio))
|
26 |
image = image.resize(size, Image.Resampling.LANCZOS)
|
27 |
|
28 |
-
# Convert to array for overlay
|
29 |
image_array = np.array(image)
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
|
34 |
-
# Create columns for
|
35 |
col1, col2 = st.columns(2)
|
36 |
|
37 |
with col1:
|
38 |
st.image(image, caption="Image originale", use_container_width=True)
|
39 |
|
40 |
with col2:
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
|
54 |
-
# Display
|
55 |
-
st.subheader("
|
56 |
-
if
|
57 |
-
|
|
|
58 |
else:
|
59 |
-
st.success(
|
60 |
|
61 |
else:
|
62 |
st.info("Veuillez télécharger une image radiographique pour l'analyse.")
|
|
|
5 |
import cv2
|
6 |
|
7 |
st.set_page_config(page_title="Détection de fractures osseuses")
|
|
|
8 |
st.title("Détection de fractures osseuses par rayons X")
|
9 |
|
10 |
@st.cache_resource
|
11 |
def load_model():
|
12 |
+
return pipeline("object-detection", model="anirban22/detr-resnet-50-med_fracture")
|
13 |
|
14 |
model = load_model()
|
15 |
|
16 |
uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"])
|
17 |
|
18 |
if uploaded_file:
|
|
|
19 |
image = Image.open(uploaded_file)
|
|
|
20 |
if image.size[0] > 800:
|
21 |
ratio = 800.0 / image.size[0]
|
22 |
size = (800, int(image.size[1] * ratio))
|
23 |
image = image.resize(size, Image.Resampling.LANCZOS)
|
24 |
|
|
|
25 |
image_array = np.array(image)
|
26 |
|
27 |
+
# Get predictions
|
28 |
+
predictions = model(image)
|
29 |
|
30 |
+
# Create columns for display
|
31 |
col1, col2 = st.columns(2)
|
32 |
|
33 |
with col1:
|
34 |
st.image(image, caption="Image originale", use_container_width=True)
|
35 |
|
36 |
with col2:
|
37 |
+
# Draw bounding boxes
|
38 |
+
img_with_boxes = image_array.copy()
|
39 |
+
for pred in predictions:
|
40 |
+
box = pred['box']
|
41 |
+
score = pred['score']
|
42 |
+
label = pred['label']
|
43 |
+
|
44 |
+
# Draw rectangle
|
45 |
+
x1, y1, x2, y2 = [int(i) for i in [box['xmin'], box['ymin'], box['xmax'], box['ymax']]]
|
46 |
+
cv2.rectangle(img_with_boxes, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
47 |
|
48 |
+
# Add label and score
|
49 |
+
text = f"{label}: {score:.2f}"
|
50 |
+
cv2.putText(img_with_boxes, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
51 |
+
|
52 |
+
st.image(img_with_boxes, caption="Fractures détectées", use_container_width=True)
|
53 |
|
54 |
+
# Display results
|
55 |
+
st.subheader("Résultats")
|
56 |
+
if predictions:
|
57 |
+
for pred in predictions:
|
58 |
+
st.warning(f"⚠️ {pred['label']} détectée (Confiance: {pred['score']*100:.1f}%)")
|
59 |
else:
|
60 |
+
st.success("✅ Aucune fracture détectée")
|
61 |
|
62 |
else:
|
63 |
st.info("Veuillez télécharger une image radiographique pour l'analyse.")
|