Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,23 @@ st.title("Détection de fractures osseuses par rayons X")
|
|
9 |
|
10 |
@st.cache_resource
|
11 |
def load_model():
|
12 |
-
return pipeline(
|
|
|
|
|
|
|
|
|
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:
|
@@ -22,42 +35,40 @@ if uploaded_file:
|
|
22 |
size = (800, int(image.size[1] * ratio))
|
23 |
image = image.resize(size, Image.Resampling.LANCZOS)
|
24 |
|
25 |
-
|
|
|
26 |
|
27 |
-
#
|
28 |
-
predictions = model(
|
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 |
-
|
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 |
-
|
|
|
47 |
|
48 |
-
#
|
49 |
-
text = f"
|
50 |
-
cv2.putText(img_with_boxes, text, (x1, y1-10),
|
|
|
51 |
|
52 |
-
st.image(img_with_boxes, caption="
|
53 |
|
54 |
-
# Display results
|
55 |
st.subheader("Résultats")
|
56 |
if predictions:
|
57 |
-
for pred in predictions:
|
58 |
-
st.warning(f"⚠️ {
|
59 |
else:
|
60 |
-
st.
|
61 |
|
62 |
else:
|
63 |
st.info("Veuillez télécharger une image radiographique pour l'analyse.")
|
|
|
9 |
|
10 |
@st.cache_resource
|
11 |
def load_model():
|
12 |
+
return pipeline(
|
13 |
+
"object-detection",
|
14 |
+
model="anirban22/detr-resnet-50-med_fracture",
|
15 |
+
threshold=0.1 # Réduire le seuil de confiance
|
16 |
+
)
|
17 |
|
18 |
model = load_model()
|
19 |
|
20 |
+
def enhance_image(image):
|
21 |
+
# Convertir en niveaux de gris
|
22 |
+
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
23 |
+
# Améliorer le contraste
|
24 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
25 |
+
enhanced = clahe.apply(gray)
|
26 |
+
# Reconvertir en RGB
|
27 |
+
return cv2.cvtColor(enhanced, cv2.COLOR_GRAY2RGB)
|
28 |
+
|
29 |
uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"])
|
30 |
|
31 |
if uploaded_file:
|
|
|
35 |
size = (800, int(image.size[1] * ratio))
|
36 |
image = image.resize(size, Image.Resampling.LANCZOS)
|
37 |
|
38 |
+
# Améliorer l'image
|
39 |
+
enhanced_image = enhance_image(image)
|
40 |
|
41 |
+
# Obtenir les prédictions
|
42 |
+
predictions = model(enhanced_image)
|
43 |
|
|
|
44 |
col1, col2 = st.columns(2)
|
45 |
|
46 |
with col1:
|
47 |
st.image(image, caption="Image originale", use_container_width=True)
|
48 |
|
49 |
with col2:
|
50 |
+
img_with_boxes = enhanced_image.copy()
|
|
|
51 |
for pred in predictions:
|
52 |
box = pred['box']
|
53 |
score = pred['score']
|
|
|
54 |
|
|
|
55 |
x1, y1, x2, y2 = [int(i) for i in [box['xmin'], box['ymin'], box['xmax'], box['ymax']]]
|
56 |
+
# Rectangle rouge plus épais
|
57 |
+
cv2.rectangle(img_with_boxes, (x1, y1), (x2, y2), (255, 0, 0), 3)
|
58 |
|
59 |
+
# Texte plus visible
|
60 |
+
text = f"Fracture: {score:.2f}"
|
61 |
+
cv2.putText(img_with_boxes, text, (x1, y1-10),
|
62 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
63 |
|
64 |
+
st.image(img_with_boxes, caption="Détection des fractures", use_container_width=True)
|
65 |
|
|
|
66 |
st.subheader("Résultats")
|
67 |
if predictions:
|
68 |
+
for idx, pred in enumerate(predictions, 1):
|
69 |
+
st.warning(f"⚠️ Fracture {idx} détectée (Confiance: {pred['score']*100:.1f}%)")
|
70 |
else:
|
71 |
+
st.warning("⚠️ Aucune fracture n'a été détectée avec certitude. Veuillez consulter un professionnel pour confirmation.")
|
72 |
|
73 |
else:
|
74 |
st.info("Veuillez télécharger une image radiographique pour l'analyse.")
|