File size: 2,681 Bytes
0ee2f7b
 
 
 
 
 
 
 
 
 
 
 
 
e3f53b0
 
 
 
0ee2f7b
 
 
 
 
046ea89
 
 
 
 
 
 
 
0ee2f7b
 
046ea89
ce26b60
 
 
 
 
 
 
 
 
 
 
 
046ea89
2c0a1c2
046ea89
 
d99e849
 
 
95898ba
d99e849
95898ba
ce26b60
d99e849
046ea89
 
0ee2f7b
95898ba
 
0ee2f7b
 
de683b9
e3f53b0
 
0ee2f7b
 
 
de683b9
0ee2f7b
 
 
5ce7eb3
0ee2f7b
 
 
 
 
 
 
 
 
 
 
 
 
1d87ea4
0ee2f7b
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from huggingface_hub import hf_hub_download
import gradio as gr
from image_inference import RTDETR_ONNX


model_path = hf_hub_download(
    repo_id="hasnatz/v-safe-rf-detr",
    filename="inference_model.onnx"
)

model = RTDETR_ONNX(model_path)

# Define inference function for Gradio
def predict(image, confidence=0.25, max_boxes=100):
    MAX_SIZE = 640   # you can tune this (e.g., 640, 1024)
    if max(image.size) > MAX_SIZE:
        image.thumbnail((MAX_SIZE, MAX_SIZE))
    # image is already a PIL.Image from Gradio
    return model.run_inference(image, confidence_threshold=confidence, max_number_boxes=max_boxes)

# Ready-made example images (local files or URLs)
examples = [
    ["examples/121113-F-LV838-027.jpg"],
    ["examples/goggles_bing_construction_goggles_000109.jpg"],
    ["examples/image-shows-busy-construction-site-where-concrete-mixer-truck-works-alongside-laborers-safety-gear-focus-teamwork-347908285 (Small).jpeg"],
    ["examples/istockphoto-1324894706-612x612.jpg"],
    ["examples/shutterstock_174689291.jpg"],
    ["examples/worker_bing_construction_building_worker_000043 (8).jpg"],
    ["examples/worker_bing_construction_building_worker_000066 (1).jpg"],
    ["examples/worker_bing_construction_building_worker_000091 (2).jpg"]
]


# Building Gradio UI


custom_theme = gr.themes.Base().set(
    body_background_fill="#0f0f11",   # background color
    block_background_fill="#0f0f11",  # blocks background
    block_border_color="#0f0f11",     # remove border feel
    background_fill_primary="#0f0f11" # for other sections
)


with gr.Blocks(theme=custom_theme) as demo:

    gr.HTML(
    """
    <div style="text-align: center;">
    <img 
        src='/gradio_api/file=Logo.png' 
        alt='My Image' 
        style='height: 100px; width: auto; display: block; margin: 0 auto;'
    >
    <h2>V-Safe: Construction Site Safety Detection Demo</h2>
    <br>
</div>
    """
    )

    

    with gr.Row():
        with gr.Column():
            input_img = gr.Image(type="pil", label="Upload an Image", height=450)
            confidence = gr.Slider(0.0, 1.0, value=0.25, step=0.05, label="Confidence Threshold")
            
            run_btn = gr.Button("Run Inference")

        with gr.Column():
            output_img = gr.Image(type="pil", label="Annotated Result", height=450)

    run_btn.click(
        fn=predict,
        inputs=[input_img, confidence],
        outputs=output_img
    )

    gr.Examples(
        examples=examples,
        inputs=[input_img],
        outputs=output_img,
        fn=predict,
        cache_examples=True
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(allowed_paths=["Logo.png"])