-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (142 loc) · 7.25 KB
/
app.py
File metadata and controls
167 lines (142 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import streamlit as st
import os
import subprocess
import shutil
from main import process_video
from detector import VehicleDetector
@st.cache_resource
def load_detector():
"""Load the model once and keep it in cache for speed and RAM safety."""
return VehicleDetector()
st.set_page_config(page_title="Smart Traffic System", page_icon="🚦", layout="centered")
st.title("🚦 Smart Traffic Monitoring System")
st.markdown("""
Upload a traffic video to analyze vehicle counts, bounding boxes, license plates, and helmet safety status!
""")
# Setup directories
os.makedirs("uploads", exist_ok=True)
os.makedirs("output", exist_ok=True)
st.markdown("### Choose an input method:")
tab1, tab2, tab3 = st.tabs(["Upload File", "YouTube Link", "Demo Video"])
run_analysis = False
input_path = None
with tab1:
uploaded_file = st.file_uploader("Upload a traffic video (.mp4)", type=["mp4"])
if uploaded_file is not None:
st.video(uploaded_file)
if st.button("Run AI Analysis", type="primary", key="file_btn"):
input_path = os.path.join("uploads", uploaded_file.name)
with open(input_path, "wb") as f:
f.write(uploaded_file.getbuffer())
run_analysis = True
with tab2:
yt_url = st.text_input("Paste a YouTube Video Link:")
if yt_url:
st.video(yt_url)
st.info("Note: To protect server resources, only the first 20 seconds will be analyzed!")
if st.button("Run AI on YouTube Clip", type="primary", key="yt_btn"):
with st.spinner("Downloading 20-second clip from YouTube..."):
target_out = "uploads/youtube_vid.mp4"
if os.path.exists(target_out): os.remove(target_out)
try:
import yt_dlp
# Attempt 1: Safe 20s clipped download
ydl_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
'outtmpl': target_out,
'download_ranges': yt_dlp.utils.download_range_func(None, [(0, 20)]),
'force_keyframes_at_cuts': True,
'quiet': True
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([yt_url])
except Exception:
# Attempt 2: Fallback to full download (capped at 50MB) if ffmpeg slicing fails for Shorts
if os.path.exists(target_out): os.remove(target_out)
try:
ydl_opts_fallback = {
'format': 'best[ext=mp4]/best',
'max_filesize': 50 * 1024 * 1024,
'outtmpl': target_out,
'noplaylist': True,
'quiet': True
}
with yt_dlp.YoutubeDL(ydl_opts_fallback) as ydl_fb:
ydl_fb.download([yt_url])
except Exception as e_fallback:
st.error(f"Download heavily failed: {e_fallback}")
if os.path.exists(target_out):
input_path = target_out
run_analysis = True
else:
st.error("Download failed to create MP4.")
with tab3:
st.info("🎥 Don't have a traffic video handy? Run the AI on the built-in demo track!")
if os.path.exists("sample_traffic.mp4"):
if st.button("Run AI on Demo Video", type="primary", key="demo_btn"):
input_path = "sample_traffic.mp4"
run_analysis = True
if run_analysis and input_path:
with st.spinner("Processing video through YOLOv8 and DeepSORT..."):
output_mp4v_path = os.path.join("output", "annotated_raw.mp4")
output_h264_path = os.path.join("output", "annotated_playable.mp4")
csv_path = os.path.join("output", "traffic_log.csv")
# Clean old runs
if os.path.exists(output_mp4v_path): os.remove(output_mp4v_path)
if os.path.exists(output_h264_path): os.remove(output_h264_path)
if os.path.exists(csv_path): os.remove(csv_path)
progress_bar = st.progress(0)
status_text = st.empty()
# 1. Run inference using the CACHED detector with High-Stability Error Trapping
try:
detector = load_detector()
success = process_video(input_path, output_mp4v_path, csv_path, detector, progress_bar, status_text)
except Exception as e:
st.error("⚠️ SYSTEM REBOOT REQUIRED")
st.warning(f"""
This application is currently running on a Limited 1GB Free Cloud Server.
The AI process was interrupted to prevent a total crash.
**Technical Note for Recruiters:** This illustrates the 'Memory-Wall' constraint in free-tier deployment.
In a production environment (AWS/GCP), this would be resolved with a higher RAM instance.
**Error Details:** {e}
""")
success = False
if success:
status_text.text("Converting video to H264 for web playback...")
# 2. Convert standard OpenCV mp4v to h264 for web browser using ffmpeg
try:
subprocess.run(
["ffmpeg", "-y", "-i", output_mp4v_path, "-vcodec", "libx264", "-acodec", "aac", output_h264_path],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
status_text.text("Conversion complete!")
st.success("Analysis Finished Successfully!")
st.subheader("Results")
# Display playable video
if os.path.exists(output_h264_path):
st.video(output_h264_path)
# Display Download Buttons
col1, col2 = st.columns(2)
with col1:
if os.path.exists(output_h264_path):
with open(output_h264_path, "rb") as f:
st.download_button("⬇️ Download Annotated Video", f.read(), file_name="annotated_traffic.mp4", mime="video/mp4")
with col2:
if os.path.exists(csv_path):
with open(csv_path, "rb") as f:
st.download_button("⬇️ Download CSV Log", f.read(), file_name="traffic_log.csv", mime="text/csv")
# Optionally display CSV dump
import pandas as pd
if os.path.exists(csv_path):
try:
df = pd.read_csv(csv_path)
if not df.empty:
st.dataframe(df)
except:
pass
except Exception as e:
status_text.text(f"Error converting video: {e}. Raw clip is still available.")
with open(output_mp4v_path, "rb") as f:
st.download_button("⬇️ Download Raw Video (.mp4v)", f.read(), file_name="annotated_raw.mp4")
else:
st.error("Failed to process the video.")