Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -108,30 +108,131 @@ def process_image(image):
|
|
| 108 |
|
| 109 |
return Image.fromarray(img)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
with gr.Tabs():
|
| 117 |
-
with gr.TabItem("Video Detection"):
|
| 118 |
-
with gr.Row():
|
| 119 |
-
video_input = gr.Video(label="Upload Video", interactive=True, elem_id="video-input")
|
| 120 |
-
process_button = gr.Button("Process Video", variant="primary", elem_id="video-process-btn")
|
| 121 |
-
video_output = gr.Video(label="Processed Video", elem_id="video-output")
|
| 122 |
-
process_button.click(fn=process_video, inputs=video_input, outputs=video_output)
|
| 123 |
-
|
| 124 |
-
with gr.TabItem("Image Detection"):
|
| 125 |
-
with gr.Row():
|
| 126 |
-
image_input = gr.Image(type="pil", label="Upload Image", interactive=True)
|
| 127 |
with gr.Row():
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
with gr.Row():
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
demo.launch()
|
| 137 |
-
|
|
|
|
| 108 |
|
| 109 |
return Image.fromarray(img)
|
| 110 |
|
| 111 |
+
css = """
|
| 112 |
+
#title {
|
| 113 |
+
text-align: center;
|
| 114 |
+
color: #2C3E50;
|
| 115 |
+
font-size: 2.5rem;
|
| 116 |
+
margin: 1.5rem 0;
|
| 117 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
.gradio-container {
|
| 121 |
+
background-color: #F5F7FA;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
.tab-item {
|
| 125 |
+
background-color: white;
|
| 126 |
+
border-radius: 10px;
|
| 127 |
+
padding: 20px;
|
| 128 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 129 |
+
margin: 10px;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
.button-row {
|
| 133 |
+
display: flex;
|
| 134 |
+
justify-content: space-around;
|
| 135 |
+
margin: 1rem 0;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
#video-process-btn, #submit-btn {
|
| 139 |
+
background-color: #3498DB;
|
| 140 |
+
border: none;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
#clear-btn {
|
| 144 |
+
background-color: #E74C3C;
|
| 145 |
+
border: none;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
.output-container {
|
| 149 |
+
margin-top: 1.5rem;
|
| 150 |
+
border: 2px dashed #3498DB;
|
| 151 |
+
border-radius: 10px;
|
| 152 |
+
padding: 10px;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.footer {
|
| 156 |
+
text-align: center;
|
| 157 |
+
margin-top: 2rem;
|
| 158 |
+
font-size: 0.9rem;
|
| 159 |
+
color: #7F8C8D;
|
| 160 |
+
}
|
| 161 |
+
"""
|
| 162 |
+
|
| 163 |
+
with gr.Blocks(css=css, title="Real-Time YOLOv5 Video & Image Object Detection") as demo:
|
| 164 |
+
gr.Markdown("""# Real-Time YOLOv5 Object Detection""", elem_id="title")
|
| 165 |
|
| 166 |
with gr.Tabs():
|
| 167 |
+
with gr.TabItem("Video Detection", elem_classes="tab-item"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
with gr.Row():
|
| 169 |
+
video_input = gr.Video(
|
| 170 |
+
label="Upload Video",
|
| 171 |
+
interactive=True,
|
| 172 |
+
elem_id="video-input"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
with gr.Row(elem_classes="button-row"):
|
| 176 |
+
process_button = gr.Button(
|
| 177 |
+
"Process Video",
|
| 178 |
+
variant="primary",
|
| 179 |
+
elem_id="video-process-btn"
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
with gr.Row(elem_classes="output-container"):
|
| 183 |
+
video_output = gr.Video(
|
| 184 |
+
label="Processed Video",
|
| 185 |
+
elem_id="video-output"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
process_button.click(
|
| 189 |
+
fn=process_video,
|
| 190 |
+
inputs=video_input,
|
| 191 |
+
outputs=video_output
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.TabItem("Image Detection", elem_classes="tab-item"):
|
| 195 |
with gr.Row():
|
| 196 |
+
image_input = gr.Image(
|
| 197 |
+
type="pil",
|
| 198 |
+
label="Upload Image",
|
| 199 |
+
interactive=True
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
# Define image_output before it's referenced
|
| 203 |
+
image_output = gr.Image(
|
| 204 |
+
label="Detected Objects",
|
| 205 |
+
elem_id="image-output"
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
with gr.Row(elem_classes="button-row"):
|
| 209 |
+
clear_button = gr.Button(
|
| 210 |
+
"Clear",
|
| 211 |
+
variant="secondary",
|
| 212 |
+
elem_id="clear-btn"
|
| 213 |
+
)
|
| 214 |
+
submit_button = gr.Button(
|
| 215 |
+
"Detect Objects",
|
| 216 |
+
variant="primary",
|
| 217 |
+
elem_id="submit-btn"
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# Now the clear_button can reference image_output
|
| 221 |
+
clear_button.click(
|
| 222 |
+
fn=lambda: None,
|
| 223 |
+
inputs=None,
|
| 224 |
+
outputs=image_output
|
| 225 |
+
)
|
| 226 |
|
| 227 |
+
submit_button.click(
|
| 228 |
+
fn=process_image,
|
| 229 |
+
inputs=image_input,
|
| 230 |
+
outputs=image_output
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
gr.Markdown("""
|
| 234 |
+
### Powered by YOLOv5.
|
| 235 |
+
This application allows real-time object detection using the YOLOv5 model.
|
| 236 |
+
""", elem_classes="footer")
|
| 237 |
|
| 238 |
demo.launch()
|
|
|