Spaces:
Sleeping
Sleeping
sergey21000
commited on
Commit
•
946fe5f
1
Parent(s):
5d171e3
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from ultralytics import YOLO
|
|
10 |
from utils import download_model, detect_image, detect_video, get_csv_annotate
|
11 |
|
12 |
|
13 |
-
# =======================
|
14 |
|
15 |
MODELS_DIR = Path('models')
|
16 |
MODELS_DIR.mkdir(exist_ok=True)
|
@@ -31,15 +31,15 @@ IMAGE_EXTENSIONS = ['.jpg', '.jpeg', '.png']
|
|
31 |
VIDEO_EXTENSIONS = ['.mp4', '.avi']
|
32 |
|
33 |
|
34 |
-
# ===================
|
35 |
|
36 |
def change_model(model_state: Dict[str, YOLO], model_name: str):
|
37 |
progress = gr.Progress()
|
38 |
-
progress(0.3, desc='
|
39 |
model_path = download_model(model_name)
|
40 |
-
progress(0.7, desc='
|
41 |
model_state['model'] = YOLO(model_path)
|
42 |
-
return f
|
43 |
|
44 |
|
45 |
def detect(file_path: str, file_link: str, model_state: Dict[str, YOLO], conf: float, iou: float):
|
@@ -50,15 +50,15 @@ def detect(file_path: str, file_link: str, model_state: Dict[str, YOLO], conf: f
|
|
50 |
file_ext = f'.{file_path.rsplit(".")[-1]}'
|
51 |
if file_ext in IMAGE_EXTENSIONS:
|
52 |
np_image = detect_image(file_path, model, conf, iou)
|
53 |
-
return np_image, "
|
54 |
elif file_ext in VIDEO_EXTENSIONS or 'youtube.com' in file_link:
|
55 |
video_path = detect_video(file_path, model, conf, iou)
|
56 |
-
return video_path, "
|
57 |
else:
|
58 |
-
gr.Info('
|
59 |
return None, None
|
60 |
|
61 |
-
# ===================
|
62 |
|
63 |
def get_output_media_components(detect_result: Optional[Union[np.ndarray, str, Path]] = None):
|
64 |
visible = isinstance(detect_result, np.ndarray)
|
@@ -88,14 +88,14 @@ def get_output_media_components(detect_result: Optional[Union[np.ndarray, str, P
|
|
88 |
|
89 |
def get_download_csv_btn(csv_annotations_path: Optional[Path] = None):
|
90 |
download_csv_btn = gr.DownloadButton(
|
91 |
-
label='
|
92 |
value=csv_annotations_path,
|
93 |
scale=0,
|
94 |
visible=csv_annotations_path is not None,
|
95 |
)
|
96 |
return download_csv_btn
|
97 |
|
98 |
-
# ===================
|
99 |
|
100 |
css = '''
|
101 |
.gradio-container { width: 70% !important }
|
@@ -109,12 +109,12 @@ with gr.Blocks(css=css) as demo:
|
|
109 |
|
110 |
with gr.Row():
|
111 |
with gr.Column():
|
112 |
-
file_path = gr.File(file_types=['image', 'video'], file_count='single', label='
|
113 |
-
file_link = gr.Textbox(label='
|
114 |
-
model_name = gr.Radio(choices=MODEL_NAMES, value=MODEL_NAMES[0], label='
|
115 |
-
conf = gr.Slider(0, 1, value=0.5, step=0.05, label='
|
116 |
-
iou = gr.Slider(0, 1, value=0.7, step=0.1, label='
|
117 |
-
status_message = gr.Textbox(value='
|
118 |
detect_btn = gr.Button('Detect', interactive=True)
|
119 |
|
120 |
with gr.Column():
|
@@ -144,7 +144,7 @@ with gr.Blocks(css=css) as demo:
|
|
144 |
inputs=[detect_result],
|
145 |
outputs=[image_output, video_output, clear_btn],
|
146 |
).then(
|
147 |
-
fn=lambda: '
|
148 |
inputs=None,
|
149 |
outputs=[status_message],
|
150 |
).then(
|
|
|
10 |
from utils import download_model, detect_image, detect_video, get_csv_annotate
|
11 |
|
12 |
|
13 |
+
# ======================= MODEL ===================================
|
14 |
|
15 |
MODELS_DIR = Path('models')
|
16 |
MODELS_DIR.mkdir(exist_ok=True)
|
|
|
31 |
VIDEO_EXTENSIONS = ['.mp4', '.avi']
|
32 |
|
33 |
|
34 |
+
# =================== ADDITIONAL INTERFACE FUNCTIONS ========================
|
35 |
|
36 |
def change_model(model_state: Dict[str, YOLO], model_name: str):
|
37 |
progress = gr.Progress()
|
38 |
+
progress(0.3, desc='Downloading the model')
|
39 |
model_path = download_model(model_name)
|
40 |
+
progress(0.7, desc='Model initialization')
|
41 |
model_state['model'] = YOLO(model_path)
|
42 |
+
return f'Model {model_name} initialized'
|
43 |
|
44 |
|
45 |
def detect(file_path: str, file_link: str, model_state: Dict[str, YOLO], conf: float, iou: float):
|
|
|
50 |
file_ext = f'.{file_path.rsplit(".")[-1]}'
|
51 |
if file_ext in IMAGE_EXTENSIONS:
|
52 |
np_image = detect_image(file_path, model, conf, iou)
|
53 |
+
return np_image, "Detection complete, opening image..."
|
54 |
elif file_ext in VIDEO_EXTENSIONS or 'youtube.com' in file_link:
|
55 |
video_path = detect_video(file_path, model, conf, iou)
|
56 |
+
return video_path, "Detection complete, converting and opening video..."
|
57 |
else:
|
58 |
+
gr.Info('Invalid image or video format...')
|
59 |
return None, None
|
60 |
|
61 |
+
# =================== INTERFACE COMPONENTS ============================
|
62 |
|
63 |
def get_output_media_components(detect_result: Optional[Union[np.ndarray, str, Path]] = None):
|
64 |
visible = isinstance(detect_result, np.ndarray)
|
|
|
88 |
|
89 |
def get_download_csv_btn(csv_annotations_path: Optional[Path] = None):
|
90 |
download_csv_btn = gr.DownloadButton(
|
91 |
+
label='Download csv annotations for video',
|
92 |
value=csv_annotations_path,
|
93 |
scale=0,
|
94 |
visible=csv_annotations_path is not None,
|
95 |
)
|
96 |
return download_csv_btn
|
97 |
|
98 |
+
# =================== APPINTERFACE ==========================
|
99 |
|
100 |
css = '''
|
101 |
.gradio-container { width: 70% !important }
|
|
|
109 |
|
110 |
with gr.Row():
|
111 |
with gr.Column():
|
112 |
+
file_path = gr.File(file_types=['image', 'video'], file_count='single', label='Select an image or video')
|
113 |
+
file_link = gr.Textbox(label='Direct link to image or YouTube link')
|
114 |
+
model_name = gr.Radio(choices=MODEL_NAMES, value=MODEL_NAMES[0], label='Select YOLO model')
|
115 |
+
conf = gr.Slider(0, 1, value=0.5, step=0.05, label='Confidence')
|
116 |
+
iou = gr.Slider(0, 1, value=0.7, step=0.1, label='IOU')
|
117 |
+
status_message = gr.Textbox(value='Ready to go', label='Status')
|
118 |
detect_btn = gr.Button('Detect', interactive=True)
|
119 |
|
120 |
with gr.Column():
|
|
|
144 |
inputs=[detect_result],
|
145 |
outputs=[image_output, video_output, clear_btn],
|
146 |
).then(
|
147 |
+
fn=lambda: 'Ready to go',
|
148 |
inputs=None,
|
149 |
outputs=[status_message],
|
150 |
).then(
|