File size: 3,156 Bytes
4d18315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import gradio as gr
import numpy as np
import tempfile
import os
from ultralytics import YOLO
from deep_sort_realtime.deepsort_tracker import DeepSort
from collections import defaultdict

# Dictionnaire pour compter les objets détectés
class_counts = defaultdict(set)

# Charger modèle YOLOv8
model = YOLO("best.pt")  # Assure-toi que ce fichier est bien dans le même dossier

# Initialiser DeepSORT
tracker = DeepSort(max_age=30)

# 📸 Détection image
def detect_on_image(image):
    results = model(image)[0]
    for box in results.boxes:
        cls_id = int(box.cls[0])
        conf = float(box.conf[0])
        x1, y1, x2, y2 = map(int, box.xyxy[0])
        if conf > 0.4:
            label = f"{model.names[cls_id]} {conf:.2f}"
            cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
            cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
    return image

# 🎥 Détection vidéo
def detect_and_track_video(video_path):
    if not os.path.exists(video_path):
        return None

    cap = cv2.VideoCapture(video_path)
    width = int(cap.get(3))
    height = int(cap.get(4))
    fps = cap.get(cv2.CAP_PROP_FPS)
    temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
    out = cv2.VideoWriter(temp_output.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
    class_counts.clear()

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

        results = model(frame)[0]
        detections = []

        for box in results.boxes:
            cls_id = int(box.cls[0])
            conf = float(box.conf[0])
            x1, y1, x2, y2 = map(int, box.xyxy[0])
            if conf > 0.4:
                detections.append(([x1, y1, x2 - x1, y2 - y1], conf, model.names[cls_id]))

        tracks = tracker.update_tracks(detections, frame=frame)

        for track in tracks:
            if not track.is_confirmed():
                continue
            track_id = track.track_id
            l, t, r, b = map(int, track.to_ltrb())
            label = track.get_det_class()
            cv2.rectangle(frame, (l, t), (r, b), (0, 255, 0), 2)
            cv2.putText(frame, f'{label} ID {track_id}', (l, t - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
            class_counts[label].add(track_id)

        out.write(frame)

    cap.release()
    out.release()

    return temp_output.name

# Interfaces Gradio
image_interface = gr.Interface(
    fn=detect_on_image,
    inputs=gr.Image(type="numpy", label="Image de surveillance"),
    outputs=gr.Image(type="numpy", label="Image annotée"),
    title="📸 Détection sur Image",
    description="Détection de bagages et objets avec YOLOv8."
)

video_interface = gr.Interface(
    fn=detect_and_track_video,
    inputs=gr.Video(label="Vidéo de surveillance"),
    outputs=gr.Video(label="Vidéo annotée avec suivi"),
    title="🎥 Suivi sur Vidéo",
    description="Suivi multi-objets avec DeepSORT + YOLOv8."
)

# Interface finale
gr.TabbedInterface(
    [image_interface, video_interface],
    tab_names=["📷 Image", "🎥 Vidéo"]
).launch()