Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,15 @@ import cv2
|
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# Load the YOLOv8 model
|
| 10 |
model = YOLO('yolov8n.pt')
|
| 11 |
|
| 12 |
def process_image(image):
|
|
|
|
|
|
|
|
|
|
| 13 |
results = model(image)
|
| 14 |
# Get detection information
|
| 15 |
boxes = results[0].boxes
|
|
@@ -23,6 +27,9 @@ def process_image(image):
|
|
| 23 |
return Image.fromarray(results[0].plot()), "\n".join(detection_info)
|
| 24 |
|
| 25 |
def process_video(video_path):
|
|
|
|
|
|
|
|
|
|
| 26 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
|
| 27 |
output_path = temp_file.name
|
| 28 |
|
|
@@ -72,6 +79,9 @@ def process_video(video_path):
|
|
| 72 |
return output_path, summary
|
| 73 |
|
| 74 |
def detect_objects(media):
|
|
|
|
|
|
|
|
|
|
| 75 |
if media is None:
|
| 76 |
return None, None, None, "Please upload an image or video to begin detection.", gr.update(visible=True), gr.update(visible=False)
|
| 77 |
|
|
@@ -103,8 +113,6 @@ custom_css = """
|
|
| 103 |
}
|
| 104 |
|
| 105 |
#logo-img {
|
| 106 |
-
display: block;
|
| 107 |
-
margin: 0 auto;
|
| 108 |
max-height: 100px;
|
| 109 |
margin-bottom: 20px;
|
| 110 |
}
|
|
@@ -133,45 +141,50 @@ custom_css = """
|
|
| 133 |
margin-top: 10px;
|
| 134 |
font-family: monospace;
|
| 135 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
"""
|
| 137 |
|
| 138 |
# Create Gradio interface
|
| 139 |
with gr.Blocks(css=custom_css) as demo:
|
| 140 |
with gr.Column(elem_id="app-container"):
|
| 141 |
# Logo and Header
|
| 142 |
-
gr.
|
| 143 |
-
""
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
)
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
file_types=["image", "video"]
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
# Status Message
|
| 162 |
-
status_text = gr.Textbox(
|
| 163 |
-
label="Status",
|
| 164 |
-
value="Waiting for upload...",
|
| 165 |
-
interactive=False
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
# Detection Information
|
| 169 |
-
detection_info = gr.Textbox(
|
| 170 |
-
label="Detection Results",
|
| 171 |
-
elem_classes="detection-info",
|
| 172 |
-
interactive=False
|
| 173 |
)
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
# Results Section
|
| 176 |
with gr.Column(elem_classes="results-container"):
|
| 177 |
with gr.Row():
|
|
@@ -180,13 +193,19 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 180 |
with gr.Column(visible=False) as video_column:
|
| 181 |
output_video = gr.Video(label="Processed Video")
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
demo.launch(share=True)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
|
| 10 |
# Load the YOLOv8 model
|
| 11 |
model = YOLO('yolov8n.pt')
|
| 12 |
|
| 13 |
def process_image(image):
|
| 14 |
+
"""
|
| 15 |
+
Process a single image for object detection
|
| 16 |
+
"""
|
| 17 |
results = model(image)
|
| 18 |
# Get detection information
|
| 19 |
boxes = results[0].boxes
|
|
|
|
| 27 |
return Image.fromarray(results[0].plot()), "\n".join(detection_info)
|
| 28 |
|
| 29 |
def process_video(video_path):
|
| 30 |
+
"""
|
| 31 |
+
Process video for object detection
|
| 32 |
+
"""
|
| 33 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
|
| 34 |
output_path = temp_file.name
|
| 35 |
|
|
|
|
| 79 |
return output_path, summary
|
| 80 |
|
| 81 |
def detect_objects(media):
|
| 82 |
+
"""
|
| 83 |
+
Unified function to handle both image and video inputs
|
| 84 |
+
"""
|
| 85 |
if media is None:
|
| 86 |
return None, None, None, "Please upload an image or video to begin detection.", gr.update(visible=True), gr.update(visible=False)
|
| 87 |
|
|
|
|
| 113 |
}
|
| 114 |
|
| 115 |
#logo-img {
|
|
|
|
|
|
|
| 116 |
max-height: 100px;
|
| 117 |
margin-bottom: 20px;
|
| 118 |
}
|
|
|
|
| 141 |
margin-top: 10px;
|
| 142 |
font-family: monospace;
|
| 143 |
}
|
| 144 |
+
|
| 145 |
+
.center {
|
| 146 |
+
display: flex;
|
| 147 |
+
justify-content: center;
|
| 148 |
+
align-items: center;
|
| 149 |
+
margin-bottom: 1rem;
|
| 150 |
+
}
|
| 151 |
"""
|
| 152 |
|
| 153 |
# Create Gradio interface
|
| 154 |
with gr.Blocks(css=custom_css) as demo:
|
| 155 |
with gr.Column(elem_id="app-container"):
|
| 156 |
# Logo and Header
|
| 157 |
+
with gr.Column(elem_classes="center"):
|
| 158 |
+
gr.Image("logo-h.png",
|
| 159 |
+
show_label=False,
|
| 160 |
+
container=False,
|
| 161 |
+
elem_id="logo-img",
|
| 162 |
+
height=100)
|
|
|
|
| 163 |
|
| 164 |
+
gr.Markdown("# π Object Detection")
|
| 165 |
+
|
| 166 |
+
# Upload Section
|
| 167 |
+
with gr.Column(elem_classes="upload-box"):
|
| 168 |
+
gr.Markdown("### π€ Upload your file")
|
| 169 |
+
input_media = gr.File(
|
| 170 |
+
label="Drag and drop or click to upload (Images: jpg, jpeg, png | Videos: mp4, avi, mov)",
|
| 171 |
+
file_types=["image", "video"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
)
|
| 173 |
|
| 174 |
+
# Status Message
|
| 175 |
+
status_text = gr.Textbox(
|
| 176 |
+
label="Status",
|
| 177 |
+
value="Waiting for upload...",
|
| 178 |
+
interactive=False
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Detection Information
|
| 182 |
+
detection_info = gr.Textbox(
|
| 183 |
+
label="Detection Results",
|
| 184 |
+
elem_classes="detection-info",
|
| 185 |
+
interactive=False
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
# Results Section
|
| 189 |
with gr.Column(elem_classes="results-container"):
|
| 190 |
with gr.Row():
|
|
|
|
| 193 |
with gr.Column(visible=False) as video_column:
|
| 194 |
output_video = gr.Video(label="Processed Video")
|
| 195 |
|
| 196 |
+
# Handle file upload
|
| 197 |
+
input_media.upload(
|
| 198 |
+
fn=detect_objects,
|
| 199 |
+
inputs=[input_media],
|
| 200 |
+
outputs=[
|
| 201 |
+
output_image,
|
| 202 |
+
output_video,
|
| 203 |
+
detection_info,
|
| 204 |
+
status_text,
|
| 205 |
+
image_column,
|
| 206 |
+
video_column
|
| 207 |
+
]
|
| 208 |
+
)
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
| 211 |
demo.launch(share=True)
|