enpaiva commited on
Commit
afd0c7e
·
verified ·
1 Parent(s): fde44f7

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +29 -37
visualization.py CHANGED
@@ -43,48 +43,40 @@ def colormap(N=256, normalized=False):
43
  return cmap
44
 
45
  def visualize_bbox(image_path, bboxes, classes, scores, id_to_names, alpha=0.3):
46
- """
47
- Visualize layout detection results on an image.
48
-
49
- Args:
50
- image_path (str): Path to the input image.
51
- bboxes (list): List of bounding boxes, each represented as [x_min, y_min, x_max, y_max].
52
- classes (list): List of class IDs corresponding to the bounding boxes.
53
- id_to_names (dict): Dictionary mapping class IDs to class names.
54
- alpha (float): Transparency factor for the filled color (default is 0.3).
55
-
56
- Returns:
57
- np.ndarray: Image with visualized layout detection results.
58
- """
59
- # Check if image_path is a PIL.Image.Image object
60
  if isinstance(image_path, Image.Image) or isinstance(image_path, np.ndarray):
61
  image = np.array(image_path)
62
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert RGB to BGR for OpenCV
63
  else:
64
  image = cv2.imread(image_path)
 
 
65
 
66
  overlay = image.copy()
67
-
68
  cmap = colormap(N=len(id_to_names), normalized=False)
69
-
70
- # Iterate over each bounding box
71
- for i, bbox in enumerate(bboxes):
72
- x_min, y_min, x_max, y_max = map(int, bbox)
73
- class_id = int(classes[i])
74
- class_name = id_to_names[class_id]
75
-
76
- text = class_name + f":{scores[i]:.3f}"
77
-
78
- color = tuple(int(c) for c in cmap[class_id])
79
- cv2.rectangle(overlay, (x_min, y_min), (x_max, y_max), color, -1)
80
- cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 2)
81
-
82
- # Add the class name with a background rectangle
83
- (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.9, 2)
84
- cv2.rectangle(image, (x_min, y_min - text_height - baseline), (x_min + text_width, y_min), color, -1)
85
- cv2.putText(image, text, (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
86
-
87
- # Blend the overlay with the original image
 
 
 
 
 
 
88
  cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
89
-
90
- return image
 
43
  return cmap
44
 
45
  def visualize_bbox(image_path, bboxes, classes, scores, id_to_names, alpha=0.3):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  if isinstance(image_path, Image.Image) or isinstance(image_path, np.ndarray):
47
  image = np.array(image_path)
48
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
49
  else:
50
  image = cv2.imread(image_path)
51
+ if image is None:
52
+ raise ValueError(f"Could not load image from path: {image_path}")
53
 
54
  overlay = image.copy()
 
55
  cmap = colormap(N=len(id_to_names), normalized=False)
56
+
57
+ if len(bboxes) == 0:
58
+ print("No bounding boxes to display.")
59
+ return image # Return original image if nothing detected
60
+
61
+ for i in range(len(bboxes)):
62
+ try:
63
+ x_min, y_min, x_max, y_max = map(int, bboxes[i])
64
+ class_id = int(classes[i])
65
+ class_name = id_to_names.get(class_id, f"unknown_{class_id}")
66
+ score = scores[i]
67
+
68
+ text = f"{class_name}:{score:.3f}"
69
+ color = tuple(int(c) for c in cmap[class_id % len(cmap)])
70
+
71
+ cv2.rectangle(overlay, (x_min, y_min), (x_max, y_max), color, -1)
72
+ cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 2)
73
+
74
+ (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.9, 2)
75
+ cv2.rectangle(image, (x_min, y_min - text_height - baseline), (x_min + text_width, y_min), color, -1)
76
+ cv2.putText(image, text, (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
77
+
78
+ except Exception as e:
79
+ print(f"Skipping box {i} due to error: {e}")
80
+
81
  cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)
82
+ return image