import cv2 import numpy as np import time from ultralytics import YOLO import torch from collections import defaultdict import pandas as pd from typing import Dict, List, Tuple # LABEL_MAP = { # 0: "auto", # 1: "bus", # 2: "car", # 3: "motorcycle", # 4: "mini-bus", # 5: "scooter", # 6: "truck", # } LABEL_MAP = {0: "auto", 1: "bus", 2: "car", 3: "electric-rickshaw", 4: "large-sized-truck",5:'medium-sized-truck',6:'motorbike',7:'small-sized-truck'} 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, ): """Draw `text` on `image` with a filled rectangle behind it.""" (text_width, text_height), baseline = cv2.getTextSize( text, font, font_scale, font_thickness ) x, y = position rect_y1 = y - text_height - padding - baseline // 2 rect_y2 = y + padding - baseline // 2 cv2.rectangle( image, (x, rect_y1), (x + text_width + 2 * padding, rect_y2), bg_color, -1, ) cv2.putText( image, text, (x + padding, y - baseline // 2), font, font_scale, text_color, font_thickness, cv2.LINE_AA, ) def get_color_for_class(cls_id: int): """Deterministic bright color for each class index.""" np.random.seed(cls_id + 37) return tuple(np.random.randint(100, 256, size=3).tolist()) def _inside(pt: Tuple[int, int], poly: np.ndarray) -> bool: """Point-in-polygon test using OpenCV (non‑zero if inside).""" return cv2.pointPolygonTest(poly, pt, False) >= 0 class YOLOVideoDetector: """ Detect objects on a video and count them **per region**. * `regions`: Dict[int, List[Tuple[int,int]]], mapping region id (0,1, …) to 4+ vertices in *pixel* coordinates (clockwise or anticlockwise). * For each frame, counts are stored in a DataFrame column named `