File size: 24,139 Bytes
f145ba4
 
 
 
c38e692
 
 
d1c42bc
 
 
c38e692
f33ab54
 
 
 
 
 
 
 
 
 
 
 
 
 
c38e692
f33ab54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c38e692
 
d1c42bc
c38e692
d1c42bc
f33ab54
 
 
d1c42bc
320dc3c
 
f33ab54
320dc3c
f33ab54
 
 
d1c42bc
f33ab54
 
c38e692
f33ab54
 
 
c38e692
 
 
 
d1c42bc
 
 
c38e692
320dc3c
 
 
 
 
 
 
 
f33ab54
 
320dc3c
c38e692
320dc3c
c38e692
d1c42bc
f33ab54
 
320dc3c
f33ab54
 
 
 
 
 
d1c42bc
320dc3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1c42bc
c38e692
320dc3c
c38e692
 
 
 
f33ab54
 
320dc3c
c38e692
f33ab54
 
 
320dc3c
f33ab54
 
c38e692
f33ab54
c38e692
 
d1c42bc
c38e692
 
 
 
 
 
320dc3c
f33ab54
320dc3c
 
c38e692
320dc3c
d1c42bc
320dc3c
d1c42bc
 
 
f33ab54
 
 
 
320dc3c
f33ab54
 
 
d1c42bc
c38e692
d1c42bc
c38e692
 
 
 
 
320dc3c
 
f33ab54
c38e692
 
320dc3c
 
d1c42bc
 
f33ab54
320dc3c
 
 
d1c42bc
320dc3c
 
 
d1c42bc
 
f33ab54
320dc3c
f33ab54
d1c42bc
f33ab54
 
 
d1c42bc
f33ab54
 
320dc3c
 
d1c42bc
 
320dc3c
 
d1c42bc
320dc3c
 
 
f33ab54
320dc3c
 
 
f33ab54
 
d1c42bc
 
f33ab54
320dc3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1c42bc
320dc3c
 
 
f33ab54
320dc3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8dbb15
320dc3c
f33ab54
320dc3c
 
b683266
a8dbb15
320dc3c
 
 
 
d1c42bc
 
320dc3c
d1c42bc
f33ab54
 
 
 
 
 
 
 
d1c42bc
 
 
f33ab54
 
 
 
 
320dc3c
f33ab54
 
 
320dc3c
 
f33ab54
320dc3c
 
f33ab54
320dc3c
 
f33ab54
320dc3c
f33ab54
320dc3c
f33ab54
320dc3c
f33ab54
320dc3c
 
 
 
 
f33ab54
320dc3c
 
 
f33ab54
 
 
 
 
320dc3c
d1c42bc
c38e692
 
f145ba4
c38e692
 
 
f33ab54
c38e692
f33ab54
c38e692
f33ab54
320dc3c
f33ab54
 
 
 
c38e692
 
f33ab54
c38e692
 
a8dbb15
f33ab54
c38e692
 
f33ab54
c38e692
f33ab54
 
d1c42bc
f33ab54
 
d1c42bc
c38e692
f33ab54
c38e692
b683266
320dc3c
c38e692
b683266
f33ab54
320dc3c
f33ab54
d1c42bc
320dc3c
 
 
 
 
 
 
f33ab54
d1c42bc
 
f33ab54
 
 
 
 
 
d1c42bc
f33ab54
d1c42bc
 
 
 
f33ab54
d1c42bc
320dc3c
c38e692
 
f33ab54
 
 
 
c38e692
 
f33ab54
 
 
c38e692
d1c42bc
c38e692
d1c42bc
 
 
 
320dc3c
 
f33ab54
c38e692
320dc3c
c38e692
f33ab54
320dc3c
 
 
f33ab54
320dc3c
 
f33ab54
 
 
320dc3c
f33ab54
 
 
 
 
320dc3c
f33ab54
c38e692
320dc3c
f145ba4
f33ab54
d1c42bc
 
f33ab54
d1c42bc
f33ab54
d1c42bc
 
 
 
 
f33ab54
d1c42bc
f33ab54
d1c42bc
 
f33ab54
 
 
 
 
320dc3c
f33ab54
a8dbb15
f33ab54
a8dbb15
 
320dc3c
a8dbb15
 
 
 
 
 
 
320dc3c
 
f33ab54
a8dbb15
 
f33ab54
320dc3c
a8dbb15
320dc3c
f33ab54
320dc3c
f33ab54
a8dbb15
 
 
 
320dc3c
f33ab54
a8dbb15
 
d1c42bc
f33ab54
f145ba4
c38e692
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import streamlit as st
import cv2
import tempfile
import os
import time
import numpy as np
from ultralytics import YOLO
import threading
from PIL import Image
import torch

# --- FONCTIONS UTILES ---
def draw_text_with_background(
    image,
    text,
    position,
    font=cv2.FONT_HERSHEY_SIMPLEX,
    font_scale=1,
    font_thickness=2,
    text_color=(255, 255, 255),
    bg_color=(0, 0, 0),
    padding=5,
):
    """Ajoute du texte avec un fond sur une image OpenCV (bornes sécurisées)."""
    (text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
    x, y = position
    tl_x = max(0, x)
    tl_y = max(0, y - text_height - padding)
    br_x = min(image.shape[1] - 1, x + text_width + padding * 2)
    br_y = min(image.shape[0] - 1, y + padding)

    cv2.rectangle(image, (tl_x, tl_y), (br_x, br_y), bg_color, -1)
    cv2.putText(
        image,
        text,
        (tl_x + padding, min(y, image.shape[0] - 1)),
        font,
        font_scale,
        text_color,
        font_thickness,
        cv2.LINE_AA,
    )


# --- CLASSE YOLO OPTIMISÉE ---
class YOLOVideoProcessor:
    def __init__(self, model_path, poly1, poly2, tracker_method="bot"):
        # Device
        self.device = "cuda" if torch.cuda.is_available() else "cpu"

        # Paramètres d'optimisation
        self.frame_skip = 2
        self.downsample_factor = 0.5
        self.img_size = 640
        self.conf_threshold = 0.35

        # Modèle
        self.model = YOLO(model_path)
        self.model.to(self.device)

        # Tracking
        self.tracker_method = tracker_method
        self.tracker_config = "botsort.yaml" if self.tracker_method.lower() == "bot" else "bytetrack.yaml"

        # États
        self.unique_region1_ids = set()
        self.unique_region2_ids = set()
        self.poly1 = poly1
        self.poly2 = poly2
        self.stop_processing = False
        self.last_processed_frame = None
        self.current_frame = 0

        # Paramètres anti-duplicata pour camions longs
        self.iou_threshold = 0.3  # Seuil IoU pour fusionner les détections proches
        self.min_box_area = 500   # Surface minimale pour être considéré comme véhicule
        self.max_aspect_ratio = 5.0  # Ratio hauteur/largeur max pour éviter détections étirées
        
        # Historique des détections pour filtrage temporel
        self.detection_history = {}  # {track_id: {'boxes': [], 'frames': []}}
        self.history_length = 5  # Nombre de frames à garder en mémoire

    @staticmethod
    def is_in_region(center, poly):
        poly_np = np.array(poly, dtype=np.int32)
        return cv2.pointPolygonTest(poly_np, center, False) >= 0

    def reset_counts(self):
        self.unique_region1_ids.clear()
        self.unique_region2_ids.clear()
        self.detection_history.clear()

    def _pick_fourcc(self, output_path):
        ext = os.path.splitext(output_path)[1].lower()
        if ext == ".mp4":
            return cv2.VideoWriter_fourcc(*"mp4v")
        return cv2.VideoWriter_fourcc(*"XVID")

    def calculate_iou(self, box1, box2):
        """Calcule l'IoU (Intersection over Union) entre deux boîtes"""
        x1_min, y1_min, x1_max, y1_max = box1
        x2_min, y2_min, x2_max, y2_max = box2
        
        # Intersection
        inter_x_min = max(x1_min, x2_min)
        inter_y_min = max(y1_min, y2_min)
        inter_x_max = min(x1_max, x2_max)
        inter_y_max = min(y1_max, y2_max)
        
        inter_area = max(0, inter_x_max - inter_x_min) * max(0, inter_y_max - inter_y_min)
        
        # Union
        box1_area = (x1_max - x1_min) * (y1_max - y1_min)
        box2_area = (x2_max - x2_min) * (y2_max - y2_min)
        union_area = box1_area + box2_area - inter_area
        
        if union_area == 0:
            return 0
        
        return inter_area / union_area

    def filter_overlapping_detections(self, boxes_coords, track_ids, confidences):
        """Filtre les détections qui se chevauchent (ex: plusieurs détections sur un camion)"""
        if len(boxes_coords) == 0:
            return [], [], []
        
        # Créer une liste de détections avec leurs indices
        detections = []
        for i, (box, tid, conf) in enumerate(zip(boxes_coords, track_ids, confidences)):
            x_min, y_min, x_max, y_max = box
            area = (x_max - x_min) * (y_max - y_min)
            aspect_ratio = (y_max - y_min) / max(1, x_max - x_min)
            
            # Filtrer les détections trop petites ou avec un aspect ratio bizarre
            if area < self.min_box_area or aspect_ratio > self.max_aspect_ratio:
                continue
                
            detections.append({
                'index': i,
                'box': box,
                'track_id': tid,
                'conf': conf,
                'area': area
            })
        
        # Trier par confiance décroissante
        detections.sort(key=lambda x: x['conf'], reverse=True)
        
        # Non-Maximum Suppression manuel
        keep_indices = []
        while len(detections) > 0:
            # Garder la détection avec la plus haute confiance
            best = detections.pop(0)
            keep_indices.append(best['index'])
            
            # Supprimer les détections qui se chevauchent trop avec la meilleure
            filtered_detections = []
            for det in detections:
                iou = self.calculate_iou(best['box'], det['box'])
                if iou < self.iou_threshold:  # Garder si IoU faible (pas de chevauchement)
                    filtered_detections.append(det)
            
            detections = filtered_detections
        
        # Retourner les détections filtrées
        filtered_boxes = [boxes_coords[i] for i in keep_indices]
        filtered_ids = [track_ids[i] for i in keep_indices]
        filtered_confs = [confidences[i] for i in keep_indices]
        
        return filtered_boxes, filtered_ids, filtered_confs

    def update_detection_history(self, track_id, box, frame_num):
        """Met à jour l'historique des détections pour un véhicule"""
        if track_id not in self.detection_history:
            self.detection_history[track_id] = {'boxes': [], 'frames': []}
        
        self.detection_history[track_id]['boxes'].append(box)
        self.detection_history[track_id]['frames'].append(frame_num)
        
        # Garder seulement les N dernières frames
        if len(self.detection_history[track_id]['boxes']) > self.history_length:
            self.detection_history[track_id]['boxes'].pop(0)
            self.detection_history[track_id]['frames'].pop(0)

    def is_stable_detection(self, track_id):
        """Vérifie si une détection est stable (pas un faux positif temporaire)"""
        if track_id not in self.detection_history:
            return False
        
        # Considérer stable si détecté sur au moins 3 frames
        return len(self.detection_history[track_id]['boxes']) >= 3

    def process_video(self, video_path, output_path, progress_bar=None):
        """Traite une vidéo enregistrée avec optimisations"""
        cap = cv2.VideoCapture(video_path)
        if not cap.isOpened():
            st.error("⚠️ Erreur : Impossible d'ouvrir la vidéo.")
            return

        frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = cap.get(cv2.CAP_PROP_FPS)
        if not fps or fps <= 1e-3:
            fps = 30.0

        fourcc = self._pick_fourcc(output_path)
        out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
        if not out.isOpened():
            st.error("⚠️ Erreur : Impossible d'ouvrir la vidéo de sortie (codec).")
            cap.release()
            return

        self.reset_counts()
        processed_frames = 0
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        frame_count = 0

        while cap.isOpened():
            success, frame = cap.read()
            if not success:
                break

            # Progression
            if progress_bar is not None and total_frames > 0:
                progress = min(1.0, processed_frames / float(total_frames))
                progress_bar.progress(progress)

            # Skip de frames
            if frame_count % self.frame_skip == 0:
                processed_frame = self.process_frame(frame, frame_count)
                self.last_processed_frame = processed_frame
            else:
                processed_frame = self.last_processed_frame if self.last_processed_frame is not None else frame

            if processed_frame is None:
                processed_frame = frame

            # S'assurer de la taille attendue
            if processed_frame.shape[1] != frame_width or processed_frame.shape[0] != frame_height:
                processed_frame = cv2.resize(processed_frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA)

            out.write(processed_frame)
            processed_frames += 1
            frame_count += 1

        cap.release()
        out.release()
        cv2.destroyAllWindows()

        if processed_frames == 0:
            st.error("⚠️ Aucune image n'a été écrite dans la vidéo de sortie !")

        return len(self.unique_region1_ids), len(self.unique_region2_ids)

    def process_frame(self, frame, frame_num=0):
        """Traite une image individuelle avec YOLO et le tracking, avec filtrage anti-duplicata"""
        if frame is None:
            return None

        # Redimensionner l'image pour accélérer le traitement
        orig_height, orig_width = frame.shape[:2]
        resized_width, resized_height = orig_width, orig_height
        if self.downsample_factor < 1.0:
            resized_width = max(1, int(orig_width * self.downsample_factor))
            resized_height = max(1, int(orig_height * self.downsample_factor))
            resized_frame = cv2.resize(frame, (resized_width, resized_height), interpolation=cv2.INTER_AREA)
        else:
            resized_frame = frame

        # Détection + tracking
        with torch.no_grad():
            results = self.model.track(
                resized_frame,
                persist=True,
                tracker=self.tracker_config,
                conf=self.conf_threshold,
                imgsz=self.img_size,
                device=self.device,
                classes=[2, 5, 7],  # COCO: 2=car, 5=bus, 7=truck (évite autres objets)
                verbose=False
            )

        display_frame = frame.copy()
        frame_height, frame_width = display_frame.shape[:2]

        # Dessiner les polygones
        cv2.polylines(display_frame, [np.array(self.poly1, np.int32)], isClosed=True, color=(0, 255, 0), thickness=2)
        cv2.polylines(display_frame, [np.array(self.poly2, np.int32)], isClosed=True, color=(255, 0, 0), thickness=2)

        # Échelle pour remonter aux coords originales
        scale_x = orig_width / float(resized_width)
        scale_y = orig_height / float(resized_height)

        if results and len(results) > 0 and getattr(results[0], "boxes", None) is not None:
            try:
                boxes = results[0].boxes.xywh.cpu().numpy()
                ids_tensor = results[0].boxes.id
                confs = results[0].boxes.conf.cpu().numpy()
                
                if ids_tensor is None:
                    track_ids = [None] * len(boxes)
                else:
                    track_ids = ids_tensor.int().cpu().tolist()

                # Convertir les boîtes en format [x_min, y_min, x_max, y_max]
                boxes_coords = []
                for x, y, w, h in boxes:
                    center_x = int(x * scale_x)
                    center_y = int(y * scale_y)
                    width = int(w * scale_x)
                    height = int(h * scale_y)
                    x_min = max(0, center_x - width // 2)
                    y_min = max(0, center_y - height // 2)
                    x_max = min(frame_width - 1, center_x + width // 2)
                    y_max = min(frame_height - 1, center_y + height // 2)
                    boxes_coords.append([x_min, y_min, x_max, y_max])

                # Filtrer les détections qui se chevauchent
                filtered_boxes, filtered_ids, filtered_confs = self.filter_overlapping_detections(
                    boxes_coords, track_ids, confs
                )

                # Traiter les détections filtrées
                for box, track_id, conf in zip(filtered_boxes, filtered_ids, filtered_confs):
                    if track_id is None:
                        continue
                    
                    x_min, y_min, x_max, y_max = box
                    center_x = (x_min + x_max) // 2
                    center_y = (y_min + y_max) // 2
                    center_point = (center_x, center_y)
                    
                    # Mettre à jour l'historique
                    self.update_detection_history(track_id, box, frame_num)
                    
                    # Compter seulement les détections stables
                    if self.is_stable_detection(track_id):
                        if self.is_in_region(center_point, self.poly1):
                            self.unique_region1_ids.add(track_id)
                        if self.is_in_region(center_point, self.poly2):
                            self.unique_region2_ids.add(track_id)
                    
                    # Dessiner la boîte (vert si stable, jaune sinon)
                    color = (0, 255, 0) if self.is_stable_detection(track_id) else (0, 255, 255)
                    cv2.rectangle(display_frame, (x_min, y_min), (x_max, y_max), color, 2)
                    
                    # Afficher l'ID et la confiance
                    label = f"ID:{track_id} {conf:.2f}"
                    cv2.putText(display_frame, label, (x_min, y_min - 10),
                               cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
                    
            except Exception as e:
                draw_text_with_background(display_frame, f"Tracking error: {e}", (10, 60), bg_color=(80, 0, 0))

        # Affichage du comptage
        # draw_text_with_background(display_frame, f"Total Sens 1: {len(self.unique_region1_ids)}", (10, frame_height - 50))
        draw_text_with_background(display_frame, f"Total: {len(self.unique_region2_ids)}", (frame_width - 300, frame_height - 50))

        return display_frame

    def process_webcam(self, camera_id=0, display_placeholder=None, count_placeholders=None):
        """Traite la vidéo en temps réel depuis une webcam"""
        cap = cv2.VideoCapture(camera_id)
        if not cap.isOpened():
            st.error("⚠️ Erreur : Impossible d'ouvrir la webcam.")
            return

        try:
            cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
            cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
            cap.set(cv2.CAP_PROP_FPS, 30)
        except Exception:
            pass

        self.reset_counts()
        self.stop_processing = False
        frame_count = 0
        last_ts = time.time()

        while not self.stop_processing:
            success, frame = cap.read()
            if not success:
                st.error("⚠️ Erreur lors de la lecture du flux vidéo.")
                break

            if frame_count % self.frame_skip == 0:
                processed_frame = self.process_frame(frame, frame_count)
                self.last_processed_frame = processed_frame
                now = time.time()
                dt = max(1e-6, now - last_ts)
                fps = 1.0 / dt
                last_ts = now
                if processed_frame is not None:
                    draw_text_with_background(processed_frame, f"FPS: {fps:.1f}", (10, 30))
            else:
                processed_frame = self.last_processed_frame if self.last_processed_frame is not None else frame

            if processed_frame is not None:
                try:
                    processed_frame_rgb = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
                except Exception:
                    processed_frame_rgb = processed_frame
                img = Image.fromarray(processed_frame_rgb)

                if display_placeholder:
                    display_placeholder.image(img, channels="RGB", use_column_width=True)

                if count_placeholders and len(count_placeholders) >= 2:
                    count_placeholders[0].metric("Véhicules Sens 1 (Vert)", len(self.unique_region1_ids))
                    count_placeholders[1].metric("Véhicules Sens 2 (Rouge)", len(self.unique_region2_ids))

            frame_count += 1
            time.sleep(0.01)

        cap.release()
        st.success("✅ Flux vidéo arrêté.")


# --- INTERFACE STREAMLIT ---
def main():
    st.set_page_config(
        page_title="Détecteur de Véhicules",
        page_icon="🚗",
        layout="wide"
    )

    st.title("🚗 Détection et comptage de Véhicules sur l'Autoroute de l'Avenir")

    # Session state
    st.session_state.setdefault("webcam_active", False)
    st.session_state.setdefault("processor", None)

    # Modèle
    model_path = "best.pt"
    if not os.path.exists(model_path):
        with st.spinner("📥 Chargement du modèle YOLO..."):
            try:
                from huggingface_hub import hf_hub_download
                model_path = hf_hub_download(repo_id="ModuMLTECH/Trafic_congestion", filename="best.pt")
                st.success("✅ Modèle chargé depuis Hugging Face Hub.")
            except Exception as e:
                st.error(f"❌ Erreur lors du chargement du modèle: {e}")
                st.warning("⚠️ Utilisation du modèle YOLO public à la place (yolov8n.pt).")
                model_path = "yolov8n.pt"

    # Tabs
    tab1, tab2 = st.tabs(["📹 Analyse de Vidéo", "🎥 Détection en Temps Réel"])

    # Sidebar
    with st.sidebar:
        st.header("🔹 Paramètres")

        st.subheader("📍 Polygone 1 (vert)")
        poly1_input = st.text_area("Entrez 4 points (x,y) séparés par des espaces", "0,0 0,0 0,0 0,0")

        st.subheader("📍 Polygone 2 (rouge)")
        poly2_input = st.text_area("Entrez 4 points (x,y) séparés par des espaces", "500,150 700,150 1100,530 630,530")

        tracker_method = st.selectbox("Méthode de tracking", ["bot", "byte"], index=0)

        st.subheader("🚀 Paramètres d'optimisation")
        frame_skip = st.slider("Skip de frames", 1, 5, 2)
        downsample = st.slider("Facteur d'échelle", 0.3, 1.0, 0.5, 0.1)
        conf_threshold = st.slider("Seuil de confiance", 0.1, 0.9, 0.35, 0.05)
        
        st.subheader("🔧 Anti-duplicata")
        iou_thresh = st.slider("Seuil IoU (fusion détections)", 0.1, 0.9, 0.3, 0.05)
        min_area = st.slider("Surface minimale (pixels²)", 100, 2000, 500, 100)

    def parse_polygon(input_text):
        try:
            pts = []
            for token in input_text.replace(";", " ").split():
                x, y = token.split(",")
                pts.append((int(x), int(y)))
            return pts
        except Exception:
            return []

    poly1 = parse_polygon(poly1_input)
    poly2 = parse_polygon(poly2_input)
    valid_polygons = len(poly1) == 4 and len(poly2) == 4

    # Onglet 1: Analyse vidéo
    with tab1:
        uploaded_file = st.file_uploader("📂 Upload une vidéo", type=["mp4", "avi", "mkv", "mov"])
        if uploaded_file is not None:
            temp_dir = tempfile.mkdtemp()
            ext = os.path.splitext(uploaded_file.name)[1].lower() or ".mp4"
            input_video_path = os.path.join(temp_dir, f"input_video{ext}")
            output_video_path = os.path.join(temp_dir, f"output_video{ext}")

            with open(input_video_path, "wb") as f:
                f.write(uploaded_file.getbuffer())

            st.video(input_video_path)

            if st.button("▶️ Lancer la détection"):
                if valid_polygons:
                    progress_bar = st.progress(0)
                    processor = YOLOVideoProcessor(model_path, poly1, poly2, tracker_method)
                    processor.frame_skip = frame_skip
                    processor.downsample_factor = downsample
                    processor.conf_threshold = conf_threshold
                    processor.iou_threshold = iou_thresh
                    processor.min_box_area = min_area

                    start_time = time.time()
                    counts = processor.process_video(input_video_path, output_video_path, progress_bar=progress_bar)
                    end_time = time.time()
                    if counts:
                        count1, count2 = counts
                        st.success(f"✅ Traitement terminé en {end_time - start_time:.2f} s")

                        col_result1, col_result2 = st.columns(2)
                        col_result1.metric("Véhicules Sens 1 (Vert)", count1)
                        col_result2.metric("Véhicules Sens 2 (Rouge)", count2)

                        st.subheader("Vidéo traitée")
                        st.video(output_video_path)

                        with open(output_video_path, "rb") as file:
                            st.download_button(
                                label="⬇️ Télécharger la vidéo",
                                data=file,
                                file_name=f"video_traitee{ext}",
                                mime=f"video/{ext.strip('.')}",
                            )
                else:
                    st.error("❌ Les coordonnées des polygones doivent contenir **exactement 4 points**.")

    # Onglet 2: Webcam
    with tab2:
        st.header("Détection en Temps Réel avec Webcam")

        camera_options = {"Webcam par défaut": 0}
        for i in range(1, 5):
            try:
                cap = cv2.VideoCapture(i)
                if cap.isOpened():
                    camera_options[f"Caméra {i}"] = i
                cap.release()
            except Exception:
                pass

        selected_camera = st.selectbox("Sélectionnez la source vidéo", list(camera_options.keys()))
        camera_id = camera_options[selected_camera]

        video_placeholder = st.empty()
        col1, col2 = st.columns(2)
        count_placeholders = [col1.empty(), col2.empty()]

        st.info("ℹ️ Optimisations: redimensionnement, skip de frames, filtrage anti-duplicata, CUDA si disponible.")

        col_start, col_stop = st.columns(2)

        if col_start.button("▶️ Démarrer la détection en direct"):
            if not valid_polygons:
                st.error("❌ Les coordonnées des polygones doivent contenir **exactement 4 points**.")
            elif st.session_state.webcam_active:
                st.warning("⚠️ La webcam est déjà active !")
            else:
                processor = YOLOVideoProcessor(model_path, poly1, poly2, tracker_method)
                processor.frame_skip = frame_skip
                processor.downsample_factor = downsample
                processor.conf_threshold = conf_threshold
                processor.iou_threshold = iou_thresh
                processor.min_box_area = min_area

                st.session_state.processor = processor
                st.session_state.webcam_active = True

                threading.Thread(
                    target=st.session_state.processor.process_webcam,
                    args=(camera_id, video_placeholder, count_placeholders),
                    daemon=True,
                ).start()

        if col_stop.button("⏹️ Arrêter la détection"):
            if st.session_state.webcam_active and st.session_state.processor:
                st.session_state.processor.stop_processing = True
                st.session_state.webcam_active = False
                time.sleep(0.5)
                video_placeholder.empty()
            else:
                st.warning("⚠️ Aucune détection en cours !")


if __name__ == "__main__":
    main()