1

I have an YOLO Classified image with bounding boxes but how do i draw the same bounding boxes on my output image in Gradio Webapp and is there anyway for me to make the user adjust the bounding boxes in gradio?

The gr.outputs.BoundingBox and gr.outputs.ImageBoundingBox does not work in gradio newer versions.

Yannick Funk
  • 1,319
  • 10
  • 23
Tejas M A
  • 11
  • 2

2 Answers2

0

Currently, drawing Bounding Boxes directly in gradio is not possible. You have to render them yourself onto the image in your python code. The drawback of this is that the user can not adjust the boxes on the UI.

Note, there is a recent PR that has been declined. There also is a possible workaround by obtaining the bounding boxes through the cropped image.

Yannick Funk
  • 1,319
  • 10
  • 23
  • Thanks a lot. Would you know of any other libraries that I can use to draw bounding boxes on an Image that the user can adjust in the UI? – Tejas M A Apr 05 '23 at 04:24
  • No sorry, but I am not really into this field. So a little google research should help here. If you found my answer for gradio helpful, I would appreciate it if you accept it. – Yannick Funk Apr 05 '23 at 06:46
0

You can use matplotlib to draw bounding box. Here are sample codes

fig, ax = plt.subplots(1, figsize=(20, 20))
ax.imshow(img_rgb)

for i, desc in enumerate(descriptors):
    
    found = False
    for name, saved_desc in descs.items():
        dist = np.linalg.norm([desc] - saved_desc, axis=1)

        if dist < 0.6:
            found = True

            text = ax.text(rects[i][0][0], rects[i][0][1], name,
                    color='b', fontsize=40, fontweight='bold')
            text.set_path_effects([path_effects.Stroke(linewidth=10, foreground='white'), path_effects.Normal()])
            rect = patches.Rectangle(rects[i][0],
                                 rects[i][1][1] - rects[i][0][1],
                                 rects[i][1][0] - rects[i][0][0],
                                 linewidth=2, edgecolor='w', facecolor='none')
            ax.add_patch(rect)

            break
    
    if not found:
        ax.text(rects[i][0][0], rects[i][0][1], 'unknown',
                color='r', fontsize=20, fontweight='bold')
        rect = patches.Rectangle(rects[i][0],
                             rects[i][1][1] - rects[i][0][1],
                             rects[i][1][0] - rects[i][0][0],
                             linewidth=2, edgecolor='r', facecolor='none')
        ax.add_patch(rect)

plt.axis('off')
plt.savefig('result/output.png')
plt.show()```
Luke
  • 11
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 08 '23 at 19:26