deb113 commited on
Commit
0f31432
·
verified ·
1 Parent(s): f416353

Create visualization.py

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