| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- """Draw green GT / red prediction / target + decoy boxes."""
- from __future__ import annotations
- from typing import List, Optional, Sequence, Tuple
- import cv2
- import numpy as np
- GREEN = (0, 220, 0)
- RED = (0, 0, 255)
- YELLOW = (0, 220, 255)
- BOX = (0, 165, 255) # orange: main target
- DECOY = (255, 200, 0) # cyan-like: distractors
- def draw_reticle(
- frame: np.ndarray,
- center: Optional[Tuple[float, float]],
- color: Tuple[int, int, int],
- radius: int = 18,
- thickness: int = 2,
- ) -> None:
- if center is None:
- return
- x, y = int(round(center[0])), int(round(center[1]))
- cv2.circle(frame, (x, y), radius, color, thickness, lineType=cv2.LINE_AA)
- cv2.drawMarker(
- frame,
- (x, y),
- color,
- markerType=cv2.MARKER_CROSS,
- markerSize=radius,
- thickness=thickness,
- line_type=cv2.LINE_AA,
- )
- def draw_bbox(
- frame: np.ndarray,
- xyxy: Optional[Tuple[float, float, float, float]],
- color: Tuple[int, int, int] = BOX,
- thickness: int = 2,
- label: Optional[str] = None,
- ) -> None:
- if xyxy is None:
- return
- x1, y1, x2, y2 = [int(round(v)) for v in xyxy]
- cv2.rectangle(frame, (x1, y1), (x2, y2), color, thickness, lineType=cv2.LINE_AA)
- if label:
- cv2.putText(
- frame,
- label,
- (x1, max(20, y1 - 8)),
- cv2.FONT_HERSHEY_SIMPLEX,
- 0.55,
- color,
- 2,
- lineType=cv2.LINE_AA,
- )
- def draw_pair(
- frame: np.ndarray,
- green_xy: Optional[Tuple[float, float]],
- red_xy: Optional[Tuple[float, float]],
- bbox_xyxy: Optional[Tuple[float, float, float, float]] = None,
- conf: float = 0.0,
- draw_error_line: bool = True,
- draw_gt: bool = True,
- decoys: Optional[Sequence[Tuple[int, Tuple[float, float, float, float]]]] = None,
- ) -> np.ndarray:
- """
- Cyan boxes = decoys (干扰项, display only).
- Orange box + red reticle = tracked TARGET.
- Green reticle = recorded GT only.
- """
- out = frame.copy()
- if decoys:
- for tid, box in decoys:
- draw_bbox(out, box, color=DECOY, thickness=2, label=f"id:{tid}")
- if bbox_xyxy is not None:
- draw_bbox(out, bbox_xyxy, color=BOX, thickness=3, label=f"REAL {conf:.2f}")
- if draw_gt:
- draw_reticle(out, green_xy, GREEN)
- draw_reticle(out, red_xy, RED)
- if draw_error_line and green_xy is not None and red_xy is not None:
- p1 = (int(round(green_xy[0])), int(round(green_xy[1])))
- p2 = (int(round(red_xy[0])), int(round(red_xy[1])))
- cv2.line(out, p1, p2, YELLOW, 1, lineType=cv2.LINE_AA)
- return out
|