使用Gradio绘制边界框

huangapple go评论138阅读模式
英文:

Drawing Bounding Boxes with Gradio

问题

我有一个使用YOLO分类的图像,其中包含边界框,但如何在Gradio Webapp上的输出图像上绘制相同的边界框,是否有办法让用户在Gradio中调整边界框?

gr.outputs.BoundingBoxgr.outputs.ImageBoundingBox 在Gradio的新版本中不起作用。

英文:

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.

答案1

得分: 0

目前,在Gradio中无法直接绘制边界框。您必须在您的Python代码中自行渲染它们到图像上。这样做的缺点是用户无法在UI上调整边界框。

请注意,有一个最近被拒绝的Pull Request 1,还有一种可能的解决方案 2,可以通过裁剪后的图像获取边界框。

英文:

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.

答案2

得分: 0

您可以使用matplotlib绘制边界框。以下是示例代码:

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()

请注意,这是示例代码,用于使用matplotlib绘制边界框。您需要确保在代码中正确导入相关库和设置图像、描述符、矩形等变量的值。

英文:

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 &lt; 0.6:
            found = True

            text = ax.text(rects[i][0][0], rects[i][0][1], name,
                    color=&#39;b&#39;, fontsize=40, fontweight=&#39;bold&#39;)
            text.set_path_effects([path_effects.Stroke(linewidth=10, foreground=&#39;white&#39;), 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=&#39;w&#39;, facecolor=&#39;none&#39;)
            ax.add_patch(rect)

            break
    
    if not found:
        ax.text(rects[i][0][0], rects[i][0][1], &#39;unknown&#39;,
                color=&#39;r&#39;, fontsize=20, fontweight=&#39;bold&#39;)
        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=&#39;r&#39;, facecolor=&#39;none&#39;)
        ax.add_patch(rect)

plt.axis(&#39;off&#39;)
plt.savefig(&#39;result/output.png&#39;)
plt.show()```

</details>



huangapple
  • 本文由 发表于 2023年4月4日 14:50:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定