File size: 1,555 Bytes
290257a
 
f22205b
 
 
290257a
 
343712f
 
5d3aab5
 
343712f
5d3aab5
 
290257a
5d3aab5
 
 
 
 
 
 
 
343712f
290257a
5d3aab5
 
 
290257a
dbe0801
290257a
 
afa8f00
290257a
f22205b
 
 
290257a
 
f22205b
290257a
 
 
 
343712f
290257a
 
 
 
f22205b
290257a
 
f22205b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import numpy as np
import supervision as sv
from gradio.components import Image, Radio, Textbox


def plot_bounding_boxes(image, boxes, box_type):
    if not boxes:
        return image
        
    bboxes = []
    
    for row in boxes.split("\n"):
        x0, y0, x1, y1 = [int(float(i.split(".")[0])) for i in boxes.split(",")]

        if box_type == "xywh":
            x0 = x0 * image.size[0]
            y0 = y0 * image.size[1]
            x1 = x0 + (x1 * image.size[0])
            y1 = y0 + (y1 * image.size[1])

        bbox = [x0, y0, x1, y1]
        bboxes.append(bbox)

    detections = sv.Detections(
        xyxy=np.array(bboxes),
        class_id=np.array([0] * len(bboxes)),
        confidence=np.array([1.0] * len(bboxes)),
    )
    
    image = np.array(image)

    bounding_box_annotator = sv.BoundingBoxAnnotator(thickness=3)
    annotated_image = bounding_box_annotator.annotate(
        scene=image, detections=detections
    )

    return annotated_image


iface = gr.Interface(
    fn=plot_bounding_boxes,
    inputs=[
        Image(type="pil", label="Image"),
        Textbox(label="Bounding Box (separate values by comma, like '100, 200, 300, 400')", lines=3),
        Radio(["xyxy", "xywh"], label="Bounding Box Type"),
    ],
    outputs=Image(type="pil"),
    title="Plot Bounding Boxes",
    description="Plot bounding boxes on an image. Useful for testing object detection models without writing bounding box code. Powered by [supervision](https://github.com/roboflow/supervision).",
)

iface.launch()