File size: 5,889 Bytes
9fbe262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
from ultralytics import YOLO
from deep_sort_realtime.deepsort_tracker import DeepSort
import os
from datetime import datetime

model = YOLO("C:\\Users\\PC\\Desktop\\GazCounter\\models\\best (10).pt")

tracker = DeepSort(
    max_age=30,         
    n_init=3,            
    max_iou_distance=0.7,
    embedder="mobilenet",
    half=True,
    embedder_gpu=True,
)

video_path = "C:\\Users\\PC\\Desktop\\GazCounter\\data\\WhatsApp Video 2025-10-02 at 11.29.11.mp4"
cap = cv2.VideoCapture(video_path)

# ── OUTPUT SETUP ────────────────────────────────────────
width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps    = int(cap.get(cv2.CAP_PROP_FPS))

output_dir   = "C:\\Users\\PC\\Desktop\\GazCounter\\results"
os.makedirs(output_dir, exist_ok=True)
timestamp    = datetime.now().strftime("%Y%m%d_%H%M%S")
output_video = os.path.join(output_dir, f"result_{timestamp}.mp4")
output_txt   = os.path.join(output_dir, f"result_{timestamp}.txt")

writer = cv2.VideoWriter(
    output_video,
    cv2.VideoWriter_fourcc(*"mp4v"),
    fps, (width, height)
)

# ── STATE ───────────────────────────────────────────────
count_per_brand = {}
detected_ids    = set()
id_to_box       = {}
id_to_class     = {}

IOU_THRESHOLD = 0.5

def iou(boxA, boxB):
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])
    inter = max(0, xB - xA) * max(0, yB - yA)
    if inter == 0:
        return 0.0
    areaA = (boxA[2]-boxA[0]) * (boxA[3]-boxA[1])
    areaB = (boxB[2]-boxB[0]) * (boxB[3]-boxB[1])
    return inter / float(areaA + areaB - inter)

# ── MAIN LOOP ───────────────────────────────────────────
while True:
    ret, frame = cap.read()
    if not ret:
        break

    # ── Step 1: YOLO detect (no tracking) ───────────────
    results = model.predict(frame, conf=0.4, verbose=False)

    detections = []
    cls_map    = {}  # index β†’ class name

    for r in results:
        if r.boxes is None:
            continue
        for i, box in enumerate(r.boxes):
            x1, y1, x2, y2 = map(int, box.xyxy[0].cpu().numpy())
            conf     = float(box.conf[0])
            cls_id   = int(box.cls[0])
            cls_name = r.names[cls_id]

            # DeepSORT format: ([x1,y1,w,h], conf, class_name)
            w = x2 - x1
            h = y2 - y1
            detections.append(([x1, y1, w, h], conf, cls_name))

    # ── Step 2: DeepSORT update ──────────────────────────
    tracks = tracker.update_tracks(detections, frame=frame)

    for track in tracks:
        if not track.is_confirmed():
            continue

        track_id = track.track_id
        cls_name = track.get_det_class()
        ltrb     = track.to_ltrb()  # (x1, y1, x2, y2)
        x1, y1, x2, y2 = map(int, ltrb)
        curr_box = (x1, y1, x2, y2)

        if cls_name is None:
            continue

        # Draw
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.putText(
            frame,
            f"{cls_name} #{track_id}",
            (x1, y1 - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2
        )

        # ── Unique count with IoU dedup ──────────────────
        if track_id not in detected_ids:
            is_duplicate = False
            for known_id, known_box in id_to_box.items():
                if known_id not in detected_ids:
                    continue
                if iou(curr_box, known_box) > IOU_THRESHOLD:
                    is_duplicate = True
                    break

            if not is_duplicate:
                detected_ids.add(track_id)
                count_per_brand[cls_name] = count_per_brand.get(cls_name, 0) + 1

        id_to_box[track_id]   = curr_box
        id_to_class[track_id] = cls_name

    # ── Overlay panel ────────────────────────────────────
    overlay = frame.copy()
    panel_h = 30 + len(count_per_brand) * 28
    cv2.rectangle(overlay, (5, 5), (300, panel_h), (0, 0, 0), -1)
    cv2.addWeighted(overlay, 0.4, frame, 0.6, 0, frame)

    for i, (brand, count) in enumerate(sorted(count_per_brand.items())):
        cv2.putText(
            frame, f"{brand}: {count}",
            (10, 28 + i * 28),
            cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 255, 255), 2
        )

    total = sum(count_per_brand.values())
    cv2.putText(
        frame, f"TOTAL: {total} bouteilles",
        (10, frame.shape[0] - 10),
        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2
    )

    writer.write(frame)
    cv2.imshow("GazCounter", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
writer.release()
cv2.destroyAllWindows()

# ── Save report ──────────────────────────────────────────
with open(output_txt, "w") as f:
    f.write(f"GazCounter Results - {timestamp}\n")
    f.write("=" * 40 + "\n")
    for brand, count in sorted(count_per_brand.items()):
        f.write(f"  {brand}: {count} bouteille(s)\n")
    f.write("=" * 40 + "\n")
    f.write(f"  TOTAL: {sum(count_per_brand.values())} bouteilles\n")

print("\n" + "=" * 40)
print(f"Video  β†’ {output_video}")
print(f"Report β†’ {output_txt}")
print("=" * 40)
for brand, count in sorted(count_per_brand.items()):
    print(f"  {brand}: {count} bouteille(s)")
print(f"  TOTAL: {sum(count_per_brand.values())} bouteilles")