overlay.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Draw green GT / red prediction / target + decoy boxes."""
  2. from __future__ import annotations
  3. from typing import List, Optional, Sequence, Tuple
  4. import cv2
  5. import numpy as np
  6. GREEN = (0, 220, 0)
  7. RED = (0, 0, 255)
  8. YELLOW = (0, 220, 255)
  9. BOX = (0, 165, 255) # orange: main target
  10. DECOY = (255, 200, 0) # cyan-like: distractors
  11. def draw_reticle(
  12. frame: np.ndarray,
  13. center: Optional[Tuple[float, float]],
  14. color: Tuple[int, int, int],
  15. radius: int = 18,
  16. thickness: int = 2,
  17. ) -> None:
  18. if center is None:
  19. return
  20. x, y = int(round(center[0])), int(round(center[1]))
  21. cv2.circle(frame, (x, y), radius, color, thickness, lineType=cv2.LINE_AA)
  22. cv2.drawMarker(
  23. frame,
  24. (x, y),
  25. color,
  26. markerType=cv2.MARKER_CROSS,
  27. markerSize=radius,
  28. thickness=thickness,
  29. line_type=cv2.LINE_AA,
  30. )
  31. def draw_bbox(
  32. frame: np.ndarray,
  33. xyxy: Optional[Tuple[float, float, float, float]],
  34. color: Tuple[int, int, int] = BOX,
  35. thickness: int = 2,
  36. label: Optional[str] = None,
  37. ) -> None:
  38. if xyxy is None:
  39. return
  40. x1, y1, x2, y2 = [int(round(v)) for v in xyxy]
  41. cv2.rectangle(frame, (x1, y1), (x2, y2), color, thickness, lineType=cv2.LINE_AA)
  42. if label:
  43. cv2.putText(
  44. frame,
  45. label,
  46. (x1, max(20, y1 - 8)),
  47. cv2.FONT_HERSHEY_SIMPLEX,
  48. 0.55,
  49. color,
  50. 2,
  51. lineType=cv2.LINE_AA,
  52. )
  53. def draw_pair(
  54. frame: np.ndarray,
  55. green_xy: Optional[Tuple[float, float]],
  56. red_xy: Optional[Tuple[float, float]],
  57. bbox_xyxy: Optional[Tuple[float, float, float, float]] = None,
  58. conf: float = 0.0,
  59. draw_error_line: bool = True,
  60. draw_gt: bool = True,
  61. decoys: Optional[Sequence[Tuple[int, Tuple[float, float, float, float]]]] = None,
  62. ) -> np.ndarray:
  63. """
  64. Cyan boxes = decoys (干扰项, display only).
  65. Orange box + red reticle = tracked TARGET.
  66. Green reticle = recorded GT only.
  67. """
  68. out = frame.copy()
  69. if decoys:
  70. for tid, box in decoys:
  71. draw_bbox(out, box, color=DECOY, thickness=2, label=f"id:{tid}")
  72. if bbox_xyxy is not None:
  73. draw_bbox(out, bbox_xyxy, color=BOX, thickness=3, label=f"REAL {conf:.2f}")
  74. if draw_gt:
  75. draw_reticle(out, green_xy, GREEN)
  76. draw_reticle(out, red_xy, RED)
  77. if draw_error_line and green_xy is not None and red_xy is not None:
  78. p1 = (int(round(green_xy[0])), int(round(green_xy[1])))
  79. p2 = (int(round(red_xy[0])), int(round(red_xy[1])))
  80. cv2.line(out, p1, p2, YELLOW, 1, lineType=cv2.LINE_AA)
  81. return out