Update app.py
Browse files
app.py
CHANGED
@@ -13,21 +13,17 @@ def load_model():
|
|
13 |
return processor, model
|
14 |
|
15 |
def create_overlay(image, mask, alpha=0.5):
|
16 |
-
# Convertir le masque en RGB
|
17 |
mask_rgb = np.zeros((*mask.shape, 3), dtype=np.uint8)
|
18 |
-
mask_rgb[mask == 1] = [255, 0, 0]
|
19 |
|
20 |
-
# Convertir l'image en array numpy
|
21 |
image_np = np.array(image)
|
22 |
-
if len(image_np.shape) == 2:
|
23 |
image_np = np.stack([image_np] * 3, axis=-1)
|
24 |
|
25 |
-
# Redimensionner le masque si nécessaire
|
26 |
if mask_rgb.shape[:2] != image_np.shape[:2]:
|
27 |
mask_rgb = Image.fromarray(mask_rgb).resize(image.size)
|
28 |
mask_rgb = np.array(mask_rgb)
|
29 |
|
30 |
-
# Créer l'overlay
|
31 |
overlay = Image.fromarray((image_np * (1 - alpha) + mask_rgb * alpha).astype(np.uint8))
|
32 |
return overlay
|
33 |
|
@@ -39,21 +35,18 @@ def main():
|
|
39 |
uploaded_file = st.file_uploader("Télécharger une radiographie thoracique", type=["jpg", "jpeg", "png"])
|
40 |
|
41 |
if uploaded_file:
|
42 |
-
image
|
|
|
43 |
st.image(image, caption="Image originale", use_column_width=True)
|
44 |
|
45 |
if st.button("Analyser"):
|
46 |
with st.spinner("Analyse en cours..."):
|
47 |
try:
|
48 |
-
# Préparer l'image
|
49 |
inputs = processor(images=image, return_tensors="pt")
|
50 |
-
|
51 |
-
# Prédiction
|
52 |
outputs = model(**inputs)
|
53 |
logits = outputs.logits
|
54 |
predicted_mask = torch.argmax(logits, dim=1).squeeze().numpy()
|
55 |
|
56 |
-
# Créer et afficher l'overlay
|
57 |
overlay = create_overlay(image, predicted_mask)
|
58 |
st.image(overlay, caption="Zones détectées", use_column_width=True)
|
59 |
|
|
|
13 |
return processor, model
|
14 |
|
15 |
def create_overlay(image, mask, alpha=0.5):
|
|
|
16 |
mask_rgb = np.zeros((*mask.shape, 3), dtype=np.uint8)
|
17 |
+
mask_rgb[mask == 1] = [255, 0, 0]
|
18 |
|
|
|
19 |
image_np = np.array(image)
|
20 |
+
if len(image_np.shape) == 2:
|
21 |
image_np = np.stack([image_np] * 3, axis=-1)
|
22 |
|
|
|
23 |
if mask_rgb.shape[:2] != image_np.shape[:2]:
|
24 |
mask_rgb = Image.fromarray(mask_rgb).resize(image.size)
|
25 |
mask_rgb = np.array(mask_rgb)
|
26 |
|
|
|
27 |
overlay = Image.fromarray((image_np * (1 - alpha) + mask_rgb * alpha).astype(np.uint8))
|
28 |
return overlay
|
29 |
|
|
|
35 |
uploaded_file = st.file_uploader("Télécharger une radiographie thoracique", type=["jpg", "jpeg", "png"])
|
36 |
|
37 |
if uploaded_file:
|
38 |
+
# Ouvrir et convertir l'image en RGB
|
39 |
+
image = Image.open(uploaded_file).convert('RGB')
|
40 |
st.image(image, caption="Image originale", use_column_width=True)
|
41 |
|
42 |
if st.button("Analyser"):
|
43 |
with st.spinner("Analyse en cours..."):
|
44 |
try:
|
|
|
45 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
|
46 |
outputs = model(**inputs)
|
47 |
logits = outputs.logits
|
48 |
predicted_mask = torch.argmax(logits, dim=1).squeeze().numpy()
|
49 |
|
|
|
50 |
overlay = create_overlay(image, predicted_mask)
|
51 |
st.image(overlay, caption="Zones détectées", use_column_width=True)
|
52 |
|