jkushwaha commited on
Commit
5bd0afd
·
verified ·
1 Parent(s): 7d58a6f

Update display_BB.py

Browse files
Files changed (1) hide show
  1. display_BB.py +24 -21
display_BB.py CHANGED
@@ -1,24 +1,27 @@
1
- from PIL import Image, ImageDraw
 
 
 
2
 
3
- def draw_bounding_boxes(image_path, boxes, labels):
4
- image = Image.open(image_path)
5
- draw = ImageDraw.Draw(image)
6
-
 
 
 
 
 
 
 
 
7
  for box, label in zip(boxes, labels):
8
- x_min, y_min, x_max, y_max = box
9
- draw.rectangle([x_min, y_min, x_max, y_max], outline="red", width=2)
10
- draw.text((x_min, y_min), label, fill="red")
11
- # cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, thickness)
12
- # cv2.putText(image, label, (xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness)
13
-
14
- # if save_path:
15
- # image.save(save_path)
16
-
17
- return image
18
 
19
- # Example usage:
20
- image_path = "example.jpg"
21
- boxes = [(100, 100, 200, 200), (300, 300, 400, 400)] # Example bounding box coordinates
22
- labels = ["Object 1", "Object 2"] # Example labels
23
- output_image = draw_bounding_boxes(image_path, boxes, labels, save_path="output.jpg")
24
- output_image.show()
 
1
+ import matplotlib.pyplot as plt
2
+ from skimage import io
3
+ from skimage.draw import rectangle_perimeter
4
+ from skimage.color import label2rgb
5
 
6
+ def display_bounding_boxes(image_path, boxes, labels):
7
+ # Load the image
8
+ image = io.imread(image_path)
9
+
10
+ # Convert to RGB (if grayscale)
11
+ if image.ndim == 2:
12
+ image = label2rgb(image)
13
+
14
+ # Plot the image
15
+ plt.imshow(image)
16
+
17
+ # Add bounding boxes and labels
18
  for box, label in zip(boxes, labels):
19
+ ymin, xmin, ymax, xmax = box
20
+ rr, cc = rectangle_perimeter(start=(ymin, xmin), end=(ymax, xmax), shape=image.shape)
21
+ image[rr, cc] = (0, 1, 0) # Draw green bounding boxes
22
+
23
+ # Add label text
24
+ plt.text(xmin, ymin - 2, label, color='r')
 
 
 
 
25
 
26
+ plt.axis('off')
27
+ plt.show()