Bounding Boxes are Off the Object

I am trying to visualize the bounding boxes I created in Nvidia Omniverse Code in Google Colab but my bounding boxes keep going off the object. Please how do I fix it?
Here is the code in Google Colab:

Here is the bounding box. Off the target:

I found the solution:
It seems like PIL was expecting my bounding box co-ordiantes is a particular order but Nvidia Omniverse Code was outputting in a different order.
So this is the change to the code that worked:

import json
import numpy as np
import cv2
from PIL import Image, ImageDraw
from IPython.display import display

Replace these paths with the actual paths to your files

img_path = ‘/content/drive/MyDrive/Synthetic Images Shop/Red_Trunk_Test/Files/replicator_test/rgb_0009.png’
json_path = ‘/content/drive/MyDrive/Synthetic Images Shop/Red_Trunk_Test/Files/replicator_test/bounding_box_2d_tight_labels_0009.json’
npy_path = ‘/content/drive/MyDrive/Synthetic Images Shop/Red_Trunk_Test/Files/replicator_test/bounding_box_2d_tight_0009.npy’

Load the image

img = cv2.imread(img_path)

Load bounding box coordinates from NumPy file

bounding_box_npy = np.load(npy_path)

Load class information from JSON file

with open(json_path, ‘r’) as f:
json_data = json.load(f)
class_info = json_data.get(“data”, {})
class_labels = class_info.get(“semanticId”, “”)

Print relevant information for debugging

print(“Bounding Box NPY:”, bounding_box_npy)
print(“Class Labels:”, class_labels)

Extract relevant coordinates from bounding_box_npy

if len(bounding_box_npy) == 1 and len(bounding_box_npy[0]) == 6:
semantic_id, x_min, y_min, x_max, y_max, occlusion_ratio = bounding_box_npy[0]

# Print the coordinates
print("Bounding Box Coordinates:", (x_min, y_min, x_max, y_max))

# Draw bounding box
pil_img_npy = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw_npy = ImageDraw.Draw(pil_img_npy)
draw_npy.rectangle([(x_min, y_min), (x_max, y_max)], outline='blue', width=2)

# Display the image with bounding box using NumPy coordinates
display(pil_img_npy)

else:
print(“Invalid bounding box format”)

I hope it helps!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.