Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,42 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
-
import
|
| 4 |
from ultralytics import YOLO
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# Initialize YOLOv8 model
|
| 9 |
-
model = YOLO("yolov8n.pt")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
st.
|
| 13 |
-
st.
|
| 14 |
|
| 15 |
# Sidebar for video upload
|
| 16 |
-
st.sidebar.
|
| 17 |
-
uploaded_video = st.sidebar.file_uploader("Choose a video...", type=["mp4", "mov", "avi", "mkv"])
|
| 18 |
|
| 19 |
-
if
|
| 20 |
# Save the uploaded video to a temporary file
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
# Display the uploaded video
|
| 26 |
-
st.
|
| 27 |
|
| 28 |
-
#
|
| 29 |
if st.sidebar.button("Submit"):
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
cap = cv2.VideoCapture(video_path)
|
| 34 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 35 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 36 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 37 |
|
| 38 |
-
# Create a temporary file to save the output video
|
| 39 |
-
temp_output_video = NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 40 |
-
output_video_path = temp_output_video.name
|
| 41 |
-
|
| 42 |
# Define codec and create VideoWriter object
|
| 43 |
-
|
| 44 |
-
out = cv2.VideoWriter(
|
| 45 |
|
| 46 |
-
# Process each frame of the video
|
| 47 |
while cap.isOpened():
|
| 48 |
ret, frame = cap.read()
|
| 49 |
if not ret:
|
|
@@ -52,35 +45,34 @@ if uploaded_video is not None:
|
|
| 52 |
# Perform object detection
|
| 53 |
results = model(frame)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
for
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
out.write(frame)
|
| 67 |
|
|
|
|
| 68 |
cap.release()
|
| 69 |
out.release()
|
| 70 |
|
| 71 |
# Display the processed video
|
| 72 |
-
st.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
mime="video/mp4"
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
# Clean up temporary files
|
| 85 |
-
os.remove(video_path)
|
| 86 |
-
os.remove(output_video_path)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
+
import tempfile
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Title and Description
|
| 8 |
+
st.title("🔍 YOLOv8 Object Detection on Video")
|
| 9 |
+
st.write("Upload a video file to detect objects using the YOLOv8 model. You can download the processed video with bounding boxes around detected objects.")
|
| 10 |
|
| 11 |
# Sidebar for video upload
|
| 12 |
+
uploaded_file = st.sidebar.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
|
|
|
|
| 13 |
|
| 14 |
+
if uploaded_file is not None:
|
| 15 |
# Save the uploaded video to a temporary file
|
| 16 |
+
temp_input_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 17 |
+
temp_input_file.write(uploaded_file.read())
|
| 18 |
+
temp_input_file.close()
|
| 19 |
|
| 20 |
# Display the uploaded video
|
| 21 |
+
st.video(temp_input_file.name)
|
| 22 |
|
| 23 |
+
# Process video button
|
| 24 |
if st.sidebar.button("Submit"):
|
| 25 |
+
# Load YOLOv8 model
|
| 26 |
+
model = YOLO("yolov8n.pt")
|
| 27 |
+
|
| 28 |
+
# Open the input video file
|
| 29 |
+
cap = cv2.VideoCapture(temp_input_file.name)
|
| 30 |
|
| 31 |
+
# Get video properties
|
|
|
|
| 32 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 33 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 34 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Define codec and create VideoWriter object
|
| 37 |
+
temp_output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 38 |
+
out = cv2.VideoWriter(temp_output_file.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
| 39 |
|
|
|
|
| 40 |
while cap.isOpened():
|
| 41 |
ret, frame = cap.read()
|
| 42 |
if not ret:
|
|
|
|
| 45 |
# Perform object detection
|
| 46 |
results = model(frame)
|
| 47 |
|
| 48 |
+
# Access detection results and draw bounding boxes
|
| 49 |
+
if results:
|
| 50 |
+
for result in results:
|
| 51 |
+
boxes = result.boxes # Access boxes attribute
|
| 52 |
+
for box in boxes:
|
| 53 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 54 |
+
conf = box.conf[0]
|
| 55 |
+
cls = box.cls[0]
|
| 56 |
+
label = f'{model.names[int(cls)]} {conf:.2f}'
|
| 57 |
+
|
| 58 |
+
# Draw bounding boxes on the frame
|
| 59 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 60 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
| 61 |
+
|
| 62 |
+
# Write the frame with bounding boxes to the output video
|
| 63 |
out.write(frame)
|
| 64 |
|
| 65 |
+
# Release resources
|
| 66 |
cap.release()
|
| 67 |
out.release()
|
| 68 |
|
| 69 |
# Display the processed video
|
| 70 |
+
st.video(temp_output_file.name)
|
| 71 |
+
|
| 72 |
+
# Provide download link for processed video
|
| 73 |
+
st.sidebar.download_button(
|
| 74 |
+
label="Download Processed Video",
|
| 75 |
+
data=open(temp_output_file.name, "rb"),
|
| 76 |
+
file_name="processed_video.mp4",
|
| 77 |
+
mime="video/mp4"
|
| 78 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|