-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp_interactive.py
More file actions
406 lines (343 loc) · 14.9 KB
/
Copy pathapp_interactive.py
File metadata and controls
406 lines (343 loc) · 14.9 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import streamlit as st
import os
import tempfile
import cv2
import numpy as np
from PIL import Image
import time
import subprocess
import sys
from pathlib import Path
from streamlit_drawable_canvas import st_canvas
# Import from existing codebase
from visualization import process_cctv_to_top_view, get_perspective_transform
from simulation_enhancement import enhanced_process_cctv_to_top_view
from person_detection import AreaManager, PersonDetector
from density_estimation import CrowdDensityEstimator
from anomaly_detection import AnomalyDetector
from video_preprocessor import preprocess_video
# Set page config
st.set_page_config(
page_title="BhedChaal Interactive - CCTV Analysis",
page_icon="🎥",
layout="wide",
initial_sidebar_state="expanded",
)
# Define global variables
TEMP_DIR = "temp"
if not os.path.exists(TEMP_DIR):
os.makedirs(TEMP_DIR)
# Initialize session state variables
if 'points' not in st.session_state:
st.session_state.points = []
if 'video_path' not in st.session_state:
st.session_state.video_path = None
if 'frame' not in st.session_state:
st.session_state.frame = None
if 'perspective_points' not in st.session_state:
st.session_state.perspective_points = []
if 'canvas_result' not in st.session_state:
st.session_state.canvas_result = None
if 'step' not in st.session_state:
st.session_state.step = "upload"
if 'results' not in st.session_state:
st.session_state.results = None
def save_uploaded_file(uploaded_file):
"""Save uploaded file to temporary directory and return the file path"""
file_path = os.path.join(TEMP_DIR, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
return file_path
def display_interactive_point_selection(frame):
"""
Display interactive canvas for point selection
"""
st.write("### Select 4 points for perspective transformation")
st.write("Click to place points on the image in clockwise order: top-left, top-right, bottom-right, bottom-left")
# Get image dimensions
h, w = frame.shape[:2]
# Calculate display width maintaining aspect ratio
display_width = 800
display_height = int(h * display_width / w)
# Convert CV2 BGR image to RGB for display
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Create two columns - one for the canvas, one for the instructions/buttons
col1, col2 = st.columns([3, 1])
with col1:
# Create the canvas for drawing
canvas_result = st_canvas(
fill_color="rgba(255, 165, 0, 0.3)", # Fill color for circle
stroke_width=2,
stroke_color="#00FF00", # Green border for circle
background_image=Image.fromarray(rgb_frame),
update_streamlit=True,
height=display_height,
width=display_width,
drawing_mode="point",
point_display_radius=5,
key="perspective_canvas",
)
with col2:
st.write("### Point Selection")
st.write("Click on the image to select points")
# Display current points
if canvas_result.json_data is not None and "objects" in canvas_result.json_data:
points = []
for obj in canvas_result.json_data["objects"]:
if obj["type"] == "point":
# Convert from canvas coordinates to original image coordinates
x = int(obj["left"] * w / display_width)
y = int(obj["top"] * h / display_height)
points.append([x, y])
# Update session state
st.session_state.points = points
# Display the points
if len(points) > 0:
st.write("Selected points:")
for i, point in enumerate(points):
st.write(f"Point {i+1}: ({point[0]}, {point[1]})")
# Button to clear points
if st.button("Clear Points"):
st.session_state.points = []
st.rerun()
# Button to confirm points
if st.button("Confirm Points") and len(st.session_state.points) >= 4:
# Take the first 4 points
st.session_state.perspective_points = st.session_state.points[:4]
st.session_state.step = "options"
st.rerun()
# Create a preview with the current points
if st.session_state.points:
preview_img = frame.copy()
for i, point in enumerate(st.session_state.points[:4]): # Limit to first 4 points
# Draw points on image
cv2.circle(preview_img, tuple(point), 5, (0, 255, 0), -1)
# Draw lines connecting points
if i > 0:
cv2.line(preview_img, tuple(st.session_state.points[i-1]), tuple(point), (0, 255, 0), 2)
# Connect last point to first if we have 4 points
if i == 3:
cv2.line(preview_img, tuple(point), tuple(st.session_state.points[0]), (0, 255, 0), 2)
# Display the preview
st.image(preview_img, caption="Preview with selected points", width=display_width, channels="BGR")
return st.session_state.points
def run_video_analysis(video_path, src_points, options):
"""Run video analysis with the existing code and return results"""
# Create output paths
video_name = Path(video_path).stem
output_dir = os.path.join(TEMP_DIR, "output_videos")
os.makedirs(output_dir, exist_ok=True)
output_original = os.path.join(output_dir, f"{video_name}_enhanced_original.mp4")
output_top_view = os.path.join(output_dir, f"{video_name}_enhanced_top_view.mp4")
output_density = os.path.join(output_dir, f"{video_name}_enhanced_density.mp4")
# Process the video
if options["enhanced"]:
result = enhanced_process_cctv_to_top_view(
video_path,
output_original,
None, # Use the first frame as calibration
src_points,
use_tracking=options["tracking"],
yolo_model_size=options["model_size"],
csrnet_model_path=options.get("csrnet_weights"),
density_threshold=options["density_threshold"],
max_points=options["max_points"],
preprocess_video=options["preprocess"],
anomaly_threshold=options["anomaly_threshold"],
stampede_threshold=options["stampede_threshold"],
max_bottlenecks=options["max_bottlenecks"]
)
else:
result = process_cctv_to_top_view(
video_path,
output_original,
None, # Use the first frame as calibration
src_points,
use_tracking=options["tracking"],
yolo_model_size=options["model_size"],
csrnet_model_path=options.get("csrnet_weights"),
preprocess_video=options["preprocess"],
anomaly_threshold=options["anomaly_threshold"],
stampede_threshold=options["stampede_threshold"],
max_bottlenecks=options["max_bottlenecks"]
)
return {
"original_video": output_original,
"top_view_video": output_top_view,
"density_video": output_density,
"result": result
}
def show_results(results):
"""Display the results of the video analysis"""
st.write("## Analysis Results")
# Create three columns for the different videos
col1, col2, col3 = st.columns(3)
with col1:
st.write("### Original Video with Detections")
if os.path.exists(results["original_video"]):
st.video(results["original_video"])
else:
st.error("Original video output not found.")
with col2:
st.write("### Top View")
if os.path.exists(results["top_view_video"]):
st.video(results["top_view_video"])
else:
st.error("Top view video output not found.")
with col3:
st.write("### Density Visualization")
if os.path.exists(results["density_video"]):
st.video(results["density_video"])
else:
st.error("Density video output not found.")
# Show additional outputs
st.write("### Processing Results")
st.json(results["result"])
# Button to restart
if st.button("Process Another Video"):
st.session_state.step = "upload"
st.session_state.video_path = None
st.session_state.frame = None
st.session_state.perspective_points = []
st.session_state.points = []
st.session_state.results = None
st.rerun()
def main():
st.title("BhedChaal - Interactive CCTV Crowd Analysis")
# Upload video step
if st.session_state.step == "upload":
# Video input
st.write("### Upload Video")
uploaded_file = st.file_uploader("Upload Video", type=["mp4", "avi", "mov"])
if uploaded_file is not None:
# Save uploaded file
video_path = save_uploaded_file(uploaded_file)
st.session_state.video_path = video_path
st.write(f"Video uploaded successfully: {uploaded_file.name}")
# Get the first frame for perspective selection
cap = cv2.VideoCapture(video_path)
ret, frame = cap.read()
cap.release()
if ret:
st.session_state.frame = frame
st.session_state.step = "perspective"
st.rerun()
else:
st.error("Failed to read the uploaded video. Please try a different file.")
# Perspective selection step
elif st.session_state.step == "perspective":
if st.session_state.frame is not None:
display_interactive_point_selection(st.session_state.frame)
# Button to go back to upload
if st.button("Back to Upload"):
st.session_state.step = "upload"
st.rerun()
# Options selection step
elif st.session_state.step == "options":
st.write("### Processing Options")
# Sidebar for options
st.sidebar.title("Options")
# Processing options
enhanced = st.sidebar.checkbox("Enhanced Visualization", value=True)
tracking = st.sidebar.checkbox("Enable Tracking", value=True)
preprocess = st.sidebar.checkbox("Preprocess Video", value=True)
model_size = st.sidebar.selectbox(
"YOLOv8 Model Size",
["n", "s", "m", "l", "x"],
index=4 # Default to 'x'
)
# Enhanced options
density_threshold = 0.2
max_points = 200
anomaly_threshold = 30
stampede_threshold = 35
max_bottlenecks = 3
if enhanced:
st.sidebar.header("Enhanced Options")
density_threshold = st.sidebar.slider(
"Density Threshold",
min_value=0.1,
max_value=0.5,
value=0.2,
step=0.05
)
max_points = st.sidebar.slider(
"Max Density Points",
min_value=50,
max_value=500,
value=200,
step=50
)
anomaly_threshold = st.sidebar.slider(
"Anomaly Threshold",
min_value=10,
max_value=50,
value=30,
step=5
)
stampede_threshold = st.sidebar.slider(
"Stampede Threshold",
min_value=15,
max_value=55,
value=35,
step=5
)
max_bottlenecks = st.sidebar.slider(
"Max Bottlenecks",
min_value=1,
max_value=5,
value=3,
step=1
)
# Display selected points preview
if st.session_state.frame is not None and st.session_state.perspective_points:
preview_img = st.session_state.frame.copy()
for i, point in enumerate(st.session_state.perspective_points):
# Draw points on image
cv2.circle(preview_img, tuple(point), 5, (0, 255, 0), -1)
# Draw lines connecting points
if i > 0:
cv2.line(preview_img, tuple(st.session_state.perspective_points[i-1]), tuple(point), (0, 255, 0), 2)
# Connect last point to first
if i == len(st.session_state.perspective_points) - 1:
cv2.line(preview_img, tuple(point), tuple(st.session_state.perspective_points[0]), (0, 255, 0), 2)
# Display the preview
st.image(preview_img, caption="Selected perspective points", width=800, channels="BGR")
# Process button
col1, col2 = st.columns(2)
with col1:
if st.button("Back to Point Selection"):
st.session_state.step = "perspective"
st.rerun()
with col2:
if st.button("Process Video"):
with st.spinner("Processing video... This may take a while."):
# Collect options
options = {
"enhanced": enhanced,
"tracking": tracking,
"model_size": model_size,
"preprocess": preprocess,
"density_threshold": density_threshold,
"max_points": max_points,
"anomaly_threshold": anomaly_threshold,
"stampede_threshold": stampede_threshold,
"max_bottlenecks": max_bottlenecks
}
# Run analysis
results = run_video_analysis(st.session_state.video_path, st.session_state.perspective_points, options)
# Store results and move to results page
st.session_state.results = results
st.session_state.step = "results"
st.rerun()
# Results step
elif st.session_state.step == "results":
if st.session_state.results:
show_results(st.session_state.results)
else:
st.error("No results available. Please process a video first.")
if st.button("Back to Upload"):
st.session_state.step = "upload"
st.rerun()
if __name__ == "__main__":
main()