Stroke-ia commited on
Commit
fbbcf08
·
verified ·
1 Parent(s): 6085d71

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from ultralytics import YOLO
4
+ import cv2, os
5
+ from datetime import datetime
6
+ import numpy as np
7
+
8
+ # ---------------- Config générale ----------------
9
+ MODEL_PATH = "best.pt"
10
+ SAVE_DIR = os.path.join("/tmp", "results")
11
+ os.makedirs(SAVE_DIR, exist_ok=True)
12
+
13
+ # Charger le modèle
14
+ model = YOLO(MODEL_PATH)
15
+
16
+ # ---------------- Fonctions utilitaires ----------------
17
+ def predict_image(image, conf=0.25, show_labels=True):
18
+ image = np.array(image)
19
+ if image.shape[2] == 4:
20
+ image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
21
+ else:
22
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
23
+
24
+ results = model.predict(source=image, conf=conf, verbose=False)
25
+ annotated_image = results[0].plot(labels=show_labels)
26
+
27
+ out_path = os.path.join(SAVE_DIR, f"image_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png")
28
+ cv2.imwrite(out_path, annotated_image)
29
+ return out_path
30
+
31
+ def predict_video(video_path, conf=0.25, show_labels=True):
32
+ cap = cv2.VideoCapture(video_path)
33
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
34
+ out_path = os.path.join(SAVE_DIR, f"video_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4")
35
+
36
+ fps = cap.get(cv2.CAP_PROP_FPS) or 30
37
+ width, height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
38
+ out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
39
+
40
+ while cap.isOpened():
41
+ ret, frame = cap.read()
42
+ if not ret:
43
+ break
44
+ results = model.predict(frame, conf=conf, verbose=False)
45
+ annotated = results[0].plot(labels=show_labels)
46
+ out.write(annotated)
47
+
48
+ cap.release()
49
+ out.release()
50
+ return out_path
51
+
52
+ # ---------------- Interface Streamlit ----------------
53
+ st.title("🧠 Stroke-IA – Détection AVC par IA")
54
+
55
+ # Sidebar (paramètres utilisateur)
56
+ st.sidebar.header("⚙️ Paramètres")
57
+ conf_threshold = st.sidebar.slider("Seuil de confiance", 0.1, 1.0, 0.25, 0.05)
58
+ show_labels = st.sidebar.checkbox("Afficher les labels", value=True)
59
+
60
+ st.sidebar.header("📂 Exemples rapides")
61
+ if st.sidebar.button("Tester une image exemple"):
62
+ if os.path.exists("example.jpg"):
63
+ img = Image.open("example.jpg")
64
+ path = predict_image(img, conf=conf_threshold, show_labels=show_labels)
65
+ st.image(path, caption="Exemple annoté", use_column_width=True)
66
+ else:
67
+ st.warning("⚠️ Aucun fichier example.jpg trouvé dans le projet.")
68
+
69
+ if st.sidebar.button("Tester une vidéo exemple"):
70
+ if os.path.exists("example.mp4"):
71
+ path = predict_video("example.mp4", conf=conf_threshold, show_labels=show_labels)
72
+ st.video(path)
73
+ else:
74
+ st.warning("⚠️ Aucun fichier example.mp4 trouvé dans le projet.")
75
+
76
+ # Section vidéo upload
77
+ st.header("🎥 Détection sur vidéo")
78
+ video_file = st.file_uploader("Uploader une vidéo (mp4, mov, etc.)", type=["mp4", "mov"])
79
+ if video_file and st.button("Analyser la vidéo"):
80
+ temp_path = os.path.join(SAVE_DIR, "temp_video.mp4")
81
+ with open(temp_path, "wb") as f:
82
+ f.write(video_file.read())
83
+ result_path = predict_video(temp_path, conf=conf_threshold, show_labels=show_labels)
84
+ st.video(result_path)
85
+
86
+ # Section image upload
87
+ st.header("🖼️ Détection sur image")
88
+ image_file = st.file_uploader("Uploader une image", type=["jpg", "jpeg", "png"])
89
+ if image_file and st.button("Analyser l'image"):
90
+ image = Image.open(image_file)
91
+ result_path = predict_image(image, conf=conf_threshold, show_labels=show_labels)
92
+ st.image(result_path, caption="Image annotée", use_column_width=True)
93
+
94
+ # Disclaimer
95
+ st.markdown(f"""
96
+ ---
97
+ ⚠️ **Disclaimer :** Stroke-IA est une démo technique, pas un avis médical.
98
+ © {datetime.now().year} — Badsi Djilali.
99
+ """)