Update app.py
Browse files
app.py
CHANGED
@@ -4,121 +4,96 @@ from PIL import Image
|
|
4 |
import os
|
5 |
import yolov9
|
6 |
|
7 |
-
|
8 |
-
# Load your custom-trained model
|
9 |
-
model_name='./best.pt'
|
10 |
-
#model = YOLO(model_name)
|
11 |
-
# Images
|
12 |
-
|
13 |
def yolov9_inference(img_path, image_size, conf_threshold, iou_threshold):
|
14 |
-
"""
|
15 |
-
Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjust
|
16 |
-
the input size and apply test time augmentation.
|
17 |
-
|
18 |
-
:param model_path: Path to the YOLOv9 model file.
|
19 |
-
:param conf_threshold: Confidence threshold for NMS.
|
20 |
-
:param iou_threshold: IoU threshold for NMS.
|
21 |
-
:param img_path: Path to the image file.
|
22 |
-
:param size: Optional, input size for inference.
|
23 |
-
:return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying.
|
24 |
-
"""
|
25 |
-
# Import YOLOv9
|
26 |
-
|
27 |
-
# Load the model
|
28 |
-
#model_path = download_models(model_id)
|
29 |
model = yolov9.load('./best.pt')
|
30 |
-
|
31 |
-
# Set model parameters
|
32 |
model.conf = conf_threshold
|
33 |
model.iou = iou_threshold
|
34 |
-
|
35 |
-
# Perform inference
|
36 |
results = model(img_path, size=image_size)
|
37 |
-
|
38 |
-
# Optionally, show detection bounding boxes on image
|
39 |
output = results.render()
|
40 |
-
|
41 |
return output[0]
|
|
|
42 |
def app():
|
43 |
-
with gr.Blocks():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
with gr.Row():
|
45 |
-
with gr.Column():
|
46 |
-
img_path = gr.Image(type="filepath", label="Image")
|
47 |
-
|
48 |
-
|
49 |
-
minimum=320,
|
50 |
-
maximum=
|
51 |
-
step=
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
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 |
-
|
71 |
-
with gr.Column():
|
72 |
-
output_numpy = gr.Image(type="numpy",label="Output")
|
73 |
-
|
74 |
yolov9_infer.click(
|
75 |
fn=yolov9_inference,
|
76 |
-
inputs=[
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
iou_threshold,
|
81 |
-
],
|
82 |
-
outputs=[output_numpy])
|
83 |
gr.Examples(
|
84 |
examples=[
|
85 |
-
[
|
86 |
-
|
87 |
-
|
88 |
-
640,
|
89 |
-
0.4,
|
90 |
-
0.5,
|
91 |
-
],
|
92 |
-
[
|
93 |
-
"./images.jpeg", #images.jpeg
|
94 |
-
|
95 |
-
640,
|
96 |
-
0.4,
|
97 |
-
0.5,
|
98 |
-
],
|
99 |
],
|
100 |
fn=yolov9_inference,
|
101 |
-
inputs=[
|
102 |
-
img_path,
|
103 |
-
image_size,
|
104 |
-
conf_threshold,
|
105 |
-
iou_threshold,
|
106 |
-
],
|
107 |
outputs=[output_numpy],
|
108 |
cache_examples=True,
|
109 |
)
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
|
|
|
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
gr.HTML(
|
116 |
-
"""
|
117 |
-
<h1 style='text-align: center'>
|
118 |
-
YOLOv9: Manhole Detector
|
119 |
-
</h1>
|
120 |
-
""")
|
121 |
-
with gr.Row():
|
122 |
-
with gr.Column():
|
123 |
-
app()
|
124 |
-
gradio_app.launch(debug=True, share=True)
|
|
|
4 |
import os
|
5 |
import yolov9
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def yolov9_inference(img_path, image_size, conf_threshold, iou_threshold):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
model = yolov9.load('./best.pt')
|
|
|
|
|
9 |
model.conf = conf_threshold
|
10 |
model.iou = iou_threshold
|
|
|
|
|
11 |
results = model(img_path, size=image_size)
|
|
|
|
|
12 |
output = results.render()
|
|
|
13 |
return output[0]
|
14 |
+
|
15 |
def app():
|
16 |
+
with gr.Blocks() as demo:
|
17 |
+
gr.HTML(
|
18 |
+
"""
|
19 |
+
<div style="text-align: center; max-width: 800px; margin: 0 auto;">
|
20 |
+
<h1 style="color: #2c3e50; font-size: 2.5em; margin-bottom: 0.5em;">YOLOv9: Manhole Detector</h1>
|
21 |
+
<p style="color: #34495e; font-size: 1.2em; margin-bottom: 1em;">Detect manholes in images using YOLOv9</p>
|
22 |
+
</div>
|
23 |
+
"""
|
24 |
+
)
|
25 |
with gr.Row():
|
26 |
+
with gr.Column(scale=1, min_width=300):
|
27 |
+
img_path = gr.Image(type="filepath", label="Upload Image")
|
28 |
+
with gr.Box():
|
29 |
+
gr.Markdown("### Model Parameters")
|
30 |
+
image_size = gr.Slider(label="Image Size", minimum=320, maximum=1280, step=32, value=640)
|
31 |
+
conf_threshold = gr.Slider(label="Confidence Threshold", minimum=0.1, maximum=1.0, step=0.1, value=0.4)
|
32 |
+
iou_threshold = gr.Slider(label="IoU Threshold", minimum=0.1, maximum=1.0, step=0.1, value=0.5)
|
33 |
+
yolov9_infer = gr.Button("Detect Manholes", variant="primary")
|
34 |
+
|
35 |
+
with gr.Column(scale=1, min_width=300):
|
36 |
+
output_numpy = gr.Image(type="numpy", label="Detection Result")
|
37 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
yolov9_infer.click(
|
39 |
fn=yolov9_inference,
|
40 |
+
inputs=[img_path, image_size, conf_threshold, iou_threshold],
|
41 |
+
outputs=[output_numpy]
|
42 |
+
)
|
43 |
+
|
|
|
|
|
|
|
44 |
gr.Examples(
|
45 |
examples=[
|
46 |
+
["./openmanhole.jpg", 640, 0.4, 0.5],
|
47 |
+
["./images.jpeg", 640, 0.4, 0.5],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
],
|
49 |
fn=yolov9_inference,
|
50 |
+
inputs=[img_path, image_size, conf_threshold, iou_threshold],
|
|
|
|
|
|
|
|
|
|
|
51 |
outputs=[output_numpy],
|
52 |
cache_examples=True,
|
53 |
)
|
54 |
+
|
55 |
+
return demo
|
56 |
+
|
57 |
+
css = """
|
58 |
+
body {
|
59 |
+
font-family: 'Arial', sans-serif;
|
60 |
+
background-color: #f0f3f6;
|
61 |
+
}
|
62 |
+
.gradio-container {
|
63 |
+
max-width: 900px !important;
|
64 |
+
margin-left: auto !important;
|
65 |
+
margin-right: auto !important;
|
66 |
+
background-color: white;
|
67 |
+
border-radius: 10px;
|
68 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
69 |
+
padding: 20px;
|
70 |
+
}
|
71 |
+
.gr-button {
|
72 |
+
background-color: #3498db !important;
|
73 |
+
border: none !important;
|
74 |
+
}
|
75 |
+
.gr-button:hover {
|
76 |
+
background-color: #2980b9 !important;
|
77 |
+
}
|
78 |
+
.gr-box {
|
79 |
+
border-radius: 8px;
|
80 |
+
border: 1px solid #e0e0e0;
|
81 |
+
padding: 15px;
|
82 |
+
margin-top: 10px;
|
83 |
+
background-color: #f9f9f9;
|
84 |
+
}
|
85 |
+
.gr-form {
|
86 |
+
background-color: white;
|
87 |
+
padding: 15px;
|
88 |
+
border-radius: 8px;
|
89 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
|
90 |
+
}
|
91 |
+
"""
|
92 |
+
|
93 |
+
demo = gr.Blocks(css=css)
|
94 |
|
95 |
+
with demo:
|
96 |
+
app()
|
97 |
|
98 |
+
if __name__ == "__main__":
|
99 |
+
demo.launch(debug=True, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|