File size: 1,120 Bytes
4cc3971
cd8dd20
 
 
29947a9
 
cd8dd20
 
69af496
ff6c220
cd8dd20
4cc3971
cd8dd20
4cc3971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd8dd20
 
4cc3971
cd8dd20
 
 
 
 
 
 
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

import gradio as gr
import requests
from transformers import pipeline
from PIL import Image, ImageDraw
import numpy as np

obj_detector = pipeline(
    "object-detection", model="bninaos/finetuned-ViT-model",
device = 'cpu')

def plot_results(image, results, threshold=0.7):
    results = obj_detector(image)
    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 > threshold else "red",
            )
    return image

iface = gr.Interface(
    fn=plot_results,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(type="pil"),
    title="Hardhat Detection",
    description="Upload an image to detect hardhats.",
)

iface.launch(share = True)