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