sergey21000 commited on
Commit
530388a
1 Parent(s): a3c6281

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -83
app.py CHANGED
@@ -1,12 +1,18 @@
1
  import shutil
2
  from pathlib import Path
3
- from typing import List, Dict, Union, Tuple, Literal, Optional
4
 
5
  import numpy as np
6
  import gradio as gr
7
  from ultralytics import YOLO
8
 
9
- from utils import download_model, detect_image, detect_video, get_csv_annotate
 
 
 
 
 
 
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 { width: 70% !important }'''
100
 
101
  with gr.Blocks(css=css) as demo:
102
- gr.HTML("""<h3 style='text-align: center'>YOLOv11 Detector</h3>""")
103
-
104
- model_state = gr.State({'model': default_model})
105
- detect_result = gr.State(None)
106
- csv_annotations_path = gr.State(None)
107
-
108
- with gr.Row():
109
- with gr.Column():
110
- file_path = gr.File(file_types=['image', 'video'], file_count='single', label='Select image or video')
111
-
112
- # file_link = gr.Textbox(label='Direct link to image or YouTube link')
113
- file_link = gr.State(None)
114
-
115
- model_name = gr.Radio(choices=MODEL_NAMES, value=MODEL_NAMES[0], label='Select YOLO model')
116
- conf = gr.Slider(0, 1, value=0.5, step=0.05, label='Confidence')
117
- iou = gr.Slider(0, 1, value=0.7, step=0.1, label='IOU')
118
- status_message = gr.Textbox(value='Ready to go', label='Status')
119
- detect_btn = gr.Button('Detect', interactive=True)
120
-
121
- with gr.Column():
122
- image_output, video_output, clear_btn = get_output_media_components()
123
- download_csv_btn = get_download_csv_btn()
124
-
125
- model_name.change(
126
- fn=lambda: gr.update(interactive=False),
127
- inputs=None,
128
- outputs=[detect_btn],
129
- ).then(
130
- fn=change_model,
131
- inputs=[model_state, model_name],
132
- outputs=[status_message],
133
- ).success(
134
- fn=lambda: gr.update(interactive=True),
135
- inputs=None,
136
- outputs=[detect_btn],
137
- )
138
-
139
- detect_btn.click(
140
- fn=detect,
141
- inputs=[file_path, file_link, model_state, conf, iou],
142
- outputs=[detect_result, status_message],
143
- ).success(
144
- fn=get_output_media_components,
145
- inputs=[detect_result],
146
- outputs=[image_output, video_output, clear_btn],
147
- ).then(
148
- fn=lambda: 'Ready to go',
149
- inputs=None,
150
- outputs=[status_message],
151
- ).then(
152
- fn=get_csv_annotate,
153
- inputs=[detect_result],
154
- outputs=[csv_annotations_path],
155
- ).success(
156
- fn=get_download_csv_btn,
157
- inputs=[csv_annotations_path],
158
- outputs=[download_csv_btn],
159
- )
160
-
161
- def clear_results_dir(detect_result):
162
- if isinstance(detect_result, Path):
163
- shutil.rmtree(detect_result.parent, ignore_errors=True)
164
-
165
- clear_components = [image_output, video_output, clear_btn, download_csv_btn]
166
- clear_btn.click(
167
- fn=lambda: [gr.update(visible=False) for _ in range(len(clear_components))],
168
- inputs=None,
169
- outputs=clear_components,
170
- ).then(
171
- fn=clear_results_dir,
172
- inputs=[detect_result],
173
- outputs=None,
174
- ).then(
175
- fn=lambda: (None, None),
176
- inputs=None,
177
- outputs=[detect_result, csv_annotations_path]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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