Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -108,6 +108,16 @@ def process_image(image):
|
|
108 |
|
109 |
return Image.fromarray(img)
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
css = """
|
112 |
#title {
|
113 |
text-align: center;
|
@@ -145,6 +155,11 @@ css = """
|
|
145 |
border: none;
|
146 |
}
|
147 |
|
|
|
|
|
|
|
|
|
|
|
148 |
.output-container {
|
149 |
margin-top: 1.5rem;
|
150 |
border: 2px dashed #3498DB;
|
@@ -192,6 +207,12 @@ with gr.Blocks(css=css, title="Real-Time YOLOv5 Video & Image Object Detection")
|
|
192 |
)
|
193 |
|
194 |
with gr.TabItem("Image Detection", elem_classes="tab-item"):
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
with gr.Row():
|
196 |
image_input = gr.Image(
|
197 |
type="pil",
|
@@ -199,31 +220,46 @@ with gr.Blocks(css=css, title="Real-Time YOLOv5 Video & Image Object Detection")
|
|
199 |
interactive=True
|
200 |
)
|
201 |
|
202 |
-
#
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
@@ -235,4 +271,4 @@ with gr.Blocks(css=css, title="Real-Time YOLOv5 Video & Image Object Detection")
|
|
235 |
This application allows real-time object detection using the YOLOv5 model.
|
236 |
""", elem_classes="footer")
|
237 |
|
238 |
-
demo.launch()
|
|
|
108 |
|
109 |
return Image.fromarray(img)
|
110 |
|
111 |
+
|
112 |
+
def use_example_image():
|
113 |
+
# Function to load the example image
|
114 |
+
example_path = "pexels-hikaique-109919.jpg"
|
115 |
+
if os.path.exists(example_path):
|
116 |
+
return Image.open(example_path)
|
117 |
+
else:
|
118 |
+
print(f"Warning: Example image not found at {example_path}")
|
119 |
+
return None
|
120 |
+
|
121 |
css = """
|
122 |
#title {
|
123 |
text-align: center;
|
|
|
155 |
border: none;
|
156 |
}
|
157 |
|
158 |
+
#example-btn {
|
159 |
+
background-color: #27AE60;
|
160 |
+
border: none;
|
161 |
+
}
|
162 |
+
|
163 |
.output-container {
|
164 |
margin-top: 1.5rem;
|
165 |
border: 2px dashed #3498DB;
|
|
|
207 |
)
|
208 |
|
209 |
with gr.TabItem("Image Detection", elem_classes="tab-item"):
|
210 |
+
# Define image_output before it's referenced
|
211 |
+
image_output = gr.Image(
|
212 |
+
label="Detected Objects",
|
213 |
+
elem_id="image-output"
|
214 |
+
)
|
215 |
+
|
216 |
with gr.Row():
|
217 |
image_input = gr.Image(
|
218 |
type="pil",
|
|
|
220 |
interactive=True
|
221 |
)
|
222 |
|
223 |
+
# Place buttons immediately below the upload section
|
|
|
|
|
|
|
|
|
|
|
224 |
with gr.Row(elem_classes="button-row"):
|
225 |
clear_button = gr.Button(
|
226 |
"Clear",
|
227 |
variant="secondary",
|
228 |
elem_id="clear-btn"
|
229 |
)
|
230 |
+
example_button = gr.Button(
|
231 |
+
"Use Example Image",
|
232 |
+
variant="secondary",
|
233 |
+
elem_id="example-btn"
|
234 |
+
)
|
235 |
submit_button = gr.Button(
|
236 |
"Detect Objects",
|
237 |
variant="primary",
|
238 |
elem_id="submit-btn"
|
239 |
)
|
240 |
|
241 |
+
with gr.Row(elem_classes="output-container"):
|
242 |
+
pass
|
243 |
+
|
244 |
+
# Button click functions
|
245 |
+
clear_button.click(
|
246 |
+
fn=lambda: None,
|
247 |
+
inputs=None,
|
248 |
+
outputs=image_input
|
249 |
+
)
|
250 |
+
|
251 |
clear_button.click(
|
252 |
fn=lambda: None,
|
253 |
inputs=None,
|
254 |
outputs=image_output
|
255 |
)
|
256 |
|
257 |
+
example_button.click(
|
258 |
+
fn=use_example_image,
|
259 |
+
inputs=None,
|
260 |
+
outputs=image_input
|
261 |
+
)
|
262 |
+
|
263 |
submit_button.click(
|
264 |
fn=process_image,
|
265 |
inputs=image_input,
|
|
|
271 |
This application allows real-time object detection using the YOLOv5 model.
|
272 |
""", elem_classes="footer")
|
273 |
|
274 |
+
demo.launch()
|