cledouxluma commited on
Commit
bb2c9b1
·
verified ·
1 Parent(s): 0b90d44

Upload utils/visualization.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. utils/visualization.py +62 -0
utils/visualization.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Visualization utilities for face detection results."""
2
+
3
+ import numpy as np
4
+ import cv2
5
+ from typing import List, Optional, Tuple
6
+
7
+
8
+ def draw_detections(image: np.ndarray, boxes: np.ndarray,
9
+ scores: Optional[np.ndarray] = None,
10
+ track_ids: Optional[np.ndarray] = None,
11
+ landmarks: Optional[np.ndarray] = None,
12
+ color: Tuple[int, int, int] = (0, 255, 0),
13
+ thickness: int = 2) -> np.ndarray:
14
+ """Draw face detections on image."""
15
+ vis = image.copy()
16
+
17
+ for i in range(len(boxes)):
18
+ x1, y1, x2, y2 = boxes[i].astype(int)
19
+ cv2.rectangle(vis, (x1, y1), (x2, y2), color, thickness)
20
+
21
+ label_parts = []
22
+ if track_ids is not None:
23
+ label_parts.append(f"ID:{track_ids[i]}")
24
+ if scores is not None:
25
+ label_parts.append(f"{scores[i]:.2f}")
26
+ label = ' '.join(label_parts)
27
+
28
+ if label:
29
+ (tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
30
+ cv2.rectangle(vis, (x1, y1 - th - 4), (x1 + tw, y1), color, -1)
31
+ cv2.putText(vis, label, (x1, y1 - 2),
32
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
33
+
34
+ if landmarks is not None and i < len(landmarks):
35
+ lmk = landmarks[i]
36
+ colors_lmk = [(0, 0, 255), (0, 255, 255), (0, 255, 0),
37
+ (255, 255, 0), (255, 0, 0)]
38
+ for j in range(5):
39
+ x, y = int(lmk[j*2]), int(lmk[j*2+1])
40
+ if x > 0 and y > 0:
41
+ cv2.circle(vis, (x, y), 3, colors_lmk[j], -1)
42
+
43
+ return vis
44
+
45
+
46
+ def create_comparison_grid(images: List[np.ndarray], titles: List[str],
47
+ cols: int = 3, cell_size: Tuple[int, int] = (400, 400)
48
+ ) -> np.ndarray:
49
+ """Create a grid of images with titles for comparison."""
50
+ rows = (len(images) + cols - 1) // cols
51
+ grid = np.zeros((rows * cell_size[1], cols * cell_size[0], 3), dtype=np.uint8)
52
+
53
+ for idx, (img, title) in enumerate(zip(images, titles)):
54
+ r, c = idx // cols, idx % cols
55
+ resized = cv2.resize(img, cell_size)
56
+ y1, y2 = r * cell_size[1], (r + 1) * cell_size[1]
57
+ x1, x2 = c * cell_size[0], (c + 1) * cell_size[0]
58
+ grid[y1:y2, x1:x2] = resized
59
+ cv2.putText(grid, title, (x1 + 5, y1 + 20),
60
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
61
+
62
+ return grid