Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
|
5 |
+
|
6 |
+
def download_models(model_id):
|
7 |
+
hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")
|
8 |
+
return f"./{model_id}"
|
9 |
+
|
10 |
+
@spaces.GPU
|
11 |
+
def yolov9_inference(img_path, model_id, image_size, conf_threshold, iou_threshold):
|
12 |
+
# Import YOLOv9
|
13 |
+
import yolov9
|
14 |
+
|
15 |
+
# Load the model
|
16 |
+
model_path = download_models(model_id)
|
17 |
+
model = yolov9.load(model_path, device="cuda:0")
|
18 |
+
|
19 |
+
# Set model parameters
|
20 |
+
model.conf = conf_threshold
|
21 |
+
model.iou = iou_threshold
|
22 |
+
|
23 |
+
# Perform inference
|
24 |
+
results = model(img_path, size=image_size)
|
25 |
+
|
26 |
+
# Optionally, show detection bounding boxes on image
|
27 |
+
output = results.render()
|
28 |
+
|
29 |
+
return output[0]
|
30 |
+
|
31 |
+
|
32 |
+
def app():
|
33 |
+
with gr.Blocks():
|
34 |
+
with gr.Row():
|
35 |
+
with gr.Column():
|
36 |
+
img_path = gr.Image(type="filepath", label="Image")
|
37 |
+
model_path = gr.Dropdown(
|
38 |
+
label="Model",
|
39 |
+
choices=[
|
40 |
+
"gelan-c.pt",
|
41 |
+
"gelan-e.pt",
|
42 |
+
"yolov9-c.pt",
|
43 |
+
"yolov9-e.pt",
|
44 |
+
],
|
45 |
+
value="gelan-e.pt",
|
46 |
+
)
|
47 |
+
image_size = gr.Slider(
|
48 |
+
label="Image Size",
|
49 |
+
minimum=320,
|
50 |
+
maximum=1280,
|
51 |
+
step=32,
|
52 |
+
value=640,
|
53 |
+
)
|
54 |
+
conf_threshold = gr.Slider(
|
55 |
+
label="Confidence Threshold",
|
56 |
+
minimum=0.1,
|
57 |
+
maximum=1.0,
|
58 |
+
step=0.1,
|
59 |
+
value=0.4,
|
60 |
+
)
|
61 |
+
iou_threshold = gr.Slider(
|
62 |
+
label="IoU Threshold",
|
63 |
+
minimum=0.1,
|
64 |
+
maximum=1.0,
|
65 |
+
step=0.1,
|
66 |
+
value=0.5,
|
67 |
+
)
|
68 |
+
yolov9_infer = gr.Button(value="Inference")
|
69 |
+
|
70 |
+
with gr.Column():
|
71 |
+
output_numpy = gr.Image(type="numpy",label="Output")
|
72 |
+
|
73 |
+
yolov9_infer.click(
|
74 |
+
fn=yolov9_inference,
|
75 |
+
inputs=[
|
76 |
+
img_path,
|
77 |
+
model_path,
|
78 |
+
image_size,
|
79 |
+
conf_threshold,
|
80 |
+
iou_threshold,
|
81 |
+
],
|
82 |
+
outputs=[output_numpy],
|
83 |
+
)
|
84 |
+
|
85 |
+
gr.Examples(
|
86 |
+
examples=[
|
87 |
+
[
|
88 |
+
"data/zidane.jpg",
|
89 |
+
"gelan-e.pt",
|
90 |
+
640,
|
91 |
+
0.4,
|
92 |
+
0.5,
|
93 |
+
],
|
94 |
+
[
|
95 |
+
"data/huggingface.jpg",
|
96 |
+
"yolov9-c.pt",
|
97 |
+
640,
|
98 |
+
0.4,
|
99 |
+
0.5,
|
100 |
+
],
|
101 |
+
],
|
102 |
+
fn=yolov9_inference,
|
103 |
+
inputs=[
|
104 |
+
img_path,
|
105 |
+
model_path,
|
106 |
+
image_size,
|
107 |
+
conf_threshold,
|
108 |
+
iou_threshold,
|
109 |
+
],
|
110 |
+
outputs=[output_numpy],
|
111 |
+
cache_examples=True,
|
112 |
+
)
|
113 |
+
|
114 |
+
|
115 |
+
gradio_app = gr.Blocks()
|
116 |
+
with gradio_app:
|
117 |
+
gr.HTML()
|
118 |
+
gr.HTML()
|
119 |
+
with gr.Row():
|
120 |
+
with gr.Column():
|
121 |
+
app()
|
122 |
+
|
123 |
+
gradio_app.launch(debug=True)
|