Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import cv2
|
|
| 5 |
import time
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
|
|
|
| 8 |
|
| 9 |
# Create cache directory for models
|
| 10 |
os.makedirs("models", exist_ok=True)
|
|
@@ -91,16 +92,37 @@ def process_video(video_path):
|
|
| 91 |
|
| 92 |
return output_path
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
video_input = gr.Video(label="Upload Video")
|
| 100 |
-
process_button = gr.Button("Process Video")
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import time
|
| 6 |
import os
|
| 7 |
from pathlib import Path
|
| 8 |
+
from PIL import Image
|
| 9 |
|
| 10 |
# Create cache directory for models
|
| 11 |
os.makedirs("models", exist_ok=True)
|
|
|
|
| 92 |
|
| 93 |
return output_path
|
| 94 |
|
| 95 |
+
def process_image(image):
|
| 96 |
+
img = np.array(image)
|
| 97 |
+
results = model(img, size=640)
|
| 98 |
|
| 99 |
+
detections = results.pred[0].cpu().numpy()
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
for *xyxy, conf, cls in detections:
|
| 102 |
+
x1, y1, x2, y2 = map(int, xyxy)
|
| 103 |
+
class_id = int(cls)
|
| 104 |
+
color = colors[class_id].tolist()
|
| 105 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), color, 3, lineType=cv2.LINE_AA)
|
| 106 |
+
label = f"{model.names[class_id]} {conf:.2f}"
|
| 107 |
+
cv2.putText(img, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
|
| 108 |
|
| 109 |
+
return Image.fromarray(img)
|
| 110 |
|
| 111 |
+
# Gradio Interface
|
| 112 |
+
with gr.Blocks(title="Real-Time YOLOv5 Video & Image Detection") as demo:
|
| 113 |
+
gr.Markdown("# Real-Time YOLOv5 Object Detection")
|
| 114 |
+
|
| 115 |
+
with gr.Tabs():
|
| 116 |
+
with gr.TabItem("Video Detection"):
|
| 117 |
+
with gr.Row():
|
| 118 |
+
video_input = gr.Video(label="Upload Video")
|
| 119 |
+
process_button = gr.Button("Process Video")
|
| 120 |
+
video_output = gr.Video(label="Processed Video")
|
| 121 |
+
process_button.click(fn=process_video, inputs=video_input, outputs=video_output)
|
| 122 |
+
|
| 123 |
+
with gr.TabItem("Image Detection"):
|
| 124 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 125 |
+
image_output = gr.Image(label="Detected Objects")
|
| 126 |
+
image_input.change(fn=process_image, inputs=image_input, outputs=image_output)
|
| 127 |
+
|
| 128 |
+
demo.launch()
|