Spaces:
Sleeping

VisionGuard-AI / core /visualize.py
Abhyuday-06's picture
feat: Bengaluru-ready detection pipeline with retrained models
29088fd
Raw History Blame Contribute Delete
5.2 kB
"""Annotation drawing: scene overlays, vehicles, riders, violations, HUD."""
from __future__ import annotations
from typing import Any, Iterable, Sequence
import cv2
import numpy as np
from core.geometry import BBox
from core.rules import HELMET, PARKING, RED_LIGHT, SEATBELT, STOP_LINE, TRIPLE, WRONG_SIDE
# BGR colours
VIOLATION_COLORS = {
HELMET: (40, 40, 230),
TRIPLE: (0, 140, 255),
SEATBELT: (220, 90, 30),
WRONG_SIDE: (200, 40, 200),
STOP_LINE: (0, 215, 255),
RED_LIGHT: (0, 0, 180),
PARKING: (160, 60, 120),
}
SIGNAL_COLORS = {"RED": (40, 40, 230), "YELLOW": (0, 200, 255), "GREEN": (60, 180, 60), "UNKNOWN": (150, 150, 150)}
NEUTRAL = (90, 200, 90)
FONT = cv2.FONT_HERSHEY_SIMPLEX
def _scale(frame: np.ndarray) -> float:
return max(0.6, min(2.2, frame.shape[1] / 1280.0))
def draw_label(img: np.ndarray, text: str, org: tuple[int, int], color: tuple[int, int, int],
scale: float = 0.5, thickness: int = 1, text_color=(255, 255, 255)) -> None:
(tw, th), base = cv2.getTextSize(text, FONT, scale, thickness)
x, y = int(org[0]), int(max(org[1], th + 4))
x = max(0, min(x, img.shape[1] - tw - 6))
cv2.rectangle(img, (x, y - th - 4), (x + tw + 6, y + base), color, -1)
cv2.putText(img, text, (x + 3, y - 2), FONT, scale, text_color, thickness, cv2.LINE_AA)
def _alpha_poly(img: np.ndarray, poly: np.ndarray, color, alpha: float = 0.18) -> None:
overlay = img.copy()
cv2.fillPoly(overlay, [poly.astype(np.int32)], color)
cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0, dst=img)
cv2.polylines(img, [poly.astype(np.int32)], True, color, 2, cv2.LINE_AA)
def draw_scene(img: np.ndarray, lanes: Sequence[Any], stop_line: Any, parking_zones: Sequence[np.ndarray],
signal: str, light_box: BBox | None = None) -> None:
s = _scale(img)
for lane in lanes:
_alpha_poly(img, lane.polygon, (200, 160, 60), 0.10)
c = lane.polygon.mean(axis=0)
L = 60 * s
p1 = (int(c[0] - lane.direction[0] * L / 2), int(c[1] - lane.direction[1] * L / 2))
p2 = (int(c[0] + lane.direction[0] * L / 2), int(c[1] + lane.direction[1] * L / 2))
cv2.arrowedLine(img, p1, p2, (255, 220, 120), max(2, int(3 * s)), cv2.LINE_AA, tipLength=0.35)
draw_label(img, lane.name, (int(c[0]) - 20, int(c[1]) - int(18 * s)), (160, 120, 40), 0.45 * s)
for zone in parking_zones:
_alpha_poly(img, zone, VIOLATION_COLORS[PARKING], 0.15)
x, y = zone.min(axis=0)
draw_label(img, "NO PARKING", (int(x) + 4, int(y) + int(20 * s)), VIOLATION_COLORS[PARKING], 0.45 * s)
if stop_line is not None:
color = SIGNAL_COLORS.get(signal, SIGNAL_COLORS["UNKNOWN"])
a = tuple(int(v) for v in stop_line.a)
b = tuple(int(v) for v in stop_line.b)
cv2.line(img, a, b, color, max(3, int(4 * s)), cv2.LINE_AA)
draw_label(img, f"STOP LINE ({signal})", (min(a[0], b[0]), min(a[1], b[1]) - 6), color, 0.45 * s)
if light_box is not None:
cv2.rectangle(img, light_box[:2], light_box[2:], SIGNAL_COLORS.get(signal, (150, 150, 150)), 2)
def draw_frame(
frame: np.ndarray,
detections: Iterable[Any],
violations_by_track: dict[int, list[str]],
heads_by_track: dict[int, list[Any]],
signal: str,
extra_labels: dict[int, str] | None = None,
plates: dict[int, str] | None = None,
show_all: bool = True,
) -> np.ndarray:
img = frame.copy()
s = _scale(img)
extra_labels = extra_labels or {}
plates = plates or {}
for det in detections:
v = violations_by_track.get(det.track_id, [])
if not v and not show_all:
continue
x1, y1, x2, y2 = det.bbox
if v:
color = VIOLATION_COLORS.get(v[0], (0, 0, 255))
cv2.rectangle(img, (x1, y1), (x2, y2), color, max(2, int(3 * s)))
for i, name in enumerate(v):
draw_label(img, name.upper(), (x1, y1 - 6 - i * int(20 * s)), VIOLATION_COLORS.get(name, color),
0.5 * s, 1 if s < 1.2 else 2)
else:
cv2.rectangle(img, (x1, y1), (x2, y2), NEUTRAL, 1)
tag = f"#{det.track_id} " if det.track_id >= 0 else ""
sub = f"{tag}{det.label}"
if det.track_id in plates:
sub += f" | {plates[det.track_id]}"
if det.track_id in extra_labels:
sub += f" | {extra_labels[det.track_id]}"
if v or show_all:
draw_label(img, sub, (x1, y2 + int(16 * s)), (50, 50, 50), 0.4 * s)
for head in heads_by_track.get(det.track_id, []):
hc = (60, 200, 60) if head.helmet else (40, 40, 230)
cv2.rectangle(img, head.bbox[:2], head.bbox[2:], hc, max(1, int(2 * s)))
# HUD
color = SIGNAL_COLORS.get(signal, SIGNAL_COLORS["UNKNOWN"])
draw_label(img, f"SIGNAL: {signal}", (10, int(28 * s)), color, 0.6 * s, 2)
return img
def highlight(frame: np.ndarray, bbox: BBox, text: str, color) -> np.ndarray:
img = frame.copy()
s = _scale(img)
x1, y1, x2, y2 = bbox
cv2.rectangle(img, (x1, y1), (x2, y2), color, max(3, int(4 * s)))
draw_label(img, text, (x1, y1 - 8), color, 0.6 * s, 2)
return img