Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,20 @@ 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 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# List to hold results
|
| 15 |
detection_results = []
|
|
@@ -36,6 +42,9 @@ def detect_video(video):
|
|
| 36 |
|
| 37 |
cap.release()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
return detection_results
|
| 40 |
|
| 41 |
# Gradio Interface
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
from datetime import datetime
|
| 7 |
+
import shutil
|
| 8 |
|
| 9 |
# Load YOLOv5 model
|
| 10 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
|
| 11 |
|
| 12 |
def detect_video(video):
|
| 13 |
+
# Create a temporary file to save the video
|
| 14 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
|
| 15 |
+
tmpfile.write(video.read()) # Save the uploaded video to the temp file
|
| 16 |
+
video_path = tmpfile.name # Get the temporary file path
|
| 17 |
+
|
| 18 |
+
cap = cv2.VideoCapture(video_path) # Open the temporary video file
|
| 19 |
|
| 20 |
# List to hold results
|
| 21 |
detection_results = []
|
|
|
|
| 42 |
|
| 43 |
cap.release()
|
| 44 |
|
| 45 |
+
# Clean up the temporary file
|
| 46 |
+
shutil.remove(video_path)
|
| 47 |
+
|
| 48 |
return detection_results
|
| 49 |
|
| 50 |
# Gradio Interface
|