Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -248,54 +248,67 @@ class YOLOVideoProcessor:
|
|
| 248 |
|
| 249 |
frame_count += 1
|
| 250 |
|
| 251 |
-
# Attendre un peu moins pour augmenter le FPS
|
| 252 |
-
time.sleep(0.01)
|
| 253 |
-
|
| 254 |
cap.release()
|
| 255 |
st.success("✅ Flux vidéo arrêté.")
|
| 256 |
|
| 257 |
|
| 258 |
-
# ---
|
| 259 |
def display_webcam_preview(camera_id, display_placeholder, poly1=None, poly2=None):
|
| 260 |
"""Affiche une prévisualisation simple de la webcam sans détection"""
|
|
|
|
| 261 |
cap = cv2.VideoCapture(camera_id)
|
| 262 |
|
| 263 |
# Configuration de la webcam pour une bonne performance
|
| 264 |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
|
| 265 |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
|
| 266 |
-
cap.set(cv2.CAP_PROP_FPS, 30)
|
| 267 |
|
| 268 |
if not cap.isOpened():
|
| 269 |
st.error("⚠️ Erreur : Impossible d'ouvrir la webcam.")
|
| 270 |
return False
|
| 271 |
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
st.session_state.preview_active = True
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
-
cap.release()
|
| 299 |
return True
|
| 300 |
|
| 301 |
|
|
@@ -309,21 +322,20 @@ def main():
|
|
| 309 |
|
| 310 |
st.title("🚗 Détection et comptage de Véhicules sur l'Autoroute de l'Avenir")
|
| 311 |
|
| 312 |
-
#
|
| 313 |
if 'webcam_active' not in st.session_state:
|
| 314 |
st.session_state.webcam_active = False
|
| 315 |
if 'preview_active' not in st.session_state:
|
| 316 |
st.session_state.preview_active = False
|
|
|
|
|
|
|
| 317 |
if 'processor' not in st.session_state:
|
| 318 |
st.session_state.processor = None
|
| 319 |
-
if 'preview_thread' not in st.session_state:
|
| 320 |
-
st.session_state.preview_thread = None
|
| 321 |
|
| 322 |
# Vérifier si le modèle existe déjà ou doit être téléchargé
|
| 323 |
model_path = "best.pt"
|
| 324 |
if not os.path.exists(model_path):
|
| 325 |
with st.spinner("📥 Chargement du modèle YOLO... Cela peut prendre un moment."):
|
| 326 |
-
# Utilisez hub.load pour télécharger le modèle depuis Hugging Face Hub
|
| 327 |
try:
|
| 328 |
from huggingface_hub import hf_hub_download
|
| 329 |
model_path = hf_hub_download(repo_id="ModuMLTECH/projet_trafic_2", filename="best.pt")
|
|
@@ -436,7 +448,7 @@ def main():
|
|
| 436 |
# Sélectionner la source vidéo
|
| 437 |
camera_options = {"Webcam par défaut": 0}
|
| 438 |
# Ajouter des options pour d'autres caméras si disponibles
|
| 439 |
-
for i in range(1,
|
| 440 |
try:
|
| 441 |
cap = cv2.VideoCapture(i)
|
| 442 |
if cap.isOpened():
|
|
@@ -458,29 +470,36 @@ def main():
|
|
| 458 |
preview_col1, preview_col2 = st.columns(2)
|
| 459 |
|
| 460 |
with preview_col1:
|
| 461 |
-
if st.button("👁️ Afficher la prévisualisation"):
|
| 462 |
# Arrêter la détection si elle est active
|
| 463 |
-
if st.session_state.webcam_active:
|
| 464 |
st.session_state.processor.stop_processing = True
|
| 465 |
st.session_state.webcam_active = False
|
| 466 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 467 |
|
| 468 |
-
#
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
)
|
| 476 |
-
|
| 477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
with preview_col2:
|
| 480 |
-
if st.button("⏹️ Arrêter la prévisualisation"):
|
|
|
|
|
|
|
| 481 |
st.session_state.preview_active = False
|
| 482 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 483 |
video_placeholder.empty() # Effacer l'affichage vidéo
|
|
|
|
| 484 |
|
| 485 |
# Section de détection
|
| 486 |
st.subheader("2️⃣ Détection des véhicules")
|
|
@@ -492,7 +511,7 @@ def main():
|
|
| 492 |
detect_col1, detect_col2 = st.columns(2)
|
| 493 |
|
| 494 |
with detect_col1:
|
| 495 |
-
if st.button("▶️ Démarrer la détection"):
|
| 496 |
if not valid_polygons:
|
| 497 |
st.error("❌ Les coordonnées des polygones doivent contenir **exactement 4 points**.")
|
| 498 |
elif st.session_state.webcam_active:
|
|
@@ -500,6 +519,7 @@ def main():
|
|
| 500 |
else:
|
| 501 |
# Arrêter la prévisualisation si elle est active
|
| 502 |
if st.session_state.preview_active:
|
|
|
|
| 503 |
st.session_state.preview_active = False
|
| 504 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 505 |
|
|
@@ -514,19 +534,22 @@ def main():
|
|
| 514 |
|
| 515 |
# Démarrer le traitement dans un thread séparé
|
| 516 |
processing_thread = threading.Thread(
|
| 517 |
-
target=
|
| 518 |
args=(camera_id, video_placeholder, count_placeholders)
|
| 519 |
)
|
| 520 |
processing_thread.daemon = True
|
| 521 |
processing_thread.start()
|
|
|
|
|
|
|
| 522 |
|
| 523 |
with detect_col2:
|
| 524 |
-
if st.button("⏹️ Arrêter la détection"):
|
| 525 |
-
if st.session_state.webcam_active and st.session_state
|
| 526 |
st.session_state.processor.stop_processing = True
|
| 527 |
st.session_state.webcam_active = False
|
| 528 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 529 |
video_placeholder.empty() # Effacer l'affichage vidéo
|
|
|
|
| 530 |
else:
|
| 531 |
st.warning("⚠️ Aucune détection en cours !")
|
| 532 |
|
|
|
|
| 248 |
|
| 249 |
frame_count += 1
|
| 250 |
|
|
|
|
|
|
|
|
|
|
| 251 |
cap.release()
|
| 252 |
st.success("✅ Flux vidéo arrêté.")
|
| 253 |
|
| 254 |
|
| 255 |
+
# --- FONCTION CORRIGÉE POUR AFFICHER PRÉVISUALISATION DE LA WEBCAM ---
|
| 256 |
def display_webcam_preview(camera_id, display_placeholder, poly1=None, poly2=None):
|
| 257 |
"""Affiche une prévisualisation simple de la webcam sans détection"""
|
| 258 |
+
# Utiliser directement OpenCV au lieu d'un thread
|
| 259 |
cap = cv2.VideoCapture(camera_id)
|
| 260 |
|
| 261 |
# Configuration de la webcam pour une bonne performance
|
| 262 |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
|
| 263 |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
|
|
|
|
| 264 |
|
| 265 |
if not cap.isOpened():
|
| 266 |
st.error("⚠️ Erreur : Impossible d'ouvrir la webcam.")
|
| 267 |
return False
|
| 268 |
|
| 269 |
+
# Lecture d'une seule frame pour vérifier que tout fonctionne
|
| 270 |
+
success, frame = cap.read()
|
| 271 |
+
if not success:
|
| 272 |
+
st.error("⚠️ Erreur lors de la lecture du flux vidéo.")
|
| 273 |
+
cap.release()
|
| 274 |
+
return False
|
| 275 |
+
|
| 276 |
+
# Initialiser st.session_state pour le contrôle de la prévisualisation
|
| 277 |
+
if 'preview_stop' not in st.session_state:
|
| 278 |
+
st.session_state.preview_stop = False
|
| 279 |
+
|
| 280 |
+
# Boucle principale de la prévisualisation
|
| 281 |
st.session_state.preview_active = True
|
| 282 |
|
| 283 |
+
try:
|
| 284 |
+
while st.session_state.preview_active and not st.session_state.preview_stop:
|
| 285 |
+
success, frame = cap.read()
|
| 286 |
+
if not success:
|
| 287 |
+
break
|
| 288 |
+
|
| 289 |
+
# Dessiner les polygones si disponibles
|
| 290 |
+
if poly1 and len(poly1) >= 3:
|
| 291 |
+
cv2.polylines(frame, [np.array(poly1, np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 292 |
+
if poly2 and len(poly2) >= 3:
|
| 293 |
+
cv2.polylines(frame, [np.array(poly2, np.int32)], isClosed=True, color=(255, 0, 0), thickness=2)
|
| 294 |
+
|
| 295 |
+
# Ajouter un texte explicatif
|
| 296 |
+
draw_text_with_background(frame, "Prévisualisation (sans détection)", (10, 30))
|
| 297 |
+
|
| 298 |
+
# Convertir et afficher l'image
|
| 299 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 300 |
+
img = Image.fromarray(frame_rgb)
|
| 301 |
+
|
| 302 |
+
# Afficher l'image dans le placeholder
|
| 303 |
+
display_placeholder.image(img, channels="RGB", use_column_width=True)
|
| 304 |
+
|
| 305 |
+
# Court délai pour ne pas saturer Streamlit
|
| 306 |
+
time.sleep(0.1)
|
| 307 |
+
except Exception as e:
|
| 308 |
+
st.error(f"Erreur lors de la prévisualisation: {e}")
|
| 309 |
+
finally:
|
| 310 |
+
cap.release()
|
| 311 |
|
|
|
|
| 312 |
return True
|
| 313 |
|
| 314 |
|
|
|
|
| 322 |
|
| 323 |
st.title("🚗 Détection et comptage de Véhicules sur l'Autoroute de l'Avenir")
|
| 324 |
|
| 325 |
+
# Initialisation des variables d'état de session
|
| 326 |
if 'webcam_active' not in st.session_state:
|
| 327 |
st.session_state.webcam_active = False
|
| 328 |
if 'preview_active' not in st.session_state:
|
| 329 |
st.session_state.preview_active = False
|
| 330 |
+
if 'preview_stop' not in st.session_state:
|
| 331 |
+
st.session_state.preview_stop = False
|
| 332 |
if 'processor' not in st.session_state:
|
| 333 |
st.session_state.processor = None
|
|
|
|
|
|
|
| 334 |
|
| 335 |
# Vérifier si le modèle existe déjà ou doit être téléchargé
|
| 336 |
model_path = "best.pt"
|
| 337 |
if not os.path.exists(model_path):
|
| 338 |
with st.spinner("📥 Chargement du modèle YOLO... Cela peut prendre un moment."):
|
|
|
|
| 339 |
try:
|
| 340 |
from huggingface_hub import hf_hub_download
|
| 341 |
model_path = hf_hub_download(repo_id="ModuMLTECH/projet_trafic_2", filename="best.pt")
|
|
|
|
| 448 |
# Sélectionner la source vidéo
|
| 449 |
camera_options = {"Webcam par défaut": 0}
|
| 450 |
# Ajouter des options pour d'autres caméras si disponibles
|
| 451 |
+
for i in range(1, 3): # Limiter à 3 caméras pour ne pas ralentir le chargement
|
| 452 |
try:
|
| 453 |
cap = cv2.VideoCapture(i)
|
| 454 |
if cap.isOpened():
|
|
|
|
| 470 |
preview_col1, preview_col2 = st.columns(2)
|
| 471 |
|
| 472 |
with preview_col1:
|
| 473 |
+
if st.button("👁️ Afficher la prévisualisation", key="start_preview"):
|
| 474 |
# Arrêter la détection si elle est active
|
| 475 |
+
if st.session_state.webcam_active and hasattr(st.session_state, 'processor'):
|
| 476 |
st.session_state.processor.stop_processing = True
|
| 477 |
st.session_state.webcam_active = False
|
| 478 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 479 |
|
| 480 |
+
# Réinitialiser le flag d'arrêt
|
| 481 |
+
st.session_state.preview_stop = False
|
| 482 |
+
st.session_state.preview_active = True
|
| 483 |
+
|
| 484 |
+
# Utiliser un thread pour la prévisualisation
|
| 485 |
+
preview_thread = threading.Thread(
|
| 486 |
+
target=display_webcam_preview,
|
| 487 |
+
args=(camera_id, video_placeholder, poly1, poly2)
|
| 488 |
+
)
|
| 489 |
+
preview_thread.daemon = True
|
| 490 |
+
preview_thread.start()
|
| 491 |
+
|
| 492 |
+
# Message de succès
|
| 493 |
+
st.success("📷 Prévisualisation démarrée")
|
| 494 |
|
| 495 |
with preview_col2:
|
| 496 |
+
if st.button("⏹️ Arrêter la prévisualisation", key="stop_preview"):
|
| 497 |
+
# Arrêter la prévisualisation
|
| 498 |
+
st.session_state.preview_stop = True
|
| 499 |
st.session_state.preview_active = False
|
| 500 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 501 |
video_placeholder.empty() # Effacer l'affichage vidéo
|
| 502 |
+
st.success("🛑 Prévisualisation arrêtée")
|
| 503 |
|
| 504 |
# Section de détection
|
| 505 |
st.subheader("2️⃣ Détection des véhicules")
|
|
|
|
| 511 |
detect_col1, detect_col2 = st.columns(2)
|
| 512 |
|
| 513 |
with detect_col1:
|
| 514 |
+
if st.button("▶️ Démarrer la détection", key="start_detection"):
|
| 515 |
if not valid_polygons:
|
| 516 |
st.error("❌ Les coordonnées des polygones doivent contenir **exactement 4 points**.")
|
| 517 |
elif st.session_state.webcam_active:
|
|
|
|
| 519 |
else:
|
| 520 |
# Arrêter la prévisualisation si elle est active
|
| 521 |
if st.session_state.preview_active:
|
| 522 |
+
st.session_state.preview_stop = True
|
| 523 |
st.session_state.preview_active = False
|
| 524 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 525 |
|
|
|
|
| 534 |
|
| 535 |
# Démarrer le traitement dans un thread séparé
|
| 536 |
processing_thread = threading.Thread(
|
| 537 |
+
target=processor.process_webcam,
|
| 538 |
args=(camera_id, video_placeholder, count_placeholders)
|
| 539 |
)
|
| 540 |
processing_thread.daemon = True
|
| 541 |
processing_thread.start()
|
| 542 |
+
|
| 543 |
+
st.success("✅ Détection démarrée")
|
| 544 |
|
| 545 |
with detect_col2:
|
| 546 |
+
if st.button("⏹️ Arrêter la détection", key="stop_detection"):
|
| 547 |
+
if st.session_state.webcam_active and hasattr(st.session_state, 'processor'):
|
| 548 |
st.session_state.processor.stop_processing = True
|
| 549 |
st.session_state.webcam_active = False
|
| 550 |
time.sleep(0.5) # Attendre que le thread se termine
|
| 551 |
video_placeholder.empty() # Effacer l'affichage vidéo
|
| 552 |
+
st.success("🛑 Détection arrêtée")
|
| 553 |
else:
|
| 554 |
st.warning("⚠️ Aucune détection en cours !")
|
| 555 |
|