File size: 1,291 Bytes
317c901
ec2a6ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1828143
 
 
 
 
 
fbf9586
ec2a6ff
 
1828143
 
 
317c901
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
import gradio as gr
from PIL import Image, ImageDraw
from transformers import pipeline


def plot_results(image, results, threshold=0.7):
    image = Image.fromarray(np.uint8(image))
    draw = ImageDraw.Draw(image)
    for result in results:
        score = result["score"]
        label = result["label"]
        box = list(result["box"].values())
        if score > threshold:
            x, y, x2, y2 = tuple(box)
            draw.rectangle((x, y, x2, y2), outline="red", width=1)
            draw.text((x, y), label, fill="white")
            draw.text(
                (x + 0.5, y - 0.5),
                text=str(score),
                fill="green" if score > 0.7 else "red",
            )
    return image

def predict(image):
    # make the object detection pipeline
    obj_detector = pipeline(
        "object-detection", model="anindya64/detr-resnet-50-dc5-hardhat-finetuned"
    )
    results = obj_detector(train_dataset[0]["image"])
    return plot_results(image)

title = "Do you have your helmet on?"
description = """
DETR model finetuned on "anindya64/hardhat" for hard hats detection.
"""

demo = gr.Interface(
    fn=predict, 
    inputs=gr.Image(type="filepath", label="Input Image"), 
    outputs="image",
    title=title,
    description=description
)
demo.launch()