Update app.py
Browse files
app.py
CHANGED
@@ -12,33 +12,29 @@ def load_model():
|
|
12 |
return pipeline(
|
13 |
"object-detection",
|
14 |
model="anirban22/detr-resnet-50-med_fracture",
|
15 |
-
threshold=0.1
|
16 |
)
|
17 |
|
18 |
model = load_model()
|
19 |
|
20 |
-
def enhance_image(
|
21 |
-
|
22 |
-
gray = cv2.cvtColor(
|
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:
|
32 |
-
image = Image.open(uploaded_file)
|
33 |
if image.size[0] > 800:
|
34 |
ratio = 800.0 / image.size[0]
|
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)
|
@@ -47,28 +43,26 @@ if uploaded_file:
|
|
47 |
st.image(image, caption="Image originale", use_container_width=True)
|
48 |
|
49 |
with col2:
|
50 |
-
|
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 |
-
|
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(
|
62 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
63 |
|
64 |
-
st.image(
|
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
|
72 |
|
73 |
else:
|
74 |
st.info("Veuillez télécharger une image radiographique pour l'analyse.")
|
|
|
12 |
return pipeline(
|
13 |
"object-detection",
|
14 |
model="anirban22/detr-resnet-50-med_fracture",
|
15 |
+
threshold=0.1
|
16 |
)
|
17 |
|
18 |
model = load_model()
|
19 |
|
20 |
+
def enhance_image(img):
|
21 |
+
img_array = np.array(img)
|
22 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
|
|
23 |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
24 |
enhanced = clahe.apply(gray)
|
|
|
25 |
return cv2.cvtColor(enhanced, cv2.COLOR_GRAY2RGB)
|
26 |
|
27 |
uploaded_file = st.file_uploader("Téléchargez une image radiographique", type=["jpg", "jpeg", "png"])
|
28 |
|
29 |
if uploaded_file:
|
30 |
+
image = Image.open(uploaded_file).convert('RGB')
|
31 |
if image.size[0] > 800:
|
32 |
ratio = 800.0 / image.size[0]
|
33 |
size = (800, int(image.size[1] * ratio))
|
34 |
image = image.resize(size, Image.Resampling.LANCZOS)
|
35 |
|
|
|
36 |
enhanced_image = enhance_image(image)
|
37 |
|
|
|
38 |
predictions = model(enhanced_image)
|
39 |
|
40 |
col1, col2 = st.columns(2)
|
|
|
43 |
st.image(image, caption="Image originale", use_container_width=True)
|
44 |
|
45 |
with col2:
|
46 |
+
output_img = np.array(image).copy()
|
47 |
for pred in predictions:
|
48 |
box = pred['box']
|
49 |
score = pred['score']
|
50 |
|
51 |
x1, y1, x2, y2 = [int(i) for i in [box['xmin'], box['ymin'], box['xmax'], box['ymax']]]
|
52 |
+
cv2.rectangle(output_img, (x1, y1), (x2, y2), (255, 0, 0), 3)
|
|
|
53 |
|
|
|
54 |
text = f"Fracture: {score:.2f}"
|
55 |
+
cv2.putText(output_img, text, (x1, y1-10),
|
56 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
57 |
|
58 |
+
st.image(output_img, caption="Détection des fractures", use_container_width=True)
|
59 |
|
60 |
st.subheader("Résultats")
|
61 |
if predictions:
|
62 |
for idx, pred in enumerate(predictions, 1):
|
63 |
st.warning(f"⚠️ Fracture {idx} détectée (Confiance: {pred['score']*100:.1f}%)")
|
64 |
else:
|
65 |
+
st.warning("⚠️ Aucune fracture n'a été détectée avec certitude")
|
66 |
|
67 |
else:
|
68 |
st.info("Veuillez télécharger une image radiographique pour l'analyse.")
|