Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,48 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
results = model(frame)
|
| 11 |
-
return results
|
| 12 |
-
|
| 13 |
-
# Open the video file (or camera)
|
| 14 |
-
cap = cv2.VideoCapture("test_video.mp4") # replace with your video path or camera index (0 for webcam)
|
| 15 |
-
|
| 16 |
-
while cap.isOpened():
|
| 17 |
-
ret, frame = cap.read()
|
| 18 |
-
if not ret:
|
| 19 |
-
break
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
cv2.destroyAllWindows()
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
+
# Load YOLOv5 model
|
| 8 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
|
| 9 |
|
| 10 |
+
def detect_video(video):
|
| 11 |
+
cap = cv2.VideoCapture(video.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# List to hold results
|
| 14 |
+
detection_results = []
|
| 15 |
+
|
| 16 |
+
while cap.isOpened():
|
| 17 |
+
ret, frame = cap.read()
|
| 18 |
+
if not ret:
|
| 19 |
+
break
|
| 20 |
+
|
| 21 |
+
# Get timestamp
|
| 22 |
+
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 23 |
+
|
| 24 |
+
# Perform detection
|
| 25 |
+
results = model(frame)
|
| 26 |
+
|
| 27 |
+
# Extract bounding boxes and confidence scores
|
| 28 |
+
for *xyxy, conf, cls in results.xywh[0]:
|
| 29 |
+
x1, y1, x2, y2 = map(int, xyxy) # Convert to integers
|
| 30 |
+
detection_results.append({
|
| 31 |
+
'timestamp': timestamp,
|
| 32 |
+
'coordinates': {'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2},
|
| 33 |
+
'confidence': float(conf)
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
cap.release()
|
| 37 |
+
|
| 38 |
+
return detection_results
|
| 39 |
|
| 40 |
+
# Gradio Interface
|
| 41 |
+
interface = gr.Interface(fn=detect_video,
|
| 42 |
+
inputs=gr.inputs.Video(source="upload", type="file"),
|
| 43 |
+
outputs="json",
|
| 44 |
+
live=True,
|
| 45 |
+
title="YOLOv5 Video Object Detection",
|
| 46 |
+
description="Upload a video to detect objects and get bounding boxes with timestamps.")
|
| 47 |
|
| 48 |
+
interface.launch()
|
|
|