Update display_BB.py
Browse files- display_BB.py +24 -21
display_BB.py
CHANGED
|
@@ -1,24 +1,27 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
for box, label in zip(boxes, labels):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
# if save_path:
|
| 15 |
-
# image.save(save_path)
|
| 16 |
-
|
| 17 |
-
return image
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|