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

Upload app.py

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