Spaces:
Build error
Build error
File size: 3,929 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 | import cv2
from ultralytics import YOLO
import os
from datetime import datetime
model = YOLO("C:\\Users\\PC\\Desktop\\GazCounter\\best (10).pt")
video_path = "C:\\Users\\PC\\Desktop\\GazCounter\\WhatsApp Video 2025-10-02 at 11.29.11.mp4"
cap = cv2.VideoCapture(video_path)
# ββ OUTPUT VIDEO 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)
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
count_per_brand = {}
detected_ids = set()
while True:
ret, frame = cap.read()
if not ret:
break
results = model.track(frame, persist=True, conf=0.4)
for r in results:
if r.boxes is None:
continue
for box in r.boxes:
if box.id is None:
continue
track_id = int(box.id[0])
cls_id = int(box.cls[0])
cls_name = r.names[cls_id]
conf = float(box.conf[0])
x1, y1, x2, y2 = map(int, box.xyxy[0].cpu().numpy())
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(
frame,
f"{cls_name} #{track_id} ({conf:.0%})",
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2
)
if track_id not in detected_ids:
detected_ids.add(track_id)
count_per_brand[cls_name] = count_per_brand.get(cls_name, 0) + 1
# ββ 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
)
# ββ SAVE FRAME TO OUTPUT VIDEO ββ
writer.write(frame)
cv2.imshow("GazCounter", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
writer.release()
cv2.destroyAllWindows()
# ββ SAVE RESULTS TO TXT FILE ββββββββββββββββββββββββββββ
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 RESULTS βββββββββββββββββββββββββββββββββββββββ
print("\n" + "=" * 40)
print(f"β
Video saved β {output_video}")
print(f"β
Report saved β {output_txt}")
print("=" * 40)
print("\nResults:")
for brand, count in sorted(count_per_brand.items()):
print(f" {brand}: {count} bouteille(s)")
print(f" TOTAL: {sum(count_per_brand.values())} bouteilles") |