GazCounter / src /test.py
Moamineelhilali's picture
Fix HF metadata and ignore rules
9fbe262
Raw
History Blame Contribute Delete
3.93 kB
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")