wjbmattingly commited on
Commit
e7a4a9a
·
verified ·
1 Parent(s): d39c80d

Create visualize.py

Browse files
Files changed (1) hide show
  1. visualize.py +84 -0
visualize.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ from PIL import Image
4
+
5
+ def colormap(N=256, normalized=False):
6
+ """
7
+ Generate the color map.
8
+ Args:
9
+ N (int): Number of labels (default is 256).
10
+ normalized (bool): If True, return colors normalized to [0, 1]. Otherwise, return [0, 255].
11
+ Returns:
12
+ np.ndarray: Color map array of shape (N, 3).
13
+ """
14
+ def bitget(byteval, idx):
15
+ """
16
+ Get the bit value at the specified index.
17
+ Args:
18
+ byteval (int): The byte value.
19
+ idx (int): The index of the bit.
20
+ Returns:
21
+ int: The bit value (0 or 1).
22
+ """
23
+ return ((byteval & (1 << idx)) != 0)
24
+
25
+ cmap = np.zeros((N, 3), dtype=np.uint8)
26
+ for i in range(N):
27
+ r = g = b = 0
28
+ c = i
29
+ for j in range(8):
30
+ r = r | (bitget(c, 0) << (7 - j))
31
+ g = g | (bitget(c, 1) << (7 - j))
32
+ b = b | (bitget(c, 2) << (7 - j))
33
+ c = c >> 3
34
+ cmap[i] = np.array([r, g, b])
35
+
36
+ if normalized:
37
+ cmap = cmap.astype(np.float32) / 255.0
38
+
39
+ return cmap
40
+
41
+ def visualize_bbox(image_path, bboxes, classes, scores, id_to_names, alpha=0.3):
42
+ """
43
+ Visualize layout detection results on an image.
44
+ Args:
45
+ image_path (str): Path to the input image.
46
+ bboxes (list): List of bounding boxes, each represented as [x_min, y_min, x_max, y_max].
47
+ classes (list): List of class IDs corresponding to the bounding boxes.
48
+ id_to_names (dict): Dictionary mapping class IDs to class names.
49
+ alpha (float): Transparency factor for the filled color (default is 0.3).
50
+ Returns:
51
+ np.ndarray: Image with visualized layout detection results.
52
+ """
53
+ # Check if image_path is a PIL.Image.Image object
54
+ if isinstance(image_path, Image.Image) or isinstance(image_path, np.ndarray):
55
+ image = np.array(image_path)
56
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert RGB to BGR for OpenCV
57
+ else:
58
+ image = cv2.imread(image_path)
59
+
60
+ overlay = image.copy()
61
+
62
+ cmap = colormap(N=len(id_to_names), normalized=False)
63
+
64
+ # Iterate over each bounding box
65
+ for i, bbox in enumerate(bboxes):
66
+ x_min, y_min, x_max, y_max = map(int, bbox)
67
+ class_id = int(classes[i])
68
+ class_name = id_to_names[class_id]
69
+
70
+ text = class_name + f":{scores[i]:.3f}"
71
+
72
+ color = tuple(int(c) for c in cmap[class_id])
73
+ cv2.rectangle(overlay, (x_min, y_min), (x_max, y_max), color, -1)
74
+ cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 2)
75
+
76
+ # Add the class name with a background rectangle
77
+ (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.9, 2)
78
+ cv2.rectangle(image, (x_min, y_min - text_height - baseline), (x_min + text_width, y_min), color, -1)
79
+ cv2.putText(image, text, (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
80
+
81
+ # Blend the overlay with the original image
82
+ cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
83
+
84
+ return image