Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,69 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
import cv2
|
6 |
|
7 |
-
st.set_page_config(page_title="Détection de
|
8 |
-
st.title("Détection de
|
9 |
|
10 |
@st.cache_resource
|
11 |
def load_model():
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
threshold=0.1
|
16 |
-
)
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
predictions = model(image)
|
32 |
-
|
33 |
-
col1, col2 = st.columns(2)
|
34 |
-
|
35 |
-
with col1:
|
36 |
-
st.image(image, caption="Image originale", use_container_width=True)
|
37 |
-
|
38 |
-
with col2:
|
39 |
-
img_array = np.array(image)
|
40 |
-
for pred in predictions:
|
41 |
-
box = pred['box']
|
42 |
-
score = pred['score']
|
43 |
-
x1, y1, x2, y2 = [int(i) for i in [box['xmin'], box['ymin'], box['xmax'], box['ymax']]]
|
44 |
-
cv2.rectangle(img_array, (x1, y1), (x2, y2), (255, 0, 0), 3)
|
45 |
-
text = f"Fracture: {score:.2f}"
|
46 |
-
cv2.putText(img_array, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModelForObjectDetection
|
3 |
+
import torch
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import cv2
|
7 |
|
8 |
+
st.set_page_config(page_title="Détection de nodules pulmonaires")
|
9 |
+
st.title("Détection de nodules pulmonaires sur images scanner")
|
10 |
|
11 |
@st.cache_resource
|
12 |
def load_model():
|
13 |
+
model = AutoModelForObjectDetection.from_pretrained("monai-test/lung_nodule_ct_detection")
|
14 |
+
model.eval()
|
15 |
+
return model
|
|
|
|
|
16 |
|
17 |
+
def process_image(image):
|
18 |
+
# Convertir en niveau de gris
|
19 |
+
img_array = np.array(image.convert('L'))
|
20 |
+
# Normaliser
|
21 |
+
normalized = (img_array - img_array.min()) / (img_array.max() - img_array.min())
|
22 |
+
# Redimensionner
|
23 |
+
resized = cv2.resize(normalized, (512, 512))
|
24 |
+
# Préparer pour PyTorch
|
25 |
+
tensor = torch.FloatTensor(resized).unsqueeze(0).unsqueeze(0)
|
26 |
+
return tensor
|
27 |
|
28 |
+
try:
|
29 |
+
model = load_model()
|
30 |
|
31 |
+
uploaded_file = st.file_uploader("Téléchargez une image scanner", type=["jpg", "jpeg", "png"])
|
32 |
+
|
33 |
+
if uploaded_file:
|
34 |
+
image = Image.open(uploaded_file)
|
35 |
+
|
36 |
+
col1, col2 = st.columns(2)
|
37 |
+
|
38 |
+
with col1:
|
39 |
+
st.image(image, caption="Image originale", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
with col2:
|
42 |
+
with torch.no_grad():
|
43 |
+
input_tensor = process_image(image)
|
44 |
+
predictions = model(input_tensor)
|
45 |
+
|
46 |
+
# Visualisation
|
47 |
+
img_array = np.array(image)
|
48 |
+
for pred in predictions:
|
49 |
+
if pred['score'] > 0.5:
|
50 |
+
box = pred['box']
|
51 |
+
x1, y1, x2, y2 = map(int, [box['xmin'], box['ymin'], box['xmax'], box['ymax']])
|
52 |
+
cv2.rectangle(img_array, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
53 |
+
text = f"Nodule: {pred['score']:.2f}"
|
54 |
+
cv2.putText(img_array, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
55 |
+
|
56 |
+
st.image(img_array, caption="Détections", use_container_width=True)
|
57 |
+
|
58 |
+
# Résultats
|
59 |
+
if len(predictions) > 0:
|
60 |
+
st.warning(f"⚠️ {len(predictions)} nodules détectés")
|
61 |
+
for i, pred in enumerate(predictions, 1):
|
62 |
+
if pred['score'] > 0.5:
|
63 |
+
st.write(f"Nodule {i}: Confiance {pred['score']:.1%}")
|
64 |
+
else:
|
65 |
+
st.success("✅ Aucun nodule détecté")
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
st.error(f"Erreur lors du chargement du modèle: {str(e)}")
|
69 |
+
st.info("Veuillez vérifier que le modèle est correctement configuré sur Hugging Face.")
|