Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
|
| 8 |
+
# Load YOLOv8 model
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
return YOLO('yolov8n.pt') # Automatically downloads YOLOv8 pre-trained model
|
| 12 |
+
|
| 13 |
+
model = load_model()
|
| 14 |
+
|
| 15 |
+
# Streamlit app title
|
| 16 |
+
st.title("Object Detection in Video")
|
| 17 |
+
st.write("Upload a video, and the application will detect and label objects frame by frame, and generate a summary.")
|
| 18 |
+
|
| 19 |
+
# File uploader
|
| 20 |
+
uploaded_video = st.file_uploader("Upload a Video", type=["mp4", "avi", "mov"])
|
| 21 |
+
|
| 22 |
+
if uploaded_video:
|
| 23 |
+
# Save the uploaded video to a temporary file
|
| 24 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 25 |
+
temp_file.write(uploaded_video.read())
|
| 26 |
+
video_path = temp_file.name
|
| 27 |
+
|
| 28 |
+
# Open the video file
|
| 29 |
+
video = cv2.VideoCapture(video_path)
|
| 30 |
+
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 31 |
+
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 32 |
+
fps = int(video.get(cv2.CAP_PROP_FPS))
|
| 33 |
+
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 34 |
+
|
| 35 |
+
# Create an output video file
|
| 36 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 37 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 38 |
+
out = cv2.VideoWriter(output_file.name, fourcc, fps, (frame_width, frame_height))
|
| 39 |
+
|
| 40 |
+
# Initialize a set to collect unique detected objects
|
| 41 |
+
detected_objects = set()
|
| 42 |
+
|
| 43 |
+
# Process video frame by frame
|
| 44 |
+
st.write("Processing video...")
|
| 45 |
+
progress_bar = st.progress(0)
|
| 46 |
+
|
| 47 |
+
for i in range(total_frames):
|
| 48 |
+
ret, frame = video.read()
|
| 49 |
+
if not ret:
|
| 50 |
+
break
|
| 51 |
+
|
| 52 |
+
# Object detection on the current frame
|
| 53 |
+
results = model(frame)
|
| 54 |
+
|
| 55 |
+
# Collect unique object names
|
| 56 |
+
detected_objects.update([model.names[int(box.cls)] for box in results[0].boxes])
|
| 57 |
+
|
| 58 |
+
# Annotate frame with bounding boxes
|
| 59 |
+
annotated_frame = results[0].plot()
|
| 60 |
+
|
| 61 |
+
# Write annotated frame to the output video
|
| 62 |
+
out.write(annotated_frame)
|
| 63 |
+
|
| 64 |
+
# Update progress bar
|
| 65 |
+
progress_bar.progress((i + 1) / total_frames)
|
| 66 |
+
|
| 67 |
+
# Release resources
|
| 68 |
+
video.release()
|
| 69 |
+
out.release()
|
| 70 |
+
|
| 71 |
+
# Generate text summary
|
| 72 |
+
if detected_objects:
|
| 73 |
+
detected_objects_list = ", ".join(detected_objects)
|
| 74 |
+
summary_text = f"In this video, the following objects were detected: {detected_objects_list}."
|
| 75 |
+
else:
|
| 76 |
+
summary_text = "No objects were detected in the video."
|
| 77 |
+
|
| 78 |
+
st.write("Summary:")
|
| 79 |
+
st.write(summary_text)
|
| 80 |
+
|
| 81 |
+
# Generate audio summary using gTTS
|
| 82 |
+
tts = gTTS(text=summary_text, lang='en')
|
| 83 |
+
audio_file = os.path.join(tempfile.gettempdir(), "summary.mp3")
|
| 84 |
+
tts.save(audio_file)
|
| 85 |
+
|
| 86 |
+
# Display the output video
|
| 87 |
+
st.write("Video processing complete! Download or watch the labeled video below:")
|
| 88 |
+
st.video(output_file.name)
|
| 89 |
+
st.download_button(
|
| 90 |
+
label="Download Labeled Video",
|
| 91 |
+
data=open(output_file.name, "rb").read(),
|
| 92 |
+
file_name="labeled_video.mp4",
|
| 93 |
+
mime="video/mp4"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Provide audio playback
|
| 97 |
+
st.audio(audio_file, format="audio/mp3")
|
| 98 |
+
st.download_button(
|
| 99 |
+
label="Download Audio Summary",
|
| 100 |
+
data=open(audio_file, "rb").read(),
|
| 101 |
+
file_name="summary.mp3",
|
| 102 |
+
mime="audio/mp3"
|
| 103 |
+
)
|