Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,42 +15,28 @@ model.conf = 0.5
|
|
15 |
if device.type == 'cuda':
|
16 |
model.half()
|
17 |
|
18 |
-
def process_frame(
|
19 |
-
"""
|
20 |
-
|
21 |
-
|
22 |
-
if not cap.isOpened():
|
23 |
-
print("Error: Could not open video stream.")
|
24 |
-
return None
|
25 |
-
|
26 |
-
ret, frame = cap.read()
|
27 |
-
cap.release()
|
28 |
-
|
29 |
-
if not ret:
|
30 |
-
print("Error: Could not read frame.")
|
31 |
return None
|
32 |
|
33 |
try:
|
34 |
-
|
35 |
-
image_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
36 |
-
|
37 |
with torch.no_grad():
|
38 |
results = model(image_pil)
|
39 |
|
40 |
rendered_images = results.render()
|
41 |
-
|
42 |
-
print("Frame processed successfully!")
|
43 |
-
return processed_image
|
44 |
|
45 |
except Exception as e:
|
46 |
-
print(f"
|
47 |
-
return
|
48 |
|
49 |
def process_uploaded_image(image):
|
50 |
-
"""Processes
|
51 |
if image is None:
|
52 |
return None
|
53 |
-
|
54 |
try:
|
55 |
image_pil = Image.fromarray(image)
|
56 |
with torch.no_grad():
|
@@ -58,7 +44,7 @@ def process_uploaded_image(image):
|
|
58 |
|
59 |
rendered_images = results.render()
|
60 |
return np.array(rendered_images[0]) if rendered_images else image
|
61 |
-
|
62 |
except Exception as e:
|
63 |
print(f"Error processing image: {e}")
|
64 |
return image
|
@@ -71,17 +57,19 @@ with gr.Blocks(title="Real-Time Object Detection") as app:
|
|
71 |
# 📷 Live Webcam Tab
|
72 |
with gr.TabItem("📷 Live Camera"):
|
73 |
with gr.Row():
|
74 |
-
webcam_input = gr.
|
75 |
live_output = gr.Image(label="Processed Feed")
|
76 |
-
|
|
|
|
|
77 |
|
78 |
# 🖼️ Image Upload Tab (With Submit Button)
|
79 |
with gr.TabItem("🖼️ Image Upload"):
|
80 |
with gr.Row():
|
81 |
upload_input = gr.Image(type="numpy", label="Upload Image")
|
82 |
-
submit_button = gr.Button("Submit")
|
83 |
upload_output = gr.Image(label="Detection Result")
|
84 |
|
85 |
-
submit_button.click(process_uploaded_image, inputs=upload_input, outputs=upload_output)
|
86 |
|
87 |
app.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
15 |
if device.type == 'cuda':
|
16 |
model.half()
|
17 |
|
18 |
+
def process_frame(image):
|
19 |
+
"""Processes a frame from the webcam and applies YOLOv5 object detection."""
|
20 |
+
if image is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
return None
|
22 |
|
23 |
try:
|
24 |
+
image_pil = Image.fromarray(image)
|
|
|
|
|
25 |
with torch.no_grad():
|
26 |
results = model(image_pil)
|
27 |
|
28 |
rendered_images = results.render()
|
29 |
+
return np.array(rendered_images[0]) if rendered_images else image
|
|
|
|
|
30 |
|
31 |
except Exception as e:
|
32 |
+
print(f"Error processing frame: {e}")
|
33 |
+
return image
|
34 |
|
35 |
def process_uploaded_image(image):
|
36 |
+
"""Processes an uploaded image and applies YOLOv5 object detection."""
|
37 |
if image is None:
|
38 |
return None
|
39 |
+
|
40 |
try:
|
41 |
image_pil = Image.fromarray(image)
|
42 |
with torch.no_grad():
|
|
|
44 |
|
45 |
rendered_images = results.render()
|
46 |
return np.array(rendered_images[0]) if rendered_images else image
|
47 |
+
|
48 |
except Exception as e:
|
49 |
print(f"Error processing image: {e}")
|
50 |
return image
|
|
|
57 |
# 📷 Live Webcam Tab
|
58 |
with gr.TabItem("📷 Live Camera"):
|
59 |
with gr.Row():
|
60 |
+
webcam_input = gr.Image(source="webcam", label="Live Feed")
|
61 |
live_output = gr.Image(label="Processed Feed")
|
62 |
+
|
63 |
+
# Process frame when new frame is available
|
64 |
+
webcam_input.change(process_frame, inputs=webcam_input, outputs=live_output)
|
65 |
|
66 |
# 🖼️ Image Upload Tab (With Submit Button)
|
67 |
with gr.TabItem("🖼️ Image Upload"):
|
68 |
with gr.Row():
|
69 |
upload_input = gr.Image(type="numpy", label="Upload Image")
|
70 |
+
submit_button = gr.Button("Submit")
|
71 |
upload_output = gr.Image(label="Detection Result")
|
72 |
|
73 |
+
submit_button.click(process_uploaded_image, inputs=upload_input, outputs=upload_output)
|
74 |
|
75 |
app.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|