I’m new to using Jupiter Lab and to Python - trying to find a way to perform the following :
I want to display an image (a page from a document) - and draw rectangles on top of it (bounding boxes of different texts). I get the list of bounding boxes from an external server using rest api.
I managed to do that using PIL and request easily.
But now - I want to be able to “select” specific rectangles by clicking on the image - and get the boxes of those rectangles (so the user can select the texts he wants).
What will be the easiest way to accomplish this ?
Any reference to any example will help me get started.
Maybe you’ll find the jupyter_bbox_widget handy for this task. I made it initially for annotating images with bounding boxes but I think it can cover this use case as well.
You create the widget with
from jupyter_bbox_widget import BBoxWidget
widget = BBoxWidget(
image = 'url or base64-encoded data',
bboxes = [
# a list of boxes with pixel coordinates
{'x': 100, 'y': 100, 'width': 500, 'height':200, 'label':''},
],
view_only = True, # to prevent users from moving/resizing boxes
)
Now this part isn’t really documented … But there is a selected_index trait you can observe to know which box the user clicked on:
def on_bbox_selected(change):
selected_index = change['new']
# a value of -1 means the user clicked outside of any bboxes
if selected_index == -1:
selected_bbox = None
else:
selected_bbox = widget.bboxes[selected_index]
# do whatever with the bbox
widget.observe(on_bbox_selected, names=['selected_index'])
And that’s about all the boilerplate code you need =). There’s a demo notebook on Binder where you could try this out.
Thanks for pointing me to your project - works great.
Since I’m really new to this environment I have 2 questions:
1 - how can a resize the image displayed ?
2 - in the on_box_selected - I would like to know what is the active class (so I can add the selected box with the label to a new array)
2 - what I actually wanted to get is the class selected at the bottom of the widget - cause I want the user to “pick” the class - and then click the appropriate Box to match the class
Oh, this hasn’t come up before. I’m afraid the selected class label is not currently exposed to the python side.
I could add that in the next version =). You’d access it as widget.label.