Srikanth0804 commited on
Commit
b233495
·
verified ·
1 Parent(s): ab96d88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -74
app.py CHANGED
@@ -1,74 +1,87 @@
1
- # streamlit_yolo_video.py
2
-
3
- import streamlit as st
4
- import cv2
5
- import numpy as np
6
- from ultralytics import YOLO
7
- import tempfile
8
-
9
- # -------------------------------
10
- # Streamlit App Title
11
- # -------------------------------
12
- st.title("YOLOv8 Object Detection on Video")
13
-
14
- # -------------------------------
15
- # Load YOLO Detection Model
16
- # -------------------------------
17
- @st.cache_resource
18
- def load_model(model_name="yolov8n.pt"):
19
- return YOLO(model_name)
20
-
21
- model = load_model()
22
-
23
- # -------------------------------
24
- # Upload Video
25
- # -------------------------------
26
- uploaded_file = st.file_uploader("Upload a video", type=["mp4", "mov", "avi", "mkv"])
27
-
28
- if uploaded_file is not None:
29
- # Save uploaded video temporarily
30
- tfile = tempfile.NamedTemporaryFile(delete=False)
31
- tfile.write(uploaded_file.read())
32
- video_path = tfile.name
33
-
34
- # Open video
35
- cap = cv2.VideoCapture(video_path)
36
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
37
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
38
- fps = cap.get(cv2.CAP_PROP_FPS)
39
-
40
- # Output video writer
41
- output_path = "output_video.mp4"
42
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
43
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
44
-
45
- stframe = st.empty()
46
- st.info("Processing video... This may take some time depending on length.")
47
-
48
- while cap.isOpened():
49
- ret, frame = cap.read()
50
- if not ret:
51
- break
52
-
53
- # YOLO expects RGB
54
- frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
55
- results = model(frame_rgb)
56
-
57
- # Annotated frame
58
- annotated_frame = results[0].plot()
59
- annotated_frame_bgr = cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR)
60
-
61
- # Write frame to output video
62
- out.write(annotated_frame_bgr)
63
-
64
- # Display in Streamlit
65
- stframe.image(annotated_frame, channels="RGB", use_column_width=True)
66
-
67
- cap.release()
68
- out.release()
69
-
70
- st.success("Video processing completed!")
71
-
72
- # Download button
73
- with open(output_path, "rb") as f:
74
- st.download_button("Download Annotated Video", f, "annotated_video.mp4")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import tempfile
4
+ from ultralytics import YOLO
5
+ import imageio
6
+ import os
7
+
8
+ # ---------------- YOLO Model ----------------
9
+ st.title("🎥 YOLOv8 Object Tracking on Video")
10
+ model = YOLO("yolov8n.pt") # Pretrained YOLOv8n model
11
+
12
+ # ---------------- Function to Process Video ----------------
13
+ def detect_objects_in_video(video_path):
14
+ cap = cv2.VideoCapture(video_path)
15
+ fps = int(cap.get(cv2.CAP_PROP_FPS)) or 25
16
+
17
+ # Temporary output file
18
+ temp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
19
+ output_path = temp_output.name
20
+ temp_output.close()
21
+
22
+ writer = imageio.get_writer(output_path, fps=fps, codec="libx264")
23
+
24
+ stframe = st.empty()
25
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
26
+ processed_frames = 0
27
+
28
+ while cap.isOpened():
29
+ ret, frame = cap.read()
30
+ if not ret:
31
+ break
32
+
33
+ results = model(frame, imgsz=1280, conf=0.25)
34
+ annotated_frame = results[0].plot()
35
+
36
+ writer.append_data(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
37
+
38
+ # Progress bar
39
+ processed_frames += 1
40
+ stframe.text(f"Processing frame {processed_frames}/{total_frames}...")
41
+
42
+ cap.release()
43
+ writer.close()
44
+
45
+ # Read video as bytes for Streamlit
46
+ with open(output_path, "rb") as f:
47
+ video_bytes = f.read()
48
+
49
+ try:
50
+ os.remove(output_path)
51
+ except PermissionError:
52
+ pass
53
+
54
+ return video_bytes
55
+
56
+ # ---------------- Streamlit UI ----------------
57
+ st.write("Upload a video and see detections in MP4 format.")
58
+
59
+ uploaded_file = st.file_uploader("Upload a video", type=["mp4", "mov", "avi"])
60
+
61
+ if uploaded_file is not None:
62
+ # Save uploaded video to temp file
63
+ temp_input = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
64
+ temp_input.write(uploaded_file.read())
65
+ temp_input.close()
66
+
67
+ st.subheader("📥 Original Video")
68
+ st.video(temp_input.name) # Display original video
69
+
70
+ st.write("🔄 Processing video with YOLOv8... please wait")
71
+ video_bytes = detect_objects_in_video(temp_input.name)
72
+
73
+ st.subheader("✅ Processed Video")
74
+ st.video(video_bytes) # Display processed video
75
+
76
+ st.download_button(
77
+ label="⬇️ Download Processed Video",
78
+ data=video_bytes,
79
+ file_name="processed_output.mp4",
80
+ mime="video/mp4"
81
+ )
82
+
83
+ # Cleanup
84
+ try:
85
+ os.remove(temp_input.name)
86
+ except PermissionError:
87
+ pass