Spaces:
Sleeping
Sleeping
sergey21000
commited on
Commit
•
530388a
1
Parent(s):
a3c6281
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,18 @@
|
|
1 |
import shutil
|
2 |
from pathlib import Path
|
3 |
-
from typing import
|
4 |
|
5 |
import numpy as np
|
6 |
import gradio as gr
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
-
from utils import
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
|
12 |
# ======================= MODEL ===================================
|
@@ -96,89 +102,100 @@ def get_download_csv_btn(csv_annotations_path: Optional[Path] = None):
|
|
96 |
|
97 |
# =================== APPINTERFACE ==========================
|
98 |
|
99 |
-
css = '''.gradio-container {
|
100 |
|
101 |
with gr.Blocks(css=css) as demo:
|
102 |
-
gr.
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
with gr.
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
)
|
179 |
|
180 |
-
gr.HTML("""<h3 style='text-align: center'>
|
181 |
-
<a href="https://github.com/sergey21000/yolo-detector" target='_blank'>GitHub Repository</a></h3>
|
182 |
-
""")
|
183 |
-
|
184 |
demo.launch(server_name='0.0.0.0') # debug=True
|
|
|
1 |
import shutil
|
2 |
from pathlib import Path
|
3 |
+
from typing import Dict, Union, Optional
|
4 |
|
5 |
import numpy as np
|
6 |
import gradio as gr
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
+
from utils import (
|
10 |
+
download_model,
|
11 |
+
detect_image,
|
12 |
+
detect_video,
|
13 |
+
get_csv_annotate,
|
14 |
+
get_matplotlib_fig,
|
15 |
+
)
|
16 |
|
17 |
|
18 |
# ======================= MODEL ===================================
|
|
|
102 |
|
103 |
# =================== APPINTERFACE ==========================
|
104 |
|
105 |
+
css = '''.gradio-container {width: 70% !important}'''
|
106 |
|
107 |
with gr.Blocks(css=css) as demo:
|
108 |
+
with gr.Tab('Detection image / video'):
|
109 |
+
gr.HTML("""<h3 style='text-align: center'>YOLOv11 Detector</h3>""")
|
110 |
+
|
111 |
+
model_state = gr.State({'model': default_model})
|
112 |
+
detect_result = gr.State(None)
|
113 |
+
csv_annotations_path = gr.State(None)
|
114 |
+
|
115 |
+
with gr.Row():
|
116 |
+
with gr.Column():
|
117 |
+
file_path = gr.File(file_types=['image', 'video'], file_count='single', label='Select image or video')
|
118 |
+
|
119 |
+
# file_link = gr.Textbox(label='Direct link to image or YouTube link')
|
120 |
+
file_link = gr.State(None)
|
121 |
+
|
122 |
+
model_name = gr.Radio(choices=MODEL_NAMES, value=MODEL_NAMES[0], label='Select YOLO model')
|
123 |
+
conf = gr.Slider(0, 1, value=0.5, step=0.05, label='Confidence')
|
124 |
+
iou = gr.Slider(0, 1, value=0.7, step=0.1, label='IOU')
|
125 |
+
status_message = gr.Textbox(value='Ready to go', label='Status')
|
126 |
+
detect_btn = gr.Button('Detect', interactive=True)
|
127 |
+
|
128 |
+
with gr.Column():
|
129 |
+
image_output, video_output, clear_btn = get_output_media_components()
|
130 |
+
download_csv_btn = get_download_csv_btn()
|
131 |
+
|
132 |
+
model_name.change(
|
133 |
+
fn=lambda: gr.update(interactive=False),
|
134 |
+
inputs=None,
|
135 |
+
outputs=[detect_btn],
|
136 |
+
).then(
|
137 |
+
fn=change_model,
|
138 |
+
inputs=[model_state, model_name],
|
139 |
+
outputs=[status_message],
|
140 |
+
).success(
|
141 |
+
fn=lambda: gr.update(interactive=True),
|
142 |
+
inputs=None,
|
143 |
+
outputs=[detect_btn],
|
144 |
+
)
|
145 |
+
|
146 |
+
detect_btn.click(
|
147 |
+
fn=detect,
|
148 |
+
inputs=[file_path, file_link, model_state, conf, iou],
|
149 |
+
outputs=[detect_result, status_message],
|
150 |
+
).success(
|
151 |
+
fn=get_output_media_components,
|
152 |
+
inputs=[detect_result],
|
153 |
+
outputs=[image_output, video_output, clear_btn],
|
154 |
+
).then(
|
155 |
+
fn=lambda: 'Ready to go',
|
156 |
+
inputs=None,
|
157 |
+
outputs=[status_message],
|
158 |
+
).then(
|
159 |
+
fn=get_csv_annotate,
|
160 |
+
inputs=[detect_result],
|
161 |
+
outputs=[csv_annotations_path],
|
162 |
+
).success(
|
163 |
+
fn=get_download_csv_btn,
|
164 |
+
inputs=[csv_annotations_path],
|
165 |
+
outputs=[download_csv_btn],
|
166 |
+
)
|
167 |
+
|
168 |
+
def clear_results_dir(detect_result):
|
169 |
+
if isinstance(detect_result, Path):
|
170 |
+
shutil.rmtree(detect_result.parent, ignore_errors=True)
|
171 |
+
|
172 |
+
clear_components = [image_output, video_output, clear_btn, download_csv_btn]
|
173 |
+
clear_btn.click(
|
174 |
+
fn=lambda: [gr.update(visible=False) for _ in range(len(clear_components))],
|
175 |
+
inputs=None,
|
176 |
+
outputs=clear_components,
|
177 |
+
).then(
|
178 |
+
fn=clear_results_dir,
|
179 |
+
inputs=[detect_result],
|
180 |
+
outputs=None,
|
181 |
+
).then(
|
182 |
+
fn=lambda: (None, None),
|
183 |
+
inputs=None,
|
184 |
+
outputs=[detect_result, csv_annotations_path]
|
185 |
+
)
|
186 |
+
|
187 |
+
gr.HTML("""<h3 style='text-align: center'>
|
188 |
+
<a href="https://github.com/sergey21000/yolo-detector" target='_blank'>GitHub Repository</a></h3>
|
189 |
+
""")
|
190 |
+
|
191 |
+
|
192 |
+
with gr.Tab('Show detection video results'):
|
193 |
+
show_results_btn = gr.Button('Show detection results', scale=1)
|
194 |
+
gr_info = 'To display the results, perform video detection on the first tab'
|
195 |
+
show_results_btn.click(
|
196 |
+
fn=lambda csv_path: get_matplotlib_fig(csv_path) if csv_path is not None else gr.Info(gr_info),
|
197 |
+
inputs=[csv_annotations_path],
|
198 |
+
outputs=gr.Plot(),
|
199 |
)
|
200 |
|
|
|
|
|
|
|
|
|
201 |
demo.launch(server_name='0.0.0.0') # debug=True
|