wandb / wandb

The AI developer platform. Use Weights & Biases to train and fine-tune models, and manage models from experimentation to production.
https://wandb.ai
MIT License
8.97k stars 662 forks source link

[CLI]: Logging bounding boxes only works with float dimensions, not int #3982

Open johko opened 2 years ago

johko commented 2 years ago

Describe the bug

When logging bounding boxes for an image and using int values as width and height, the library throws an Error: TypeError: Object of type int is not JSON serializable When casting the values to float it works without a problem.

frame = cv2.imread("haar_cascades/data/9_Press_Conference_Press_Conference_9_86.jpg")
frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faceRects = face_detector.detectMultiScale(
    gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30),
    flags=cv2.CASCADE_SCALE_IMAGE)

box_data = []

class_labels = {
    0: "face"
}

for (x,y,w,h) in faceRects:

    midX = int(x+w/2)
    midY = int(y+h/2) 
    box = {
                "position": {
                    "middle": [midX, midY],
                    "width": w,
                    "height": h
                },
                "domain" : "pixel",
                "class_id" : 0
            }
    box_data.append(box)

predictions = {"predictions": {
        "box_data": box_data,
        "class_labels": class_labels
    }
    }

img = wandb.Image(frame, boxes=predictions)

Here is the example in a colab: https://colab.research.google.com/drive/1dyrgGANhitXN8RND8nSDItDvX6OE4-FF?usp=sharing

TypeError                                 Traceback (most recent call last)
/home/johannes/Projects/blog/haar/haar_cascade.ipynb Cell 5' in <cell line: 38>()
     [31](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=30) predictions = {"predictions": {
     [32](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=31)         "box_data": box_data,
     [33](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=32)         "class_labels": class_labels
     [34](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=33)     }
     [35](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=34)     }
     [37](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=36) re_im =cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
---> [38](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=37) img = wandb.Image(frame, boxes=predictions)
     [40](vscode-notebook-cell:/home/johannes/Projects/blog/haar/haar_cascade.ipynb#ch0000004?line=39) wandb.log({"obama_pc": img})

File ~/.local/share/virtualenvs/haar-tXjf4l0j/lib/python3.9/site-packages/wandb/sdk/data_types/image.py:147, in Image.__init__(self, data_or_path, mode, caption, grouping, classes, boxes, masks)
    144 else:
    145     self._initialize_from_data(data_or_path, mode)
--> 147 self._set_initialization_meta(grouping, caption, classes, boxes, masks)

File ~/.local/share/virtualenvs/haar-tXjf4l0j/lib/python3.9/site-packages/wandb/sdk/data_types/image.py:175, in Image._set_initialization_meta(self, grouping, caption, classes, boxes, masks)
    172         boxes_final[key] = box_item
    173     elif isinstance(box_item, dict):
    174         # TODO: Consider injecting top-level classes if user-provided is empty
--> 175         boxes_final[key] = BoundingBoxes2D(box_item, key)
    176     total_classes.update(boxes_final[key]._class_labels)
    177 self._boxes = boxes_final
...
    178     """
--> 179     raise TypeError(f'Object of type {o.__class__.__name__} '
    180                     f'is not JSON serializable')

TypeError: Object of type int is not JSON serializable

Additional Files

No response

Environment

WandB version: 0.12.18

OS: Ubuntu 18.04

Python version: 3.9

Versions of relevant libraries: opencv-python==4.6.0.66

Additional Context

No response

lesliewandb commented 2 years ago

Hi @johko! Thank you for bringing this to our attention :) This is a known bug that we have and we have a ticket out currently to fix this. I'll increase the priority we have on it and let you know once this has been fixed

johko commented 2 years ago

Thank you @lesliewandb :) I looked for a similar issue but couldn't find it. Good to know that you are on it.