Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,30 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
from PIL import Image
|
4 |
-
import
|
5 |
-
import numpy as np
|
6 |
|
7 |
-
st.set_page_config(
|
8 |
|
9 |
@st.cache_resource
|
10 |
def load_model():
|
11 |
-
|
12 |
-
model = SegformerForSemanticSegmentation.from_pretrained("Tianmu28/segformer-b0-segments-lungs-xray")
|
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 |
|
30 |
def main():
|
31 |
-
st.title("
|
32 |
|
33 |
-
|
34 |
|
35 |
-
uploaded_file = st.file_uploader("Télécharger une radiographie
|
36 |
|
37 |
if uploaded_file:
|
38 |
-
|
39 |
-
|
40 |
-
st.image(
|
41 |
|
42 |
if st.button("Analyser"):
|
43 |
-
|
44 |
-
|
45 |
-
|
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 |
-
|
53 |
-
except Exception as e:
|
54 |
-
st.error(f"Erreur: {str(e)}")
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from ultralytics import YOLO
|
3 |
from PIL import Image
|
4 |
+
import io
|
|
|
5 |
|
6 |
+
st.set_page_config(layout="centered")
|
7 |
|
8 |
@st.cache_resource
|
9 |
def load_model():
|
10 |
+
return YOLO("keremberke/yolov8m-chest-xray-classification")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def main():
|
13 |
+
st.title("Analyse Radiographie Thoracique")
|
14 |
|
15 |
+
model = load_model()
|
16 |
|
17 |
+
uploaded_file = st.file_uploader("Télécharger une radiographie", type=["jpg", "jpeg", "png"])
|
18 |
|
19 |
if uploaded_file:
|
20 |
+
image = Image.open(uploaded_file)
|
21 |
+
resized_image = image.resize((640, 640))
|
22 |
+
st.image(resized_image, width=400)
|
23 |
|
24 |
if st.button("Analyser"):
|
25 |
+
results = model.predict(source=resized_image)
|
26 |
+
st.write(f"Résultat: {results[0].names[results[0].probs.argmax()]}")
|
27 |
+
st.write(f"Confiance: {results[0].probs.max():.2%}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
if __name__ == "__main__":
|
30 |
main()
|