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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -87
app.py CHANGED
@@ -1,87 +0,0 @@
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